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 ]
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 ]