Click to See Complete Forum and Search --> : Shell Script Question


Benny B
06-12-2001, 04:10 AM
Hi,

This is my first attempt at a shell script (first attempt at any sort of programming really) and I've got it working to my liking (almost), what it does is makes a backup of a filename I specify on the command line.
eg:

# bak filename.txt

This then copies the file to "filename.txt.bak" in the same directory

(I know this is a simple cp command, but I wanted to simplify it and try some scripting).

My current code looks like this:


#!/bin/bash
#
#A File BAKup script
#Creates a filename.bak of original file.
#
#Written by Ben Birnbaum


if [ -z $1 ]; then #check that something was entered on the command line
echo ""
echo "Error! No filename specified"
echo "Usage: bak filename"
echo ""
exit 1
else
if [ -d $1 ]; then #check if the file is a directory
echo ""
echo "Error! Specified file is a directory"
echo "Usage: bak filename"
echo ""
else
if [ -e $1 -a -r $1 ]; then #check that the file exists and is readable
file=$1
cp $file $file.bak
exit 0
else
echo ""
echo "Error! File doesn't exist or isn't readable"
echo "Usage: bak filename"
echo ""
exit 1
fi
fi
fi


I want to now be able to have multiple filenames on the command prompt, eg:

# bak file.1 file.2 file.3

and it makes a backup of each of these files. How would I go about doing that?

Thanks

[ 12 June 2001: Message edited by: Bomber_007 ]

Salmon
06-12-2001, 09:23 AM
Try this to loop through any number of arguments provided.


for file in "$@"; do

# place your file tests and copying here
# name of the file is now in $file

done


Hope that helps.

Strike
06-12-2001, 12:20 PM
Also, you might want to put the arguments to cp in quotation marks to allow for spaces in the filename, like

cp "$file" "$file.bak"

Benny B
06-13-2001, 12:16 AM
Thanks for the help guys.

I am still a little confused about the loop that Salmon mentioned, can someone clarify further please?

Also just a general question, in this instance:

# bak file.1

In the shell script file.1 is refered to as $1.

Does that mean in this instance:

# bak file.1 file.2 file.3

The shell script refers to file.2 and file.3 as $2 and $3 ?


Thanks heaps for the help. :)

Craig McPherson
06-13-2001, 01:20 AM
The first commandline argument is $1, the second is $2, the third is $3, and so forth. However, you can use a for loop with $@ as described in the example to process any number of arguments.

Benny B
06-13-2001, 03:47 AM
Um, could someone go over that for loop ...

I did this:


#!/bin/bash
#
#A File BAKup script
#Creates a filename.bak of original file.
#
#Written by Ben Birnbaum


for file in "$@"; do

if [ -z $file ]; then #check that something was entered on the command line
echo ""
echo "Error! No filename specified"
echo "Usage: bak filename"
echo ""
exit 1
else
if [ -d $file ]; then #check if the file is a directory
echo ""
echo "Error! Specified file is a directory"
echo "Usage: bak filename"
echo ""
else
if [ -e $file -a -r $file ]; then #check that the file exists and is readable
# file=$1
cp "$file" "$file.bak"
exit 0
else
echo ""
echo "Error! File doesn't exist or isn't readable"
echo "Usage: bak filename"
echo ""
exit 1
fi
fi
fi
done


But that doesn't work ....what am I missing?

Benny B
06-13-2001, 04:14 AM
DUH! :rolleyes:

I fixed it ..... forgot I had the exit command after the cp command.

Works like a charm!

Thanks for the help...

Salmon
06-13-2001, 09:43 AM
Right, what Craig said. $@ contains all the command line arguments (except $0 of course). By using the for loop that I mentioned each agrument is processed, with the value of the argument for that iteration being stored in the variable $file.

So if you wanted to echo all the arguments, each on a single line . . .

for file in $@; do
echo $file
done