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

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:
        # 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunhuxAuthor Commented:

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

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

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

sunhuxAuthor Commented:

Hurray, both yr suggestions works
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

sunhuxAuthor Commented:

> 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"
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).
sunhuxAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.