- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Traps in scripts with functions..
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2001 11:49 AM
10-09-2001 11:49 AM
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2001 11:58 AM
10-09-2001 11:58 AM
Re: Traps in scripts with functions..
From the man pages of ksh:
If sig is 0 or EXIT and the trap statement is executed inside the body of a function, the command arg is executed after the function completes. If sig is 0 or EXIT for a trap set outside any function, the command arg is executed on exit from the shell.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2001 12:06 PM
10-09-2001 12:06 PM
SolutionThere is nothing wrong with placing the trap "globally" at the beginning of the script. Add an 'exit' to the commands to be executed when the trap is caught, as:
#!/usr/bin/sh
trap 'echo "bye!";exit 1' INT
function DO_IT
{
echo "waiting IO..."
read X
echo $X
}
DO_IT
exit 0
#.end.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2001 12:08 PM
10-09-2001 12:08 PM
Re: Traps in scripts with functions..
Actually a trap can be set anywhere even within a function and its effect is global even after the function has finished. One the trap has executed, program execution resumes unless you put an exit within your trap.
Try this exeample:
#!/usr/bin/sh
trap 'echo Trap!' 2 3 15
count()
{
STOP=$1
I=1
while [ ${I} -le ${STOP} ]
do
echo "${I}"
I=`expr ${I} + 1`
done
return 0
} # count
count 100
If your send an intr via Ctrl-C the "Trap!" message will be echoed and execution will resume.
However if we modify the trap statement to:
trap 'echo Trap!; exit 1' 2 3 15
then execution stops after the echo. This should fix you.
Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2001 12:23 PM
10-09-2001 12:23 PM
Re: Traps in scripts with functions..
One more offering. Unfortunately the shell has a very limited concept of global and local environments. With traps, the last setting persists even outside the function:
#!/usr/bin/sh
trap 'echo "bye!";exit 1' INT HUP
function DO_IT
{
echo "waiting IO...but you can't interrupt me..."
read X
echo $X
}
function DO_IT_TOO
{
trap '' INT HUP
echo "waiting IO...but you can't interrupt me!!!"
read X
echo $X
}
DO_IT_TOO
DO_IT
echo "...try to interrupt now (you can't)"
read X
exit 0
#.end.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2001 01:37 PM
10-09-2001 01:37 PM
Re: Traps in scripts with functions..
David..