1847301 Members
2209 Online
110264 Solutions
New Discussion

Simple Script Help

 
SOLVED
Go to solution
Rob Smith
Respected Contributor

Simple Script Help

Hello All,

9 Months of working on WIN2K boxes has left my scripting skills rusty, which were never good to begin with but anyway.. This is a simple problem but I cannot find the answer. Any help would be appreciated.

su - dmadmin -c dba/stop.all

sleep 45

if [[ $? = 0 ]]

I do not want to test the return code of the sleep command at this point. I want to test the code of the previous line. I think it may be $! but I am not sure.


Learn the rules so you can break them properly.
4 REPLIES 4
Ian Dennison_1
Honored Contributor
Solution

Re: Simple Script Help

2 points of interest,...

1. Return Code Saving
su - dbadmin -c 'cmd'
export RC=$?

sleep 45

if (( $RC != 0 ))
then

fi

This will grab the return code for you and save it in $RC.

2. What return code
The above script will only capture the result of the 'su' command, NOT the result of the 'cmd' command. This is a problem that has dogged sysadmins for yonks. I get around this by putting the following inside the command 'cmd; print "RTN "$?'

What U then do is capture the output, grep for keyword 'RTN' and cut out the second field (cut -d' ' -f2). This will actually capture the result of the 'cmd' for future analysis.

Share and Enjoy! Ian
Building a dumber user
Sridhar Bhaskarla
Honored Contributor

Re: Simple Script Help

Rob,

There is no guarantee that you will get a return code of "1" if one of the processes in your "stop.all" fails as $? will return 0 as su completes successfully irrespective of the execution unless you explicitly return a different number in your stop.all script.

Your best bet may be to check if all the processes are stopped like

num_of_procs=`ps -ef|grep dmadmin|grep -v grep|wc -l`
if [ $num_of_procs != 0 ]
then
kill_something
fi

The above is only one way. There are thousand other ways of doing it.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
V. V. Ravi Kumar_1
Respected Contributor

Re: Simple Script Help

hi,
i don't know how to get the previous line code, but hope this may help u.


if [ $? -eq 0 ]
then
x=ok
else
x=not
fi
sleep 10
if [ "$x" = "ok" ]
then

regds
ravi
Never Say No
MANOJ SRIVASTAVA
Honored Contributor

Re: Simple Script Help

Hi Rob


Then you will ahve to put the test condition in a loop , first run the command then check for the variable and put a test condition and the the commnad then sleep and put this all in a loop .

Like

su - dmadmin -c dba/stop.all
while true
do

export A=`ps -aef | gerp stop.all |wc -l `
if [ $A = 0 ]

then

command

exit

fi

sleep 45

done


may be this will help you


Manoj Srivastava