Unable to check if my command succeeded


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to check if my command succeeded
# 1  
Old 12-02-2015
Error Unable to check if my command succeeded

Hi,

Is there a way to check if the openssl part of the below execution returns non zero without freezing the flow of the script ?

Code:
var1=$(echo | openssl s_client -connect $url 2>/dev/null | openssl x509 -noout -issuer -subject -dates)
echo $?

Output: 0

while i was expecting the output to be non zero for failing URLs.

I understand why the output shows 0 ... but what i wish to know is how can i grab the actual return code of openssl in the next line.

I tried something like
Code:
var1=$(echo | openssl s_client -connect $url 2>/dev/null | openssl x509 -noout -issuer -subject -dates)
->
openssl s_client -connect $url 2>/dev/null | openssl x509 -noout -issuer -subject -dates
or
test=$(openssl s_client -connect $url 2>/dev/null | openssl x509 -noout -issuer -subject -dates)

but both the above tricks freezes the flow and the script does not complete the while loop.

Can u help ?
# 2  
Old 12-02-2015
In bash you can query PIPESTATUS:
Code:
var1=$(echo | openssl s_client -connect $url 2>/dev/null | openssl x509 -noout -issuer -subject -dates)
echo ${PIPESTATUS[0]} ${PIPESTATUS[1]} ${PIPESTATUS[2]}

where ${PIPESTATUS[1]} is the second command (i.e. the first openssl).
Note that $? is the status of the last command.
For other/all shells you can do
Code:
var1=$(echo | openssl s_client -connect $url 2>/dev/null)
stat=$?
if [ $stat -ne 0 ]
then
  echo "bad exit status $stat"
else
  var1=$(echo "$var1" | openssl x509 -noout -issuer -subject -dates)
fi

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 12-08-2015
In bash, pipefail option will do the trick.

Set the pipefail option in top of your bash script.

Code:
set -o pipefail

-Ranga
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Unable to check if file exists on remote server using expect

Hi, I need to check if a file exists on remote server using expect. #!/bin/bash ip_addr=10.10.10.10 user=root passwd=Help filename=/root/test expect -c " spawn ssh -n -T -o NumberOfPasswordPrompts=3 -o StrictHostKeyChecking=no $user@$ip_addr expect \"*?assword:*\" send --... (6 Replies)
Discussion started by: temp_user
6 Replies

3. Shell Programming and Scripting

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,... (6 Replies)
Discussion started by: aix_admin_007
6 Replies

4. AIX

Unable to execute snap command

Hi Friends, I am not able to execute snap -a command in AIX 6 system. Could you please let me know how to make work this command and Path to be set. Thanks in Advance Siva. (4 Replies)
Discussion started by: sivakumarl
4 Replies

5. UNIX for Dummies Questions & Answers

tar unable to use command

Hello all... i am unable to use tar commands on my intel machine in solaris 10 by tying this "tar cvf /dev/rmt/mydata.tar/mydata" and created mydata directory, msg is "No:Missing Files" Please advise (5 Replies)
Discussion started by: VijaySolaris
5 Replies

6. Shell Programming and Scripting

grep functions, how to test if succeeded

Hello ...again. I am stuck on this part, I have a loop with processes an operations file. and calls different functions depending on what is in loop, which processes a database file... #so far my add function works as intended add() { ...blah blah; } # delete is kinda working... (13 Replies)
Discussion started by: gcampton
13 Replies

7. Solaris

Unable to check / filesystem

Hi buddies, I have T1000 and Solaris 10 installed on it. After a power failure system shut down improperly. Error Message is WARNING - Unable to repair the / filesystem. Run fsck manually (fsck -F ufs /dev/rdsk/c0t0d0s0). Jul 18 10:16:09 svc.startd:... (6 Replies)
Discussion started by: ahsen
6 Replies

8. Shell Programming and Scripting

How to tell if "find" command has succeeded ?

It seems that regardless of finding or not finding the file find's exist status is 0. How can i tell (in a shell script) if find has actually found the file ? sameagain-lm:~ $find /opt/cisco-vpnclient /opt/cisco-vpnclient /opt/cisco-vpnclient/bin /opt/cisco-vpnclient/bin/cisco_cert_mgr... (3 Replies)
Discussion started by: hiphamster
3 Replies

9. UNIX for Dummies Questions & Answers

unable to use command line to cd or ls

Hi: I have a script with the next command line >set a=(grep -w "something" textfile.txt | grep -w "anotherthing" | cut -c1-11) In the file textfile.txt there are some file names and their paths so when I use this command line it returns a path, for example /directory, and stores in a. ... (3 Replies)
Discussion started by: bosh
3 Replies

10. BSD

Unable to use 'su' command on FreeBSD6.2

Hi, I'm unable to do 'su' on my FreeBSD6.2 machine, I can remote ssh login as root but 'su' simply doesn't work at all. The message I get says that "The Operation Not Permitted". Requesting any help. Thanks in advance. (3 Replies)
Discussion started by: Praveen_218
3 Replies
Login or Register to Ask a Question