value of variables in Shell script became null after sftp batch command : Urgent

Posted on 2011-10-04
Last Modified: 2012-05-12

I'm writing sftp Shell scripts (in -b batch mode) but seems like after
sftp codes, the value of the variables (in particular FILE_NAME )
was reset to null (or empty).

Attached the scripts, where will call

I've tried to export the variables (as shown in the script) but no joy.
I don't have this problem previously when this script was using 'ftp'
(but I'm now required to convert it to sftp)

The outputs I got after running "ksh -v " :

Couldn't stat remote file: No such file or directory
Couldn't delete file: No such file or directory
Invalid command.
Couldn't stat remote file: No such file or directory
Couldn't delete file: No such file or directory
-rw-------  1 root root 34 Oct  5 11:56 /appl/career/dataload/in/sftp_xxo_acknowledge.dat.md5
dos2unix: converting file /appl/career/dataload/in/.md5 to UNIX format ... <==$FILE_NAME is null !
dos2unix: problems converting file /appl/career/dataload/in/.md5            <==$FILE_NAME is null !

I'm on RHES 4.6
Question by:sunhux
    LVL 19

    Accepted Solution

    I can see a couple of problems with your script.

    One is that you have "ls -l" and "dos2unix" commands within the first "for FILENAME" loop.  That loop is simply generating a command list for the sftp call - it isn't actually getting the files.  You have only retrieved the files after the "sftp" line is executed, so outside that "for FILENAME" loop.  The fix for this is to remove the "dos2unix" etc lines from within the "for FILENAME" loop, and just do them later (in the bit of code with the "problem in next few lines" comment)

    The second is that $FILE_NAME is defined in a subshell (because of the way you pipe the output for the "for FILENAME" loop).  Any variables set in a subshell are not passed back to the parent shell.  I assume that in the "ftp" version of the script, ftp was called directly from within the "for FILENAME" loop, so was executed in the same shell as the later dos2unix lines.

    One fix for this second problem is to repeat the "for FILENAME" loop, so that you have:
         for FILENAME in $FILE_LIST
            # problem in next few lines below
            # dos2unix $LOCAL_DIR/sftp_xxo_acknowledge.dat.md5
            ls -l $LOCAL_DIR/sftp_xxo_acknowledge.dat.md5
            echo "BBBBBBBB"
            echo $LOCAL_DIR/$FILE_NAME.md5
            dos2unix $LOCAL_DIR/$FILE_NAME.md5
            ping -c 7 localhost > /dev/null  # to pause for 7 secs

    Open in new window

    I'd make one other suggestion - you code looks as though it is designed to process multiple files (otherwise you wouldn't need the "for FILENAME" loops at all.  This means that $3 passed into may have spaces in it.  You should put double-quotes round $3 in the line
    This applies almost anywhere that you might have spaces in a variable, unless you *want* the spaces to split the variable up into separate parts (so you *don'* put double-quotes in the
        for FILENAME in $FILE_LIST

    Author Comment


    Thanks, will try that in a moment when I reached office.

    Btw, is the syntax   echo "-quit"  Ok?

      echo "-quit"
      } sftp -b ...


    Author Comment


    Hurray, both yr suggestions works

    Author Comment


    > is designed to process multiple files (otherwise you wouldn't need the "for FILENAME"
    >  loops at all.  This means that $3 passed into may have spaces in it

    Yes, that's right; it may have multiple files separated by a space between the files.
    But I thought already has double quotes when passing to scr2,sh ie
      "sftp_xxo_acknowledge1.dat sftp_xxo_acknowledge2.dat"
    LVL 19

    Assisted Solution

    You are right, the double-quote effect does seem to persist.

    As for the echo line, the quotes round "-quit" don't help, and aren't required.  If the first parameters of echo start with a -, and are a valid single-letter option for echo, then they are treated as options whether or not they are in quotes (the shell strips the quotes before it passes the parameter to echo).  So
      echo -n fred
      echo "-n" fred
    echoes fred without a trailing newline, but
      echo "-n fred"
    echoes -n fred, with a trailing newline (the spce and the fred are treated as part of the first parameter, so it is no longer a single-letter option).

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap ( Version 1.2 2.      Jpcap( Version 0.6 Prerequisite: 1.      GCC …
    About FreeBSD Jails In FreeBSD, jails are a way of doing operating system level virtualization.  The basis of FreeBSD jails is chroot (, which changes the root directory of processes.  As a…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Video by: Phil
    This video goes over how to configure and start a jail in FreeBSD.  This video is meant to supplement the article included with this course.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now