scripting newbie... some help please?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting scripting newbie... some help please?
# 1  
Old 03-03-2009
scripting newbie... some help please?

hi all,

i am just getting in to bash scripting, so don't be too harsh...

i've created this little backup script, and it's just awfull... ugly, doesn't work like I want it to, the works. anyways, i was hoping some of you might help me improve it and learn a little in the process.

what i wanted to do:
1. interactively take a filename (from user input) to backup w/ tar
1a - if the file wasn't present, or no input, exit
2. interactively take a "save path" (from user input) to save to
2a - if there isn't a directory w/ that name, or no input, exit
3. tar the $filename to the $savepath with a $timestamp filename

so far, i have this:

Code:
        
#!/bin/bash
#

##### Constants

FILENAME=
SAVEPATH=
TITLE="Hello $USER! Let's back something up!"
TIMESTAMP="$(date +"%m-%d-%Y")"
DIR_TREE="$(dir)"
SELECTION=
PWD="$(pwd)"


##### Functions

function dir_tree
{
        echo "$TITLE"
        echo ""
        echo " You are here: $PWD"
        echo "These are the Files/Directories in $PWD"
        echo "$DIR_TREE"
}

function press_enter
{
        echo ""
        echo -n "Please press Enter to continue:  "
        read
        clear
}

##### Main

until [ "$SELECTION" = "n" ]; do
 echo ""
        dir_tree
        echo ""
        echo ""
        echo "What would you like to backup? >  "
        read FILENAME
        if [ "$FILENAME" = "\ " ]; then
                exit 1;
        fi
        echo ""
        echo ""
        echo "Where would you like the new .tar.gz archive for $FILENAME stored? (just specify directory, filename is automatic) >  "
        read SAVEPATH
        if [ "$SAVEPATH" = "\ " ]; then
                exit 1;
        fi
        echo
        echo "O.K. - so you want to archive $FILENAME  to $SAVEPATH ? (y for yes, n to quit) >  "
        read SELECTION
        case $SELECTION in
                y ) tar -cjvf "$SAVEPATH$TIMESTAMP-of-$FILENAME.tar.gz" $FILENAME; exit;;
                n ) exit;;
                * ) echo "Invalid character... Please type in y or n to continue"; press_enter;;
        esac

done

for reasons unknown (to me Image ) the "program" won't exit if no input is selected, and I haven't figured out how to do it if the file/directory doesn't exist.

any help folks?

edit -

i have just figured out that using
Code:
if [ -z $FILENAME ]; then
    echo "No filename specified. Exiting..."; exit 1;

will work for what i wanted. now, how do i make an "overwrite" option if the $SAVEPATH specified already exists? as it is now, I'm assuming that I would have to use find (and put the two variables together, ie find -something -something $SAVEPATH$FILENAME)

Last edited by jmd9qs; 03-03-2009 at 03:15 PM.. Reason: correction based on new inpute
# 2  
Old 03-03-2009
Quote:
Originally Posted by jmd9qs
Code:
        
#!/bin/bash
#

##### Constants

FILENAME=
SAVEPATH=
TITLE="Hello $USER! Let's back something up!"
TIMESTAMP="$(date +"%m-%d-%Y")"
DIR_TREE="$(dir)"
SELECTION=
PWD="$(pwd)"


That is unnecessary; $PWD is always set to the current directory.
Quote:
Code:
##### Functions

function dir_tree


The standard syntax for defining functions is:

Code:
dir_tree()
{
  : ...
}

Quote:
{
echo "$TITLE"
echo ""
echo " You are here: $PWD"
echo "These are the Files/Directories in $PWD"
echo "$DIR_TREE"
}

function press_enter
{
echo ""
echo -n "Please press Enter to continue: "
read
clear
}

##### Main

until [ "$SELECTION" = "n" ]; do
echo ""
dir_tree
echo ""
echo ""
echo "What would you like to backup? > "
read FILENAME
if [ "$FILENAME" = "\ " ]; then[/code]

You are comparing the input to a space, not to an empty string. Use:

Code:
if [ "$FILENAME" = "" ]

Or:

Code:
if [ -z "$FILENAME" ]

Quote:
I haven't figured out how to do it if the file/directory doesn't exist.

To check for a file:

Code:
if [ -f "$FILE" ]

To check for a directory:

Code:
if [ -d "$DIRECTORY" ]

# 3  
Old 03-03-2009
Quote:
Originally Posted by cfajohnson

That is unnecessary; $PWD is always set to the current directory.


I realize that... however, I'm learning how to write scripts. I like to implement what I already know into them as much as possible. think of it as practice...

Quote:

The standard syntax for defining functions is:

Code:
dir_tree()
{
  : ...
}

so, by this, do you mean i should:

Code:
dir_tree()
{
   : echo ""
   blah blah blah
}

or is the : just to show that input goes here?

and thanks for the help!
# 4  
Old 03-03-2009
Quote:
Originally Posted by jmd9qs
[INDENT]

I realize that... however, I'm learning how to write scripts. I like to implement what I already know into them as much as possible. think of it as practice...

