[wplug] Bash subshell return value

Bob Schmertz rschmertz at speakeasy.net
Mon Aug 25 11:49:06 EDT 2003


Gary Morrow incurred the wrath of Bob on Aug 25, by saying

>I'm not a Bourne shell expert (I would have done it in Perl) but I see 
>some problems:
>
>Wise, Jeremey wrote:
>
>>I am trying to write a script to run a sync of two server vi scp. (no rsync
>>is not an option as the other server guys will not load rsync:>) I have RTFM
>>but even though I know it is some stupid user error I am not seeing the
>>forest for the trees kind of thing.
>>
>>
>>Here is the basic structure.
>>**************
>>#!/bin/bash
>>#!/bin/bash
>># Variables list
>>Fileuplist=/root/scpcopyup.list
>>Filedownlist=/root/scpcopydown.list
>>FilesSynced=/root/filesynced.list
>>
>># SCP Up to IDX
>>while [ -s $Fileuplist ]
>>        do
>># file ./root/firstline.pl is a perl script which cuts the first line out of
>>a file checks if it is a directory
>># if not directory sends it to standard out. EX: firstline.pl /root/scpcopyup.list -> ./etc/fstab. (first line)     
>>
>>Currentfile=(/root/firstline.pl $Fileuplist)
>>
>It's not parens, it's backtics. You need:
>
>Currentfile=`/root/firstline.pl $Fileuplist`

Since this is bash, he can also use $(/root/firstline.pl $Fileuplist).  
Prehaps he was reading the manual but missed the need for the $.

>
>
>># test of variable output
>>echo $Currentfile
>>        rc=$?
>>
>since $? is the exit status of the previous command (echo $Currentfile) 
>you will always get 0 (success)
>
>>        if [ $rc -ne 0 ] ; then
>>        printf "Error reading file"
>>        exit 50
>>        else
>>        scp user at acme.com:/$Currentfile .
>>
>
>$rc is still the exit value of the echo command (echo $Currentfile). 
> still always 0
>
>>                if [ $rc -ne 0 ] ; then
>>                echo "$Currentfile failed to copy up." >> $FileSynced
>>                exit 51
>>                else
>>                echo "$Currentfile successfully copied." >> $FilesSynced
>>                fi
>>        fi
>>done
>>
>># SCP Down to  ACME.com
>>.....
>>****************
>>The out put is as follows
>>[root at toybox root]# ./test.scp
>>/root/firstline.pl
>>**************
>>Yet, if I run the command directly the correct output is replied:
>>
>>[root at toybox root]# ./firstline.pl /root/scpcopyup.list
>>/etc/lilo.conf.anaconda[root at toybox root]#
>>*****************
>>

-- 
Cheers,
Bob Schmertz




More information about the wplug mailing list