What does `echo $?` mean in bash? [duplicate] - bash

This question already has answers here:
Meaning of $? (dollar question mark) in shell scripts
(8 answers)
Closed 9 years ago.
I came upon the following command:
echo $?
what does that command do?

Echoes (prints) the exit value for the previous command.
If it failed it will be different than zero (0).
$ cd ~
$ echo $?
> 0
$ cd //whatever/
> bash: cd: //whatever/: No such file or directory
$ echo $?
> 1
Programs exit with a status code. Every program is unique and has a different set of failure codes, but it's universally acknowledged that 0 is the 'success' code.

Related

Exit Code of command is 0, but binaries exit code is 3 [duplicate]

This question already has answers here:
Pipe output and capture exit status in Bash
(16 answers)
Closed 8 years ago.
I made a simple script:
$ more test.bash
#!/bin/bash
echo test
exit 1
When I run the script , the exit status should be 1
$ /tmp/test.bash
echo $?
1
But when I run this as the following
/tmp/test.bash | tr -d '\r' 1>>$LOG 2>>$LOG
echo $?
0
The exit status is 0, (not as expected 1)
It seems that the exit status comes from tr command.
But I what I want is to get the exit status from the script - test.bash.
What do I need to add/change in my syntax in order to get the right exit status from the script, and not from the command after the pipe line?
Use the PIPESTATUS array:
$ ls foo | cat
ls: foo: No such file or directory
$ echo ${PIPESTATUS[0]} ${PIPESTATUS[1]}
2 0
Note: PIPESTATUS is a bashism (i.e. not POSIX).

Automatically exit when bash command produce return code non zero [duplicate]

This question already has answers here:
Automatic exit from Bash shell script on error [duplicate]
(8 answers)
Error handling in Bash [closed]
(15 answers)
Closed 4 years ago.
In bash how do we make the script to automatically exit if a command line return code is not zero. For example:
#!/bin/bash
cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"
I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.
And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command
There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.
if cd /something_something && mv file_a /somedir/file_a; then
echo $?
exit
fi
echo "Both cd and mv worked"

What is && and || in bash? [duplicate]

This question already has answers here:
What is the purpose of "&&" in a shell command?
(9 answers)
Closed 4 years ago.
I am trying to understand following pice of code:
for f in *.out; do sort -cg <$f &>/dev/null && res="sorted" || res="unsorted"; echo "File $f is $res."; done
For loop iterates through all .out files and gives each one as a parameter to sort, and the output of the sort is redirected into "nothing". But can someone explain what: && res="sorted" || res="unsorted" does?
A command after the && will only be executed if the previous command exited with a 0 (zero) status code. The other || works in the opposite way - a command after a || will only execute if the previous command exited with a non-zero exit code.
Here is a small example:
cat /some/file/that/is/missing && echo 'Found the file!' # doesn't print
cat /some/file/that/is/missing || echo 'Unable to find the file!' # will print
With the first line, the echo command will not execute since the cat command failed (because the file doesn't exist)
With the second line, we WILL see the echo command because the cat command failed.

Bash judgement gets the unexpected result [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 5 years ago.
This is the code of the my shell script:
#! /bin/bash
if ["$SHELL" = "/bin/bash"];then
echo "this is bash"
elif ["$SHELL" = "aa"];then
echo "this is aa"
else
echo "this is not /bin/bash, but $SHELL"
fi
why I execute the test_bash_03 script file gets the else result? shouldn't it be:this is bash ?
aircraftdeMacBook-Pro:bash_demo ldl$ ./test_bash_03
./test_bash_03: line 3: [/bin/bash: No such file or directory
./test_bash_03: line 5: [/bin/bash: No such file or directory
this is not /bin/bash, but /bin/bash
And I echo the $SHELL I also get the /bin/bash
aircraftdeMacBook-Pro:bash_demo ldl$ echo $SHELL
/bin/bash
You are missing a space after [ and before ].
The bash tries to execute a command named [/bin/bash instead of [ (which is test), then doesn't find that and has an exit code of 1 (false). So you end up in the else case.

Bash - if any process is killed, exit without passing to the next steps [duplicate]

This question already has answers here:
How to exit if a command failed? [duplicate]
(9 answers)
Closed 6 years ago.
I have such a bash file;
./process1
./process2
./process3
./process4
./process5
Let's say I run this bash script, and process2 is killed for some reason. Without passing to process3, I directly want to exit. How can I manage this?
Thanks,
Just exit if non-zero exit code:
./process1 || exit
and so on ...
Another way in bash, use -e flag:
#!/bin/bash
set -e
-e Exit immediately if a command exits with a non-zero status.
You can try it so:
./process1 && ./process2 && ./process3 && ./process4 && ./process5

Resources