ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Could you please elaborate where i am doing mistake because of which in log i am getting message for cd /KBS,unable to find path,though it is existing on destination ftp server.where in the above script i can add cd /KBS so that zip file being placed on the destination server at the mentioned directory(KBS).
i am new to linux/unix ,kindly suggest what mistake i am making.
this is not the way, you would need to give more info. insert the line set -x at the beginning of the script and run it again and see the output. You will find a lot of information about the execution, probably you can also find what went wrong. (or you can post that output)
by adding cd /KBS to $ftpfile we can make the job done in the above script.
Thanks for your time and help,it helped me a lot in learning and resolving the error.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.