How to use exit status of two commands in if statement ?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to use exit status of two commands in if statement ?
# 1  
Old 02-06-2019
How to use exit status of two commands in if statement ?

I am trying to write a shell script, which looks like

Code:
#!/usr/bin/env bash
#set -e 

RED="`tput setaf 1`"
GREEN="`tput setaf 2`"
BLUE="`tput setaf 4`"
NORM="`tput sgr0`"

pushd ${MY_GIT_TOP}/body/Ue/test >/dev/null #MY_GIT_TOP is set my by gitenv
make test_trinity_svp

pushd ${MY_GIT_TOP}/body/Ue/test/bait
csplit -q --prefix='raj-' --suffix-format='%06d' trinity_trace.log '/ Test: /' '{*}'
rm -f raj-000000
awk 'FNR==1 {print "mv", FILENAME, $4 ".tclog"; nextfile}' raj-?????? | sh
#rm -f some files which are not intresting

     if make test_trinity_svp && ! grep -q '[ERROR]:' *.tclog; then
        echo -e "${GREEN}SUCCESS IN TEST"
     else
        echo -e "\n${RED}FAILURE IN TEST due to presence of ERROR strings\n"
        echo "${NORM}"
        grep -F '[ERROR]:' *.tclog
        rm -f ${MY_GIT_TOP}/body/Ue/test/bait/*tclog
    fi
	popd > /dev/null

In the above code, i am trying to use the exit value of make command and the (not) grep command to success true or failure, but it is not working since Makefile is in different folder one level above the level i am currently on see pushd, i have done pushd twice neither do i want it to run once again.

what i wanted to do is to print success if the test (make command) passed and there are no matches of word '[ERROR]:' in the log generated by the test. how can i do that?

i tried something like
Code:
make test_trinity_svp
test=$?

if $test &&  ! grep -q '[ERROR]:' *.tclog; then

but it didn't work either
what should i have to do to achieve this ? I am using bash shell

Last edited by Sekhar419; 02-06-2019 at 11:56 AM..
# 2  
Old 02-06-2019
It would be interesting to know why ${MY_GIT_TOP} is used as in this script it is a NULL string.
OR was that the intention?


Hope this helps...
This User Gave Thanks to wisecracker For This Post:
# 3  
Old 02-06-2019
wisecracker, ${MY_GIT_TOP} , takes me to the top of the project i am working on, once i source my gitenv it is set.

Last edited by RavinderSingh13; 02-06-2019 at 03:20 PM..
# 4  
Old 02-06-2019
make has an option -c for just this situation.

Code:
if make -c ../../ test_trinity_svp && ! grep whatever
...

That would be ideal I think, but if you want to save the value,
Code:
make ...
MAKERES="$?"

if [[ "$MAKERES" -eq 0 ]] && ! grep whatever
then
...
fi

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-06-2019
It may well be sourced from another file but no-one is to know that as you did NOT have the comment in your original post.
Please don't alter your OP without telling anyone what you did.
Your edit is this:
Last edited by Sekhar419; 20 Minutes Ago at 03:56 PM..
# 6  
Old 02-06-2019
Now, this is quite confusing. Let me try to paraphrase what I can infer from your spec:

In directory ${MY_GIT_TOP}/body/Ue/test you run make test_trinity_svp twice (WHY?), but in between you treat (csplit et al) first make's log files obviously produced in relative directory bait. On the second make, you want to check if an error occurred grepping the FIRST make's output.
How about a slightly different approach?
Code:
cd ${MY_GIT_TOP}/body/Ue/test
make test_trinity_svp
RES=$?
if [ "$RES" -eq  0 ] && ! grep -q '[ERROR]:' bait/trinity_trace.log
  then   echo -e "${GREEN}SUCCESS IN TEST"
  else   echo -e "\n${RED}FAILURE IN TEST due to presence of ERROR strings\n"
.
.
.
fi 
cd bait
csplit ...

grepping the entire log file has the advantage that it will quit after the first match; searching through the many split files will open and traverse every single of them, which may take seriously longer.

Last edited by RudiC; 02-06-2019 at 12:49 PM..
This User Gave Thanks to RudiC For This Post:
# 7  
Old 02-06-2019
wisetracker sorry i have edited it just to make it clear for everyone who reads this thread, after you comment i realized it would be a common question for everyone so i have edited, I am new to this page I didn't knew I shouldn't edit the original post

--- Post updated at 06:55 PM ---

--- Post updated at 06:57 PM ---

--- Post updated at 06:58 PM ---

Hello Rudic,

The problem is i want to ignore some erros in different parts of that big file, so i have divided them into small files and then renamed them according to the first line in the file (as you know from my other question) then i will remove the files which i don't want then i will search for the errors in the remaining parts that are created. i don't know yet which parts i should exclude that is why i have the comment as below

Code:
#rm -f some files which are not intresting

why i did make test twice, i have kept it there for documentation purpose to explain what i am trying to do, i did this which maybe confusing so i replaced it but i still don't know why didn't work with code i have but works with corona688

Code:
make test_trinity_svp
test=$?

if $test &&  ! grep -q '[ERROR]:' *.tclog; then

maybe not a good way of doing this, I have started shell scripting like 6 days ago only, that is what i have came up with so far, I don't know how efficient that script will be.

sorry i didn't know how to reply my replies are being smudged one after other quite confusing layout
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Want to get the exit status

Hi All, I am trying to create a zip file with all the txt files(these are in large number) in the current directory. I am able to do this operation sucessfully. After this i want to get the status of the tar command executed and do accordingly. When i am trying with the below code, the status... (3 Replies)
Discussion started by: paddu
3 Replies

2. Shell Programming and Scripting

exit status from the script is always 0

Hi , I have a bash script , which does the network configuration. Messages from this script are dumped on console as well as stored in a log file . This script is invoked from a C code using system call . The script returns different exit code , to indicate different error cases. The... (1 Reply)
Discussion started by: abhirai
1 Replies

3. Shell Programming and Scripting

Exit Status

I have a shell script (#!/bin/sh) that interacts with Appworx and Banner Admin. In my script I want to check the exit status of awrun before continuing. awrun can run for 10 seconds or it can run for over a minute. So my question is, will it go through my if statement before awrun may even be... (2 Replies)
Discussion started by: smkremer
2 Replies

4. UNIX for Dummies Questions & Answers

Using 'diff' exit status in an if statement

is there a way to compare two files using diff (ex: diff 1.txt 2.txt) in an if statement? I read that the exit status of diff is 0 if the files contain the same content. 1 if they're different. So what I am attempting is basically: if ; then echo "they're the same" else ... (2 Replies)
Discussion started by: SoVi3t
2 Replies

5. Shell Programming and Scripting

Exit status redirection

Hi, I'm having this simple code below, the file serverlist has a list of IPs one per line. When executed the while loop is executed only once, after that the program terminates. How should i redirect the exit status, so that the entire list of IP will get executed? #!/bin/bash exec <... (4 Replies)
Discussion started by: agent001
4 Replies

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

7. Shell Programming and Scripting

Exit status

I'm preparing for exam and one of exams is to write own test command... I wonder if in unix is a command which just returns exit code you specify.. I know I can easily write a function like this: exStatus() { return $1 } -> my question is rather theoretical thank you! (9 Replies)
Discussion started by: MartyIX
9 Replies

8. Shell Programming and Scripting

How to get the exit status

Hi all, I'm running a program which return 1 upon success. But when encounters problem shell return 's '1' . How to differentiate between them the shell return value and script return value. Ex. function fn return '1' if executed successfully and '0' if failed. But when if shell encounters... (1 Reply)
Discussion started by: yhacks
1 Replies

9. Shell Programming and Scripting

exit status

i downloaded a text file from metalab.unc.edu called sh.txt and in this reference manual it refers to shell scripting exit status .. at the end of one of the examples that author gave an exit status of 127.. to what does a 127 exit status refer too and what is its purpose in the code. moxxx68 (1 Reply)
Discussion started by: moxxx68
1 Replies

10. UNIX for Advanced & Expert Users

ftp exit status.

Does ftp from unix have an exit status. In the sense after ftp is invoked and if the ftp fails during file transfer does it exit out with a status other than 0. What is do right now is invoke ftp and right it to a log and then grep for 'File Transferred Sucessfully'. Is this the only way to do it... (1 Reply)
Discussion started by: oracle8
1 Replies
Login or Register to Ask a Question