0

i am a beginner with bash and im trying to understand someone elses bash script. The script consists of several subsequent invokings of rscripts with certain parameters. All these if statements have roughly the same syntax, as follows:

if Rscript -options > log_file.txt 2>&1
    script works, do smth.
else
    script failed, leave the ship!

I simply cant get my head around why this if statement does what is does. I know that 2>&1 "combines" stderr and stdout. How does this syntax work exactly?

Thanks for the answers.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Malum
  • 1
  • 1
  • Give a read to this answer: [cmd 2>&1 > log vs cmd > log 2>&1](http://stackoverflow.com/a/4708817/1983854). – fedorqui May 19 '16 at 08:58
  • Have you tried reading [the Bash manual page](http://man7.org/linux/man-pages/man1/bash.1.html)? – Some programmer dude May 19 '16 at 08:59
  • Im sorry if i was not specific enough. What confuses me is how bash knows that the statement "Rscript -options > log_file.txt 2>&1" is TRUE when there is no error, and FALSE when there is one. – Malum May 19 '16 at 09:25

3 Answers3

2

The Bash built-in command if doesn't use the output of the program to determine the condition, it uses the return code or exit status of the "command" used as the condition.

Using e.g.

if anycommand; then
...
fi

is equivalent to

anycommand
if [ $? == 0 ]; then
...
fi

The anycommand may contain any kind of options and redirections. If you have a series of piped command, the exit code used is the one of the last foreground command, so in a | b | c the exit code of c is used.

Also note that an exit code of zero is considered to be a success, i.e. it is true when used in a condition. Any exit code that is non-zero is false.


In a C program the exit code is what the main function returns, or the value passed to the exit() function.

In a Bash script or function the exit code is what is passed to the built-in exit or return commands.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

The numbers are the file descriptor numbers : 2 is the standard error output (stderr) and 1 is the standard output (stdout), so by writing 2>&1 you are saying : redirect the output from 2 (stderr) to the same as 1 (stdout), and in your command it means to log_file.txt.

zoubida13
  • 1,738
  • 12
  • 20
0

In the above case you mentioned. we are trying to execute a script in a if condition.

if Rscript -options > log_file.txt 2>&1

As per above statement, Rscript is a script and variable "options" stores the argument to run the script. When control executes if condition, first it executes Rscript file with the argument that is stored in options. During the execution of Rscript, all print statements related to Rscript will redirected to "log_file.txt" because of "> log_file.txt". Apart from print statements, error that produced during the execution of the script will also be redirected to text file, because of 2>&1 statement.

2>&1, by this statement we are instructing controller to log all the stderr to stdout, where "log_file.txt" is considered as stdout in this case.

Finally, if Rscript returns true value then control will enter into if condition , If not control will execute else condition.

Krishna
  • 179
  • 3
  • 6
  • Thank you for the answer. It seems i was not clear. What confuses me is how bash knows that the statement "Rscript -options > log_file.txt 2>&1" is TRUE when there is no error, and FALSE when there is one (i only guess this is what the if statement does because of the context of the script). – Malum May 19 '16 at 09:26