Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-10-04
6
Medium Priority
?
1,366 Views
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 scr1.sh will call scr2.sh

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 scr1.sh " :

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
/appl/career/dataload/in/.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
sftpVarnullScr3.txt
0
Comment
Question by:sunhux
  • 4
  • 2
6 Comments
 
LVL 20

Accepted Solution

by:
simon3270 earned 2000 total points
ID: 36915004
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
     do
        # 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
    done

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 scr2.sh may have spaces in it.  You should put double-quotes round $3 in the line
    FILE_LIST="$3"
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
line.
0
 

Author Comment

by:sunhux
ID: 36915208

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

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

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


0
 

Author Comment

by:sunhux
ID: 36915222

Hurray, both yr suggestions works
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sunhux
ID: 36915348

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

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

Assisted Solution

by:simon3270
simon3270 earned 2000 total points
ID: 36916345
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
and
  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).
0
 

Author Closing Comment

by:sunhux
ID: 36916521
excellent
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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.:
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

571 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