1834408 Members
1454 Online
110067 Solutions
New Discussion

Help on Script needed

 
Rudi Martin
Advisor

Help on Script needed

Hi all

Below is a sample of the script I'm working on. I cannot remember how to do the "no error" bit. Please help.

rcp -p /sdata/files/cptester/r1.txt a646001:/home/amartir/main_unix.txt
IF no error THEN
mailx -s"Copy of Main_Unix to DSA" elit
rm /sdata/files/cptester/r1.txt;;
rcp -p /sdata/files/cptester/r2.txt a646001:/home/amartir/sub_unix.txt
IF no error THEN
mailx -s"Copy of Sub_Unix to DSA" elit
rm /sdata/files/cptester/r2.txt;;
8 REPLIES 8
Borislav Perkov
Respected Contributor

Re: Help on Script needed

Hi,
You can try with:

if [ $? -eq 0 ]

$? is exit error code, and if it is 0 the command properly.

Boris.
Sanjay Kumar Suri
Honored Contributor

Re: Help on Script needed

if [ $? -eq 0 ]
then
command
fi

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Graham Cameron_1
Honored Contributor

Re: Help on Script needed

or you can use the double pipe "||" to specify an action to take if the operation fails, eg:
rcp -p /sdata/files/cptester/r1.txt a646001:/home/amartir/main_unix.txt || return 1

mailx -s"Copy of Main_Unix to DSA"

etc

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Nicolas Dumeige
Esteemed Contributor

Re: Help on Script needed

Beware that r services connot be trusted as for the return code.
rcp return code should be ok, but if're doing rsh, a good idea is to make the remote script display a final OK and the local caller grep this output.

For the test :
if [ $? -eq 0 ] ; then
command
else
other_command
fi

is equivalent to
[ $? -eq 0 ] && command || other_command
All different, all Unix
Borislav Perkov
Respected Contributor

Re: Help on Script needed

oops,
Sorry
$? is exit error code, and if it is 0 the command properly ENDED.

Boris
Elmar P. Kolkman
Honored Contributor

Re: Help on Script needed

I would do this by using not the || but the &&...

rcp -p /sdata/files/cptester/r1.txt a646001:/home/amartir/main_unix.txt && \
mailx -s"Copy of Main_Unix to DSA" elit && \
rm /sdata/files/cptester/r1.txt

The same for the other command.

Or the way mentioned above, using the return code in $? which is 0 (zero) for succesfull commands. You could even do it this way:

if rcp ...
then
mailx ...
rm ...
fi

Every problem has at least one solution. Only some solutions are harder to find.
Mark Grant
Honored Contributor

Re: Help on Script needed

The simplest way is to use the "&&" operator which will only allow the things on the right of it to run if the thing on the left is succesful.

rcp -p /sdata/files/cptester/r1.txt a646001:/home/amartir/main_unix.txt && mycommand

If "mycommand" needs to be several commands you can put them in braces as in

rcp -p /sdata/files/cptester/r1.txt a646001:/home/amartir/main_unix.txt && {
mailx .....
rm ....
etc ...
}

The opposite of && is ||
Never preceed any demonstration with anything more predictive than "watch this"
rvrameshbabu
Advisor

Re: Help on Script needed

Note that the remote shell and remote copy commands cannot be trusted for return code

#!/bin/sh
HOST=a646001
rcp -p /sdata/files/cptester/r1.txt $HOST:/home/amartir/main_unix.txt
OK=`remsh $HOST ls /home/amartir/main_unix.txt | wc -l`
if [ "$OK" -eq 1 ]
then
# ur commands
elif
# ur commands
fi

So try the above and make sure copy is done and use the number of files copied as input to ur if statement