You don't learn by doing unnecessary things. Knowing what is necessary is part of learning.
Quote:

so, by this, do you mean i should:

Code:
dir_tree()
{
   : echo ""
   blah blah blah
}

or is the : just to show that input goes here?

That's where your function goes. Using your code, it would be:

Code:
dir_tree()
{
        echo "$TITLE"
        echo ""
        echo " You are here: $PWD"
        echo "These are the Files/Directories in $PWD"
        echo "$DIR_TREE"
}

# 5  
Old 03-03-2009
Quote:
Originally Posted by cfajohnson

You don't learn by doing unnecessary things. Knowing what is necessary is part of learning.


good point... i'll take that into consideration next time.

Quote:
That's where your function goes. Using your code, it would be:

Code:
dir_tree()
{
        echo "$TITLE"
        echo ""
        echo " You are here: $PWD"
        echo "These are the Files/Directories in $PWD"
        echo "$DIR_TREE"
}

excellent... i'm pretty sure i coulda figured that one out, but thanks for taking the time to let me know.

now, on to my earlier question:

how do i make an "overwrite" option if the $SAVEPATH specified already exists? as it is now, I'm assuming that I would have to use find (and put the two variables together, ie find -something -something $SAVEPATH$FILENAME) but i'm not too sure of how to go about this.
# 6  
Old 03-03-2009
and another quick question:

will

Code:
if [ -d $DIRECTORY ];

look in only the current directory? or does it span the whole system? how does this work?
# 7  
Old 03-03-2009
No, it is not recursive
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Newbie needs help with some bash scripting

Please bear with me, I'm a beginner but have had some experience and decent knowledge to understand things as I read them and I'm in the process of trying to learn more. I recently inherited a UNIX server which has a bash script which is part of a crontab schedule that needs to be modified (or... (3 Replies)
Discussion started by: Danylko
3 Replies

2. Shell Programming and Scripting

Bash scripting - Newbie

Hi all, I have drill to do and I'll very appreciate your help: Please create a simple CSV file as follow (3 columns and 4 rows): A,B,C A,”B,C”,D “A,B”,C,D o A,B,”C,D” - Please refer to the comma between the quotation marks as a parameter and not as a separator. - Please provide... (3 Replies)
Discussion started by: elior
3 Replies

3. Shell Programming and Scripting

sed newbie scripting assistance

Howdy folks, I'm trying to craft a log file summarisation tool for an application that creates a lot of duplicate entries with only a different suffix to indicate point of execution. I thought I'd gotten close but I'm clearly missing something. Here's a genericized version: A text_file... (3 Replies)
Discussion started by: mthespian
3 Replies

4. Shell Programming and Scripting

Shell Scripting Newbie

Hi Guys, I want to create a shell script to run multiple jobs in sequence. Explaination - If I were to run each jobs individually I would have gone to folder - "abin"(where my shellscript is place) as follows cd abin abin > runappeng.sh abc001 Now, I have list of programs which are like... (8 Replies)
Discussion started by: chaits84
8 Replies

5. Shell Programming and Scripting

Scripting needed for newbie

Hi, I am newbie in shell scripting I have a file name like simple.txt which comes from Mainframe systems onto windows dir every 15 minutes daily. File name is same. Every 15 minutes it updates. I need to write shell script to check if the file arrived every 15 min or not. If the new file... (4 Replies)
Discussion started by: chinniforu2003
4 Replies

6. Shell Programming and Scripting

Shell Scripting NEWBIE - Need Help

Could someone please recommend a very good shell scripting book for me. I would be starting a new job that would require a very good understanding of shell scripting. Please help. (3 Replies)
Discussion started by: ayoka
3 Replies

7. Shell Programming and Scripting

scripting newbie needs help

I have written a script that will email a generic user when a device is "offline". I would like to enhance this by having the script lookup a contact's email and automatically add it to the MAIL_LIST. I am trying to lookup and return data based on a field common in two files File 1 ... (0 Replies)
Discussion started by: irishluck66
0 Replies

8. Shell Programming and Scripting

Scripting Newbie

Seems simple but I am having difficulty with this one: I am trying to write a single command line argument (which will be a path) - the program should print out the owner of the path. I can not get anything I write to run. Please help. (5 Replies)
Discussion started by: Kymmers7
5 Replies

9. UNIX for Dummies Questions & Answers

Shell Scripting Newbie

I'm relatively new at this scripting game, just need to learn some basic stuff for data handling. My current need is to write a script that loops through a textfile of filenames, and for each file removes the first line and re-writes that file to a new name. In fact I could do with knowing... (1 Reply)
Discussion started by: mattyjim2
1 Replies

10. Shell Programming and Scripting

Newbie - Need help in bash scripting

Hi there, I am a student and currently working on a project. I have a file that contains about 50 filenames. (1.txt, 2.txt, 3.txt ...). I would like to know how can I store these filenames into a variable using a loop? I would appreciate if anyone can help me. Thank You. Regards, Bib (4 Replies)
Discussion started by: coolbib
4 Replies
Login or Register to Ask a Question