linux_puneet_vyas |
03-21-2013 12:46 AM |
Help Needed in Korn Shell Script
Hello Experts,
I need inputs on a korn shell script which simply zip a file and ftp to some destination.the command line arguments provided to it are:
param1 -o
param2 Auto_Bill_History.zip
param3 /tmp/sddss
I need to change the destination directory where the zip file needs to be placed.i connected via file zila and using windows command prompt to see does cd /KBS works or not,it works there,so i added the same statement in korn shell script but when i ran it gave me error in log
550 '/KBS/Auto_Bill_History.zip': The system cannot find the path specified.
even after that i have hard coded the path in command line argument inside script as $/KBS/$outfile,but still the same error.
Please guide me as i am new to writing ftp script on korn shell how can i place the zip file in KBS directory at destination file.
Code:
#!/usr/bin/ksh
##########################################################################
# undss.pkzip
# This script is used to zip a file and then ftp it to some destination
##########################################################################
#
#
##########################################################################
# functions #
#############
#
# This function ensures that the netrc lockfile is deleted
#
function cleanup {
typeset exitcode # make exitcode a local variable
exitcode=$1
if [[ -a ~/.netrc ]]
then
print "Saving .netrc to .netrc.last.used ."
mv -f ~/.netrc ~/.netrc.last.used
else
print ".netrc was not created."
fi
if [[ -a ~/.netrc.saved.$timestmp ]]
then
print "Replacing the saved .netrc file."
mv -f ~/.netrc.saved.$timestmp ~/.netrc
chmod 600 ~/.netrc
else
print "No saved .netrc to restore."
fi
if [[ -a ~/.netrc.lockfile ]]
then
rm -f ~/.netrc.lockfile
print "Lockfile removed."
else
print "No lockfile to remove."
fi
exit $exitcode
}
#
# function to make sure ftp ends within a certain time period
#
function ftpends {
typeset waitcnt # make waitcnt a local variable
typeset secslice # .... also secslice
((waitcnt=$1)) # 1st parm is minutes to wait
procid=$2 # 2nd parm is procid of ftp child process
if [[ `ps -p $procid | grep -c $procid` > 0 ]]
then
print "Waiting up to $waitcnt minutes for process $procid to complete."
fi
while ((waitcnt>0)) && [[ `ps -p $procid | grep -c $procid` > 0 ]]
do
((secslice=12))
while ((secslice>0)) && [[ `ps -p $procid | grep -c $procid` > 0 ]]
do
sleep 5
((secslice-=1))
done
if [[ `ps -p $procid | grep -c $procid` > 0 ]]
then
((waitcnt-=1))
if ((waitcnt>0))
then
print "$waitcnt minutes left"
else
print "Time is up!"
fi
fi
done
if [[ `ps -p $procid | grep -c $procid` > 0 ]]
then
return 1
else
return 0
fi
}
################################################################
####################### MAINLINE ###############################
################################################################
timestmp=`date +"%Y%m%d%H%M%S"`
#
# lets get some options and make sure they are correct
#
while getopts ":o:d" opt; do
case $opt in
o ) outfile=$OPTARG ;;
d ) debug="-vd" ;;
* ) print "Invalid switch, $OPTARG"
exit 16 ;;
esac
done
shift $(($OPTIND - 1)) # the only option left is our input file
infile=$1
if [[ ! -f $infile ]]
then
print "The file, $infile, does not exist or is not sendable"
exit 16
fi
debug=${debug:="-v"}
#
# get rid of old zip file, if any
#
if [[ -f /tmp/dssautobill.zip ]]
then
print "Removing /tmp/dssautobill.zip"
rm -f /tmp/dssautobill.zip
fi
#
# zip the file (MAX says to maximize the compression ratio, but it's slower)
#
#print "Zipping $infile"
#pkzip -add -max /tmp/dssautobill.zip $infile
#
# begin the FTP of the DSS 1-time (1-time job is ran twice a year)
#
while [[ -a ~/.netrc.lockfile ]]
do
print "Lock file exists, waiting 5 seconds."
sleep 5
done
#touch ~/.netrc.lockfile # make the create of netrc uninterruptable
print "$$ 20 `basename $0`" > ~/.netrc.lockfile # data used by unsrdez only
print "Lockfile created."
trap "cleanup 16" INT TERM KILL
print "Beginning FTP of DSS file.\n"
if [[ -a ~/.netrc ]]
then
mv -f ~/.netrc ~/.netrc.saved.$timestmp
fi
ftpfile=~/netrc
if [[ -a $ftpfile ]]
then
rm -f $ftpfile
fi
# change dest= below to IP address of ftp drive shared drive (10.32.944.56)
#test destination location with full domain name
dest=abc.test.org
print "machine $dest" > $ftpfile
# change ID below to ID we will use to FTP to JAXNTDFS01 shared drive (ftpkbs)
print "login ftpkbs" >> $ftpfile
# change ID below to ID we will use to FTP and create file on Unix that will hold the password
password=$(</appl/e000/kbs/scripts/ftpkbs1.password)
print "password $password" >> $ftpfile
print "macdef init" >> $ftpfile
print "binary" >> $ftpfile
#even after changing the below put command to '/KBS/$outfile' result was same.
print "put /tmp/sddss '$outfile'" >> $ftpfile
print "bye" >> $ftpfile
print "" >> $ftpfile
mv -f $ftpfile ~/.netrc
chmod 600 ~/.netrc
let success=0
let tries=1
let maxatt=2
timeout=20 # wait 10 minutes for ftp to complete
logfile=/appl/e000/kbs/logs/ftp.logfile.$timestamp
while (( $tries <= $maxatt )) && (( $success <= 0 ))
do
print "*** in FTP loop"
print "***working directory before cd command is $PWD"
cd /KBS
print "*** working directory changed to $PWD after cd /KBS command"
ftp $debug $dest >> $logfile &
procid=$!
print "*** FTP issued, procid = $procid"
if ftpends $timeout $procid
then
print "FTP attempt # $tries completed, checking for completion code."
else
print "FTP attempt # $tries exceeded run time, validating completion code."
kill $procid
fi
cat $logfile
success=`egrep -c "^(226|250)" $logfile`
if (( $success > 0 ))
then
print "FTP attempt # $tries was successful."
else
if (( $tries < $maxatt ))
then
print "FTP attempt # $tries failed, will try again."
else
print "Last FTP attempt has failed after $tries attempts."
print "Please notify Unix SE on-call tech."
fi
((tries+=1))
fi
done
print "*** you should have done the FTP by now"
if (( $tries > $maxatt ))
then
cleanup 16
else
cleanup 0
fi
|