LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2023, 07:55 PM   #1
zeroone
LQ Newbie
 
Registered: Jul 2023
Posts: 13

Rep: Reputation: 0
Bash trap is to mean just some certain sequential lines


How can we have Bash trap meaning some certain sequential code lines only not for the rest of script ?
 
Old 07-16-2023, 10:08 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Maybe an example would help?
 
Old 07-16-2023, 11:06 PM   #3
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,640

Rep: Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697
Your meaning is unclear. Please enlighten us.
 
Old 07-17-2023, 01:29 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,864

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
yes, would be nice to explain better what do you mean. By the way, here is the documentation: https://man7.org/linux/man-pages/man1/trap.1p.html, but obviously you can read the man page of bash (and look for the built-in command trap).
 
Old 07-17-2023, 08:43 AM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by zeroone View Post
How can we have Bash trap meaning some certain sequential code lines only not for the rest of script ?
trap traps signals intended for the current process. So, as long as all your code runs as a single process you cannot trap signals only for parts of it. But you can start another process from inside your code. Something like this (untested):

Script a:
Code:
#!/bin/sh
trap 'echo Trapped on exit from a' EXIT
read -rp 'Start b (y/n) ' reply
[ 'y' = "$reply" ] && exec sh b
echo End of a
Script b:
Code:
#!/bin/sh
echo Inside b
 
Old 07-17-2023, 09:18 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Or this will trap SIGINT for a little while and then go back to the default behavior.

Code:
#!/bin/sh

trapped () {
        echo "Trapped.  Exiting"
        exit
}

echo 1
sleep 2

echo 2
sleep 2

trap trapped INT

echo 3
sleep 2

echo 4
sleep 2

trap - INT

echo 5
sleep 2

echo 6
sleep 2

exit 0
If ctrl-C is pressed any time except between 3 and 4 then it will exit as normally. Between 3 and 4, ctrl-C will exit via the function trapped() instead.

See "man sh" under the section for "trap" and "man 7 signal" for more about signals.
 
4 members found this post helpful.
Old 07-30-2023, 01:18 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Subshell?
Code:
(
   trap code signal
   ..
)
 
  


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
Bash trap ERR trap on statement that appears to be working pasha19 Programming 13 04-07-2022 02:13 AM
[SOLVED] sequential : how to find the missing numbers within a sequence of files that have sequential numbers attached to them? BW-userx Programming 71 07-15-2017 08:52 PM
[SOLVED] how to modify certain sequential patterns in bash or c schneidz Programming 27 07-28-2012 09:14 AM
how to send snmp trap & recieve trap in C program minil Programming 3 07-10-2010 09:22 AM
Extract lines containing some strings without affectting sequential order cgcamal Programming 7 11-06-2008 11:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 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