HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- trap not working as expected
Operating System - HP-UX
1826507
Members
3159
Online
109693
Solutions
Forums
Categories
Company
Local Language
back
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
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- 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-27-2009 09:33 AM
10-27-2009 09:33 AM
trap not working as expected
Below is the prototype of the script I am using -
==============================================
function1
{ typeset -i ERR=0
trap return ${ERR} ' 2 3 15 '
#some commands modify the value of ERR #
}
funtion2
{ typeset -i ERR=0
trap return ${ERR} ' 2 3 15 '
#some commands modify the value of ERR #
}
menu ()
{ select in item 'item1' 'item2'
do
case $REPLY in
1 )
function1
echo $? ;;
2 )
function2
echo $? ;;
esac
done
}
menu
=============================================
The expected - >
Whenever a INT signal is recieved when the the script is inside function1 or function2, the trap code should execute which is a return statement. Thus control control should pass to calling function i.e. menu
The actual - >
the function ( function1 or function2 ) continues to execute from the line next to the one where interrupt was recieved !!
Can someone please help me to achieve 'The expected '
==============================================
function1
{ typeset -i ERR=0
trap return ${ERR} ' 2 3 15 '
#some commands modify the value of ERR #
}
funtion2
{ typeset -i ERR=0
trap return ${ERR} ' 2 3 15 '
#some commands modify the value of ERR #
}
menu ()
{ select in item 'item1' 'item2'
do
case $REPLY in
1 )
function1
echo $? ;;
2 )
function2
echo $? ;;
esac
done
}
menu
=============================================
The expected - >
Whenever a INT signal is recieved when the the script is inside function1 or function2, the trap code should execute which is a return statement. Thus control control should pass to calling function i.e. menu
The actual - >
the function ( function1 or function2 ) continues to execute from the line next to the one where interrupt was recieved !!
Can someone please help me to achieve 'The expected '
- Tags:
- trap
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2009 10:14 AM
10-27-2009 10:14 AM
Re: trap not working as expected
What if you placed the trap outside of the function ?
#!/usr/bin/ksh
trap "echo 'interrupt...';menu" 2
function1()
{
echo function1
read yorn
echo "user chose enter"
}
function2()
{
echo function2
read yorn
echo "user chose enter"
}
menu()
{
select option in function1 function2 exit
do
case "$option" in
function1) function1;;
function2) function2;;
exit) echo "exiting...";exit ;;
esac
done
}
menu
#!/usr/bin/ksh
trap "echo 'interrupt...';menu" 2
function1()
{
echo function1
read yorn
echo "user chose enter"
}
function2()
{
echo function2
read yorn
echo "user chose enter"
}
menu()
{
select option in function1 function2 exit
do
case "$option" in
function1) function1;;
function2) function2;;
exit) echo "exiting...";exit ;;
esac
done
}
menu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2009 10:26 AM
10-27-2009 10:26 AM
Re: trap not working as expected
Hi:
Aside from some syntax errors like no keyword 'function' before the function name; and a syntactically incorrect 'select' [ it should be "select item in ..." ], you need to rethink your approach.
From the 'sh-posix' manpages, "traps remain in effect for a given shell until explicitly changed with another trap command; that is, a trap set within a function will remain in effect even after the function returns."
I would globally trap and ignore signals 2, 3 and 15 and let your functions 'return' values that are otherwise meaningful to the calling block.
Regards!
...JRF...
Aside from some syntax errors like no keyword 'function' before the function name; and a syntactically incorrect 'select' [ it should be "select item in ..." ], you need to rethink your approach.
From the 'sh-posix' manpages, "traps remain in effect for a given shell until explicitly changed with another trap command; that is, a trap set within a function will remain in effect even after the function returns."
I would globally trap and ignore signals 2, 3 and 15 and let your functions 'return' values that are otherwise meaningful to the calling block.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2009 12:21 PM
10-28-2009 12:21 PM
Re: trap not working as expected
Hello,
You should surround the command to be executed with quotes (actually double quotes to allow the shell substituting $ERR) instead of the signal numbers :
trap "return $ERR" 2 3 15
Cheers,
Jean-Philippe
You should surround the command to be executed with quotes (actually double quotes to allow the shell substituting $ERR) instead of the signal numbers :
trap "return $ERR" 2 3 15
Cheers,
Jean-Philippe
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Support
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP