What does 1>&2 mean in shell? [duplicate] - shell

This question already has answers here:
echo that outputs to stderr
(15 answers)
What does " 2>&1 " mean?
(19 answers)
Closed 8 years ago.
Pretty noob question, what does the 1>&2 do in this script?
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi

That redirects the line "This script must be run as root" from standard out (STDOUT) to standard error output (STDERR).
It's a easy way to print an error message to STDERR - this matters if you run the bash script from another script (like crontab), matters much less if you run it from the command line driectly since your terminal will show both STDOUT and STDERR.
See also echo that outputs to stderr

Related

Can a bash script distinguish between being called as a script and being run as a "source"? [duplicate]

This question already has answers here:
How to detect if a script is being sourced
(22 answers)
Closed 3 years ago.
I have a bash script that has inside it:
exit 1
When I "source" this script instead of running it, it causes the caller to exit.
Is there a way that the script can determine that it's being run with "source" and not as its script?
You can use this check inside your script:
[[ $0 = $BASH_SOURCE ]] && echo "normal run" || echo "sourced run"
Or using if/else/fi wherever you're calling exit:
if [[ $0 = $BASH_SOURCE ]]; then
exit 1
else
# don't call exit
echo "some error..."
fi

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"

How to put a command in nohup in shell script? [duplicate]

This question already has answers here:
How to include nohup inside a bash script?
(6 answers)
Closed 4 years ago.
I am trying to put shred command in nohup for shell script, once the script is run it has to take input and run shred operation on a device in nohup mode. The problem is when I add nohup to command the script does not exit and run the command in background, also i am trying to send success and failure mails with the output once shred operation is completed.
What I tried so far:
nohup shred -n 2 "device name" > success.out 2>failure.out
if [$? -eq 0]
then
mail -s "success" -a success.out "EmailID" <.
exit 0
else
mail -s "failure" -a failure.out "EmailID" <.
exit 1
fi
I am getting the success email with attachment n=but the attachment is non readable format, is there any other way??
"the script does not exit and run the command in background".
You do not tell the script to background it. Add &.
So:
nohup shred -n 2 "device name" & >success.out 2>failure.out
Will do it.

What does “1>&2” mean in bash? [duplicate]

This question already has answers here:
What does " 2>&1 " mean?
(19 answers)
Closed 6 years ago.
What does 1>&2 mean in a bash script?
For instance, what does the following line from a bash script do?
echo "$1 is not a directory!" 1>&2
I use MacOS X. My bash script is:
if [ ! -d $1 ]; then
echo "$1 is not a directory" 1>&2
exit 1
fi
It outputs the message to stderr. Therefore 1>&2 means redirect stdout to stderr.
3. All about redirection

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

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.

Resources