Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Please enlighten me. What does the following snippet of a shell script mean?


touch Apple.txt
   ls Banana.txt | while read FNAME
    /somePath/ someFolder/LG $FNAME Apple.txt

EDIT: transfers files to a server.

Content of

SO=`tput smso`
SE=`tput rmso`

TEMP_FN=`mktemp -c -d . -p ftp`
TEMP_LOG=`mktemp -c -d . -p log`

echo open>>$TEMP_FN
echo user user pass>>$TEMP_FN
echo cd \"$1\">>$TEMP_FN
echo binary>>$TEMP_FN

until [ $# -eq 0 ]
   echo mput \"$1\">>$TEMP_FN

echo bye>>$TEMP_FN

ftp -vin < $TEMP_FN > $TEMP_LOG

FILE_COUNT=`grep "226 Transfer complete" $TEMP_LOG|eval wc -l|cut -d ' ' -f 1`

if [ $FILE_COUNT -eq 0 ]
   rm -f ftp_commands.txt 2>/dev/null
   rm -f ftp.log 2>/dev/null
   mv $TEMP_FN ftp_commands.txt
   mv $TEMP_LOG ftp.log
   cat ftp.log
   banner "WARNING" "FTP FAILED"
   rm -f $TEMP_FN
   rm -f $TEMP_LOG
   echo $FILE_COUNT Files sucessfully transferred.

Thank you very much.

share|improve this question
It would be useful to know what contains. –  KTamas Jun 11 '09 at 9:24
I suppose you changed all file names? –  splattne Jun 11 '09 at 9:27
@Enty: Edited. It transfers files to a server. @Splattne: Doesn't matter I guess. –  Amanda Jun 11 '09 at 11:19

4 Answers 4

In simple format....

SETS FNAME to Grapes.txt

creates Apple.txt

then does someFolder /LG Grapes.txt Apple.txt


Not much more than that....

share|improve this answer
How about this loop- ls Banana.txt | while read FNAME? And do..done? Thanks. –  Amanda Jun 11 '09 at 11:20
ls Banana.txt will output nothing if Banana.txt does not exist, and therefore read FNAME will exit with an error and while will not run the do/done loop. If Banana.txt does exist, the ls command will output "Banana.txt" which will be fed into read FNAME which will set FNAME to "Banana.txt" and return 0 which will trigger while to run the do/done block. This is basically a strange way to write the following if statement: if [ -e Banana.txt ]; then /somePath/ someFolder/LG Banana.txt Apple.txt; fi. –  daveadams Feb 9 '11 at 15:41
  • It updates the timestamp on Apple.txt (or creates it if it don't exist)
  • If Banana.txt exists and is a file, the while loop will be run once with Banana.txt in place of $FNAME
  • If Banana.txt exists and is a directory, for each item in Banana.txt/ the statement will be run with the filename of the items in place of $FNAME
share|improve this answer

My blind guess is that it is backing up some files to an FTP server. There are some "magic" things we can't know about that script. It seems that the "Apples.txt" file is some kind of log.

The biggest mystery are the content and parameters of the script. Maybe you could add some information tio your question.

share|improve this answer

As an aside, I do not think that is as written as well as it could be, see this for why. Consider the following instead:

 while read FNAME
    /somePath/ someFolder/LG "$FNAME" Apple.txt
 done < Banana.txt
share|improve this answer
But that's how it has been done for years.. Working fine.. We are converting this in Java.. –  Amanda Jun 11 '09 at 12:17
This statement is not equivalent. It will read lines from Banana.txt and use them as FNAME in the loop. The original script pipes the output of ls Banana.txt (which will be "Banana.txt" if Banana.txt exists and empty if it does not) to the while read loop, turning it into basically an if statement, equivalent to if [ -e Banana.txt ]; then ...; fi. –  daveadams Feb 9 '11 at 15:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.