LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help Needed in Korn Shell Script (https://www.linuxquestions.org/questions/programming-9/help-needed-in-korn-shell-script-4175454944/)

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


chrism01 03-21-2013 01:16 AM

Code:

ftpfile=~/netrc

# should be this ?? (extra dot)
ftpfile=~/.netrc

You could use debug (set) cmd at line 2
Code:

#!/usr/bin/ksh
set -xv


linux_puneet_vyas 03-21-2013 01:59 AM

Unable to Place file at destination ftp directory
 
Hello Sir,

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.

Thanks,
Puneet

pan64 03-21-2013 02:46 AM

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)

chrism01 03-21-2013 06:33 AM

In addition to above notes, here are some links to help you learn
http://rute.2038bug.com/index.html.gz - Linux tutorial

If you want to use ksh, see http://www.kornshell.com/

Note that most people on Linux use the bash shell
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Ultimately its your choice.

linux_puneet_vyas 03-22-2013 01:22 AM

Solved
 
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.

bigearsbilly 03-22-2013 04:01 AM

The more discerning use ksh ;)


All times are GMT -5. The time now is 09:34 PM.