LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-13-2020, 08:31 AM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Rep: Reputation: 56
About Traps in bash script


Hello All

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:
  1. 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.
  2. Why I'm getting multiple trap echo lines?
  3. 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)?

Thanks
 
Old 05-13-2020, 08:44 AM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,662

Rep: Reputation: 2710Reputation: 2710Reputation: 2710Reputation: 2710Reputation: 2710Reputation: 2710Reputation: 2710Reputation: 2710Reputation: 2710Reputation: 2710Reputation: 2710
Is this homework?

Quote:
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.

Last edited by wpeckham; 05-13-2020 at 08:50 AM.
 
2 members found this post helpful.
Old 05-13-2020, 08:46 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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.
 
2 members found this post helpful.
Old 05-13-2020, 08:51 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,327
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
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.
 
2 members found this post helpful.
Old 05-13-2020, 09:54 AM   #5
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Thanks everybody.
 
Old 05-13-2020, 07:29 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,732

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by ddenial View Post
Thanks everybody.
Would you please share what worked for you?
 
Old 05-14-2020, 12:47 AM   #7
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by scasey View Post
Would you please share what worked for you?
Sorry about this. https://www.linuxquestions.org/quest...rd-4175675084/

Last edited by ddenial; 05-14-2020 at 12:53 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Microsoft Putting Patent Traps Inside Linux While Blackmailing Companies Using Patents Associated With These Traps LXer Syndicated Linux News 0 07-11-2019 04:06 PM
LXer: How to modify scripts behavior on signals using bash traps LXer Syndicated Linux News 0 10-25-2017 08:01 AM
The Traps Of Linux...&open Source Software john20004 Linux - General 1 04-22-2004 03:16 PM
The Traps Of Linux...&open Source Software john20004 LQ Suggestions & Feedback 1 04-22-2004 02:46 PM
The Traps Of Linux...&open Source Software john20004 Linux - Software 1 04-22-2004 02:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration