Kyle_S-C
Kyle_S-C

Reputation: 1177

Different exit codes

I'm trying to grep for a version number from my subversion command, so that I can check we have the write subversion module loaded in a bash script.

Interactively, this is an example use:

> svn --version | head -n1 | grep -q '1.7'; echo $?
0

However, when I put this same line (and nothing else) in a script and run the script:

> ./setup_svn.sh
1

Also, the script runs noticeably faster than the interactive shell command. Does anyone have ideas of what I might be missing that explains this result?


Edit

It turns out that my interactive bash script was using the wrong svn command. Not sure exactly why, but I think that might be a question for the Unix StackExchange.

It's almost certainly to do with the module system on our workstations, running interactively I get:

> module list
Currently Loaded Modulefiles:
...
12) subversion/1.7.7    
> which svn
/usr/bin/svn
> svn --version
svn, version 1.7.7 (r1393599)
...

Running in the script, I get:

> ./setup_svn.sh
Currently Loaded Modulefiles:
...
12) subversion/1.7.7    
/usr/bin/svn
svn, version 1.6.17 (r1128011)
...

Further edit

It seems that if I start a new shell, I also get the same issues:

> bash
> module list
Currently Loaded Modulefiles:
...
12) subversion/1.7.7
> svn --version
svn, version 1.6.17 (r1128011)

I think I'll find out what our module system does to the environment and use that to work out what's going wrong.

Upvotes: 0

Views: 138

Answers (2)

Junior Dussouillez
Junior Dussouillez

Reputation: 2397

I know it will not correct the issue, but I think the head command is useless. You can use the --quiet option.

$ svn --version --quiet
1.7.5

EDIT:

If it's a semicolon issue as mentioned by konsolebox, you can also use this syntax :

echo $(svn --version --quiet | grep -q '1.7')$?

Upvotes: 1

konsolebox
konsolebox

Reputation: 75488

It could be how the shell handles the semicolon. Try to place in two lines.

#!/bin/sh
svn --version | head -n1 | grep -q '1.7'
echo "$?"

If you're in bash also, make sure that pipefail is not enabled:

#!/bin/bash
shopt -u -o pipefail
svn --version | head -n1 | grep -q '1.7'
echo "$?"

Upvotes: 0

Related Questions