Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash always returning error code eq 0

Tags:

bash

I was I am trying to check if a folder is part of a git or svn repository. I use the following command to get that and store the error code in a variable.

RSVN=`svn status &> /dev/null; echo $?`
RGIT=`git status &> /dev/null; echo $?`

The thing is I am always getting 0 as error code.

I try it in the command line and I get the expected result

user@host trunk$ svn status &> /dev/null;
user@host  trunk$ echo $?
0
user@host  trunk$ git status &> /dev/null;
user@host  trunk$ echo $?
128

but when I run the script I get :

+ + svn status

+ echo 0
+ RSVN=0
M       buildpackage
+ git status
+
+ echo 0
fatal: Not a git repository (or any parent up to mount point /build)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
+ RGIT=0

The way this called is as subscript. It is callled by an othe script.

first script

#!/bin/sh -x
#set -e

#do stuf 

. ../../../tools/second_script.sh

second script

#!/bin/sh -x
set -e

#do stuf 

RSVN=`svn status &> /dev/null; echo $?`
RGIT=`git status &> /dev/null; echo $?`

Do you know why this is happening?

like image 373
fcptin Avatar asked Jun 24 '26 16:06

fcptin


1 Answers

The variable is being substituted by the shell before the command is executed. Do this instead:

svn status &> /dev/null; RSVN=$?
git status &> /dev/null; RGIT=$?

Actually, get out of the habit of using ALL_CAPS_VARS: one day you'll say PATH=$(something) and then wonder why your script is broken.

like image 125
glenn jackman Avatar answered Jun 26 '26 10:06

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!