LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2013, 12:46 AM   #1
linux_puneet_vyas
LQ Newbie
 
Registered: Mar 2013
Posts: 3

Rep: Reputation: Disabled
Post 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
 
Old 03-21-2013, 01:16 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 03-21-2013, 01:59 AM   #3
linux_puneet_vyas
LQ Newbie
 
Registered: Mar 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Post 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
 
Old 03-21-2013, 02:46 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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)
 
1 members found this post helpful.
Old 03-21-2013, 06:33 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.

Last edited by chrism01; 03-21-2013 at 06:34 AM.
 
Old 03-22-2013, 01:22 AM   #6
linux_puneet_vyas
LQ Newbie
 
Registered: Mar 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Post 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.
 
Old 03-22-2013, 04:01 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
The more discerning use ksh
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
korn shell script aianne Linux - Wireless Networking 2 09-25-2012 01:21 AM
Problem with '.' expansion in korn shell script jcipale Solaris / OpenSolaris 3 08-01-2012 12:47 PM
Korn shell script mmahulo Linux - Newbie 9 12-17-2008 09:19 AM
script in korn shell disruptive Programming 15 02-22-2007 10:18 AM
Korn shell script Muzica Solaris / OpenSolaris 4 09-06-2004 12:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration