Check output of command before outputing and keep exit code

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Check output of command before outputing and keep exit code
# 1  
Old 10-25-2016
Check output of command before outputing and keep exit code

so i have scripts that are piped and then run through one of the following mechanisms:

Code:
cat myscript.sh | sh 

cat myscript.pl | perl

what i want to do is, after either of the above commands are run, if the output from the command contains a certain string, i want it to avoid printing anything. but IF the output does not contain the specified string, then it should proceed as normal and output the result of the script and also preserve the exit code.

so in essence, what im attempting to do can be best illustrated with this:

Code:
cat myscript.sh | sh ; EXITCODE=$? | awk '!/patternToSearchFor/ {print $0}' ; exit ${EXITCODE}

cat myscript.pl | perl ; EXITCODE=$? | awk '!/patternToSearchFor/ {print $0}' ; exit ${EXITCODE}

this code is faulty and i'm hoping someone can help me fix it.
# 2  
Old 10-25-2016
Please avoid cat |!
Should be either
Code:
sh myscript.sh

or
Code:
sh < myscript.sh

or
Code:
< myscript.sh sh

Dito with perl.
--
In the shell the $? exit status is from the last command in the pipe chain.
If you want to get the exit status from the first command,
in standard shell you can save the output
Code:
saved_output=`sh myscript.sh`
EXITCODE=$?
printf "%s\n" "$saved_output" | awk '...'
echo $EXITCODE

In bash each command in a pipe chain is saved in the PIPESTATUS array
Code:
sh myscript.sh | awk '...'
echo ${PIPESTATUS[0]}

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 10-25-2016
Quote:
Originally Posted by MadeInGermany
Please avoid cat |!
Should be either
Code:
sh myscript.sh

or
Code:
sh < myscript.sh

or
Code:
< myscript.sh sh

Dito with perl.
--
In the shell the $? exit status is from the last command in the pipe chain.
If you want to get the exit status from the first command,
in standard shell you can save the output
Code:
saved_output=`sh myscript.sh`
EXITCODE=$?
printf "%s\n" "$saved_output" | awk '...'
echo $EXITCODE

In bash each command in a pipe chain is saved in the PIPESTATUS array
Code:
sh myscript.sh | awk '...'
echo ${PIPESTATUS[0]}

im not actually using cat. the cat is used to illustrate the script that is being piped from another program.

but in your examples, this scenario seems to be the most viable:

Code:
saved_output=`echo "${MYSCRIPT}" | sh`
EXITCODE=$?
echo "$saved_output" | awk '!/PatternToSearchFor/'
echo $EXITCODE

thank you for this.
however, i'm wondering if there's a shorter way to do this? less echos? possibly do it all with awk?
# 4  
Old 10-26-2016
Can you not just:-
Code:
saved_output=`${MYSCRIPT}`
EXITCODE=$?
echo "$saved_output" | awk '!/PatternToSearchFor/'
echo $EXITCODE

If you want to reduce it further, it becomes a bit of a mess, but you might consider:-
Code:
awk '!/PatternToSearchFor/' < <(${MYSCRIPT} ; printf "$?" > /tmp/EXITCODE)
cat /tmp/EXITCODE


Does this help at all?



Robin

Last edited by rbatte1; 10-26-2016 at 10:19 AM.. Reason: Didn't get EXITCODE value from subshell
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

2. Shell Programming and Scripting

Check & use output of svcs command

Hello Dear Friends, I need to check output of svcs command and so the status of some instances. -bash-3.00$ svcs -a | grep rfe online Aug_04 svc:/application/rfe/rfe_master_3:default online Aug_04 svc:/application/rfe/rfe_master_4:default as you can see there are two... (1 Reply)
Discussion started by: EAGL€
1 Replies

3. Shell Programming and Scripting

check for file existance and generate exit code amd move to another directory

Hi.. i have a file ABC_*.txt in source directory which will come evry 30 min and same file will be moved to working directory first time ...and will perform some operations then we archive ABC_*.txt ..this will run for 30 min to 45 min from 2nd time onwards requirement is ...i need to check... (3 Replies)
Discussion started by: dssyadav
3 Replies

4. Shell Programming and Scripting

Expect script: obtain the exit code of remote command

Hi all, I'm trying to run the sipp simulator in crontab but after some attempt I came to the conclusion that for some reason this isn't possible (maybe due to sipp interactive nature). This is confirmed by these posts. Now I'm trying to launch sipp from an expect script that runs in crontab. ... (0 Replies)
Discussion started by: Evan
0 Replies

5. Shell Programming and Scripting

Check for exit status

Hi I have following code I want If whole code executes successfully then return true If found any error then print the error I tried if ; then But this checks only for the just upper line execution #!/bin/bash PATH1=/var/log/mysql PATH2=/home/ankur/log FILE1=mysql-bin.index... (4 Replies)
Discussion started by: kaushik02018
4 Replies

6. Shell Programming and Scripting

How to get the exit code of -exec in the find command

Hi I have a little problem with the find command in a script that I'm writing. The script should check if there are some files younger than 100 seconds and then syncronise them with rsync. My find command: find -type f -cmin -100 -exec rsync -a --delete directory1/ directory2/ When I... (8 Replies)
Discussion started by: oku
8 Replies

7. Shell Programming and Scripting

check exit status of bg scripts

HI All, I am running one shell script, in that script i am calling 4 scripts in the background. abc.ksh & efg.ksh & xky.ksh & mno.ksh & please let me know, how could i find the success and failure of each script. i Cannot use $?, because i want to run all the scripts in parellel. ... (2 Replies)
Discussion started by: javeed7
2 Replies

8. Shell Programming and Scripting

How to get exit code in a pipe-lined command?

I have a question about how to get the exit code of the first command when it appears in a pipe-lined command. For example, I have the following script: grep abc dddd | tee -a log if ] then echo "ERROR!" fi In the above script, ] is supposed to test the exit code of "grep abc... (3 Replies)
Discussion started by: pankai
3 Replies

9. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies

10. UNIX for Advanced & Expert Users

Move command return with exit code of 2

I have a script which loads data files into Oracle and then moves each file into a 'processed' directory when each file has finished loading. Last night I found that the script was failing on the mv statement (with a return code 2) and the following message, mv: cannot access... (1 Reply)
Discussion started by: handak9
1 Replies
Login or Register to Ask a Question