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

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

#!/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

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

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

1 Like

wisecracker, ${MY_GIT_TOP} , takes me to the top of the project i am working on, once i source my gitenv it is set.

make has an option -c for just this situation.

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

That would be ideal I think, but if you want to save the value,

make ...
MAKERES="$?"

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

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..[/i]

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 grep ping the FIRST make 's output.
How about a slightly different approach?

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

grep ping 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.

1 Like

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

#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

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

If you want to run two commands in different work directories, then you can use a (subshell)

if (cd otherdir && make ...) && grep ...
then

Or vice versa

if make ... && (cd otherdir && grep ...)
then

The sub shell is a fork of the main shell. It inherits the current work directory, but it won't return it. What it returns: the output (stdout and sterr) and the exit status (normally from the last command).
Also you can use

(cd otherdir; grep ...)

or

(
cd otherdir
grep ...
)

but in general it is safer to only continue if the cd was successful.

Here comes a demo that the sub shell is indeed a complete shell:

if (
  if cd otherdir; then
    make ...
  fi
) && grep ...
then
1 Like

OK, starts to make some sense now. Knowing that you want to exclude certain parts of the log file, csplit and grep might not be the optimal choice. If you show some more details, a taylored solution based on sed , awk , perl , etc. might be possible.

What do you mean by "smudged"? I usually (not always, I confess) post and then proofread my post to eliminate the obvious nonsense I made.

EDIT: One more comment: As you can see, quite many people posted quite many remarks (to your other, related thread(s) as well) while obviously poking in the dark. Had you clearly described the problem, supplying representative sample input data and e.g. exclusion conditions for error types, you'd highly probably have a taylored overall solution by now. It usually pays back quickly to spend some decent time composing a decent specification!