I am working on a bash script that I would like to work for several types of VCS.
I am thinking of testing if a directory is a repo for a system by running a typical info command and checking the return code, success or error.
In pseudo code:
if a svn command succeded;
Then run svn commands
elif a darcs command succeded;
Then run darcs commands
elif a mercurial command succeded;
then run hg commands
else
something else
fi
I can run a command, e.g.
darcs show repo
and use $?
to get its return code.
My question is: is there a neat way to run and return the return code number in one line? for example
if [ 0 -eq `darcs show repo`$? ];
Or do I have to define a function?
An added requirement is that both stderr and stdout should be printed.
Best Answer
If automatically checks the return code:
You could also run the command and use && (logical AND) or || (logical OR) afterwards to check if it succeeded or not:
Redirecting
stdout
andstderr
can be done once withexec
The first two
exec
are saving thestdin
andstderr
file descriptors, the third redirects both to/dev/null
(or somewhere other if wished). The last twoexec
restore the file descriptors again. Everything in between gets redirected to nowhere.Append other repo checks like Gilles suggested.