Script to check one command and if it fails moves to other command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to check one command and if it fails moves to other command
# 1  
Old 05-21-2013
Script to check one command and if it fails moves to other command

Input is list of Server's, script is basically to remove old_rootvg, So it should check first command "alt_rootvg_op -X old_rootvg" if it passes move to next server and starts check and if it fails moves to other command "exportvg old_rootvg" for only that particular server. I came up with below, but its running both the commands on all the server's, which i dont want to.

Code:
#!/usr/bin/ksh
# set -x
ofile=/old_rootvg_cleanup_logs/old_rootvg-cleanup.$$
for server in `cat host_list.txt`
do
remove_old_rootvg=`ssh $server alt_rootvg_op -X old_rootvg`
if [ $? != 0 ]
then
echo "Command Failed at $server"
else 
export_old_rootvg=`ssh $server exportvg old_rootvg"
fi
echo " $server   |   $remove_old_rootvg  |    $export_old_rootvg" >> $ofile
done

---------- Post updated at 06:56 PM ---------- Previous update was at 06:37 PM ----------
# 2  
Old 05-21-2013
remove_old_rootvg=`ssh $server alt_rootvg_op -X old_rootvg`
echo $?

Does this always return 0 as status($?)?
If so, Is the remote script exiting with a 0 if successful(i.e. exit 0) and non zero if unsuccessful(i.e. exit 1)?
# 3  
Old 05-21-2013
it returns exit 1 as when it deletes using command "alt_rootvg_op -X old_rootvg" will return exit 0.

Is the remote script exiting with a 0 if successful(i.e. exit 0) and non zero if unsuccessful(i.e. exit 1)? YES
# 4  
Old 05-21-2013
Try it with the command in quotes, like this:
Code:
remove_old_rootvg=`ssh $server 'alt_rootvg_op -X old_rootvg'`

# 5  
Old 05-21-2013
Input is list of Server's, script is basically to remove old_rootvg, So it should check first command "alt_rootvg_op -X old_rootvg" if it passes move to next server and starts check and if it fails moves to other command "exportvg old_rootvg" for only that particular server. I came up with below, but its running both the commands on all the server's, which i dont want to.
# 6  
Old 05-22-2013
I did another test and this should work for what you want, notice the single quotes around the script and it's parameters being run on the remote server(s):
Code:
#!/usr/bin/ksh
# Script:  test.sh
for server in `cat host_list.txt`
do
  remove_old_rootvg=`ssh $server 'alt_rootvg_op -X old_rootvg'`
  rc="$?"
  echo $rc
  if [[ $rc = 1 ]] then
    echo "Command Failed at $server"
  else 
    export_old_rootvg=`ssh $server 'exportvg old_rootvg'`
    rc="$?"
    echo $rc
    if [[ $rc = 1 ]] then
      echo "exportvg Failed at $server"
    fi
  fi
done

# 7  
Old 05-24-2013
let me say the sequence of checks and script checks

Firstly in "lsvg | grep old" here if the output is "old_rootvg", script would then delete it by "alt_rootvg_op -X old_rootvg", and check again "lsvg | grep old" if no output then "old_rootvg" is deleted, if output is shown as "old_rootvg" existing, then script would "exportvg old_rootvg".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to check a command executed sucessfully or not

Hi All, I am trying to write a shell script to check if a command executed successfully or not in rhel 7 and finding the installed tomcat version. I am using below script. var4=$(find / -name "catalina.jar" ! -size 0 |egrep -v... (6 Replies)
Discussion started by: sravani25
6 Replies

2. Shell Programming and Scripting

Replace string works on command-line but fails when run from shell script

I wish to replace "\\n" with a single white space. The below does the job on command-line: $ echo '/fin/app/scripts\\n/fin/app/01/sql' | sed -e 's#\\\\n# #g'; /fin/app/scripts /fin/app/01/sql However, when i have the same code to a shell script it is not able to get me the same output:... (8 Replies)
Discussion started by: mohtashims
8 Replies

3. Cybersecurity

Use command 2 incase command 1 fails

I try to fire grep command on remote servers using ssh like below: sshpass -p mypassword ssh -t user1@mach2 "grep -e word1 -e word2 /var/out.txt" The issue is that i wish to run the same grep command which searches for multiple strings in a file on remote servers with different Operating... (6 Replies)
Discussion started by: mohtashims
6 Replies

4. UNIX for Beginners Questions & Answers

Linux shell | how to exit a script if any command fails.

Hi, i am new here let me say HI for all. now i have a question please: i am sending one command to my machine to create 3 names. if one of the names exists then the box return error message that already have the name but will continue to create the rests. How i can break the command and... (7 Replies)
Discussion started by: Amiri
7 Replies

5. Homework & Coursework Questions

Script performs the right task but fails against check

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a script checkFiles.sh that takes an arbitrary number of file paths from the command line and carries ... (5 Replies)
Discussion started by: Ezraek
5 Replies

6. Shell Programming and Scripting

Command to check only Autosys running jobs with autorep like command

Hi, Is there any specific command to use to check only say Running jobs via autorep or similar command for Autosys? (0 Replies)
Discussion started by: sidnow
0 Replies

7. Shell Programming and Scripting

Except script fails to check file exists or not in remote node

Dear members, The following expect script connects to remote node and check for the file "authorized_keys" in directory /root/.ssh in remote node. However the result is always found even if the file exist or doesn't exist. expect { "$fname" { send_user "found\n" } Any idea what is... (4 Replies)
Discussion started by: Sudhakar333
4 Replies

8. Shell Programming and Scripting

File check script fails for multiple files

I want to check if any file with testing*.txt exists but my script fails if more than 1 file exists. It works fine for a single file if then echo "TEST21" fi -------------- bash: How do I fix this? Thanks Please use code tags next time for your code and data. (8 Replies)
Discussion started by: sumang24
8 Replies

9. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

10. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies
Login or Register to Ask a Question