Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm testing traps in Linux, and hence I've some doubts. Following is script for testing traps.
Code:
#!/bin/bash
set -euo pipefail
echo "Script PID: $$"
function CleanUp {
echo
echo Oops I just got killed
exit 2
}
trap 'CleanUp' SIGHUP SIGINT SIGQUIT SIGPIPE SIGTERM EXIT
while true ; do
sleep 1
done
Output:
Code:
$ ./test.sh
Script PID: 16467
^C
Oops I just got killed
Oops I just got killed
My doubts are:
What is 'set -euo pipefail'? I've seen most bash scripts start with this line. I know 'set -u', which makes mandatory to declare a variable before using it. But what about -e and -o pipefail? Read man pages, but couldn't understand.
Why I'm getting multiple trap echo lines?
How do I get exit code based on the type of interrupt signal rather than the exit code which I have specified explicitly (like 2 here)?
My doubts are:
What is 'set -euo pipefail'? I've seen most bash scripts start with this line.
I know 'set -u', which makes mandatory to declare a variable before using it. But what about -e and -o pipefail?
Read man pages, but couldn't understand.
Why I'm getting multiple trap echo lines?
How do I get exit code based on the type of interrupt signal rather than the exit code which I have specified explicitly (like 2 here)?
#1 This set is actually fairly rare, EXCEPT when the script needs to trap events.
The man pages are clear enough. Look up the individual options for set -e -u -o and consider the interaction between them mentioned in the pipefail parameter to the -o option.
#2 You get multiple trap exit lines because you have trapped two events in that script. Think carefully about the flow.
#3 To treat different events differently, you might want to trap different events or signals to different managing functions. There is no directive that "there can be only one"! Consider carefully the syntax, and test your options and code to be sure that you understand the expected outcomes.
to 1. If you don't know what set -e does, don't use it. See BashFAQ #105. About pipefail, see the last paragraph here.
to 2. The first one on SIGINT, the second one on EXIT.
to 3. Then trap each signal separately.
1. The -e means that the script fails if any step fails. The -o pipefail means that if any one of a series of piped programs fails, then the pipe exits with the exit code of the failed program. See "man bash" and scroll down to "Pipelines".
2. You get two lines of output because the routine is called twice. Once from SIGINT caused by ctrl-C, then again by EXIT called when the script finally exits. You might want to remove EXIT
See "man 7 signal", except that signal 0, the EXIT, is not mentioned there.
3. Make separate trap routines for each signal you wish to identify or else look at the contents of the $_ variable within a single routine. A case statement might be a good choice for that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.