Saturday, May 19, 2018

Create Numbered backup files from the current working directory given as arguments,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
echo "Create Numbered Backup from files of the current directory given as argument"
if [ $# -eq 0 ] ; then
echo "Usage: sh ./createnumberedbackup.sh filename(s) "
echo " Will make a numbered backup of the files(s) given."
echo " Files must be in the current directory."
exit 255
fi
rc=0 # return code, default is no error
for fn in $*  # for each filename given as arguments

do
if [ ! -f $fn ] ; then # if not found
echo "File $fn not found."
rc=1 # one or more files were not found
else
cnt=1 # file counter
loop1=0 # loop flag
while [ $loop1 -eq 0 ]
do
tmp=bak-$cnt.$fn
if [ ! -f $tmp ] ; then
cp $fn $tmp
echo "File "$tmp" created."
loop1=1
# end the inner loop
else
let cnt++ # try the next one
fi
done
fi
done
exit $rc # exit with return code


Usage:


sh ./createnumberedbackup.sh a1.txt a2.txt

No comments:

Post a Comment