<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Traps in scripts with functions.. in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2591998#M925100</link>
    <description>Hi David,&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Try this exeample:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;&lt;BR /&gt;trap 'echo Trap!' 2 3 15&lt;BR /&gt;&lt;BR /&gt;count()&lt;BR /&gt;{&lt;BR /&gt;  STOP=$1&lt;BR /&gt;  I=1&lt;BR /&gt;  while [ ${I} -le ${STOP} ]&lt;BR /&gt;     do&lt;BR /&gt;        echo "${I}"&lt;BR /&gt;        I=`expr ${I} + 1`&lt;BR /&gt;     done&lt;BR /&gt;  return 0&lt;BR /&gt;} # count&lt;BR /&gt;&lt;BR /&gt;count 100&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;If your send an intr via Ctrl-C the "Trap!" message will be echoed and execution will resume.&lt;BR /&gt;&lt;BR /&gt;However if we modify the trap statement to:&lt;BR /&gt;trap 'echo Trap!; exit 1' 2 3 15&lt;BR /&gt;&lt;BR /&gt;then execution stops after the echo. This should fix you.&lt;BR /&gt;&lt;BR /&gt;Clay&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Tue, 09 Oct 2001 19:08:17 GMT</pubDate>
    <dc:creator>A. Clay Stephenson</dc:creator>
    <dc:date>2001-10-09T19:08:17Z</dc:date>
    <item>
      <title>Traps in scripts with functions..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2591995#M925097</link>
      <description>I am writing a script to kick of some system tests.. I have used many functions in my script to make it a little more manageable, the script has a lot of setup and clean up both setting up and cleaning up can take a few minutes per function, so in order to make sure it cleans up correctly when killed I have used the trap command to catch kill signals and clean up properly.. However, when sending CTRL+C, the trap command fires and starts finishing the test and cleans up, but the rest of the script continues running.. shouldn't the entire script stop except for the trap?  I've verified this by putting a number of Echo statements throughout, and sure enough, when I press CTRL+C, I see echo message during the clean-up and during set-up.  I want the setup portion to completely stop with CTRL+C, I suspect this has something to do with the fact that I am using functions, but am unclear of the relationship..  Should I put the trap command at the beginning of each function?</description>
      <pubDate>Tue, 09 Oct 2001 18:49:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2591995#M925097</guid>
      <dc:creator>David Snider</dc:creator>
      <dc:date>2001-10-09T18:49:08Z</dc:date>
    </item>
    <item>
      <title>Re: Traps in scripts with functions..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2591996#M925098</link>
      <description>Are you using ksh?&lt;BR /&gt;&lt;BR /&gt;From the man pages of ksh:&lt;BR /&gt;&lt;BR /&gt;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.</description>
      <pubDate>Tue, 09 Oct 2001 18:58:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2591996#M925098</guid>
      <dc:creator>harry d brown jr</dc:creator>
      <dc:date>2001-10-09T18:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: Traps in scripts with functions..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2591997#M925099</link>
      <description>Hi David:&lt;BR /&gt;&lt;BR /&gt;There 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:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;trap 'echo "bye!";exit 1' INT&lt;BR /&gt;function DO_IT&lt;BR /&gt;{&lt;BR /&gt;  echo "waiting IO..."&lt;BR /&gt;  read X&lt;BR /&gt;  echo $X&lt;BR /&gt;}&lt;BR /&gt;DO_IT&lt;BR /&gt;exit 0&lt;BR /&gt;#.end.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 09 Oct 2001 19:06:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2591997#M925099</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2001-10-09T19:06:16Z</dc:date>
    </item>
    <item>
      <title>Re: Traps in scripts with functions..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2591998#M925100</link>
      <description>Hi David,&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Try this exeample:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;&lt;BR /&gt;trap 'echo Trap!' 2 3 15&lt;BR /&gt;&lt;BR /&gt;count()&lt;BR /&gt;{&lt;BR /&gt;  STOP=$1&lt;BR /&gt;  I=1&lt;BR /&gt;  while [ ${I} -le ${STOP} ]&lt;BR /&gt;     do&lt;BR /&gt;        echo "${I}"&lt;BR /&gt;        I=`expr ${I} + 1`&lt;BR /&gt;     done&lt;BR /&gt;  return 0&lt;BR /&gt;} # count&lt;BR /&gt;&lt;BR /&gt;count 100&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;If your send an intr via Ctrl-C the "Trap!" message will be echoed and execution will resume.&lt;BR /&gt;&lt;BR /&gt;However if we modify the trap statement to:&lt;BR /&gt;trap 'echo Trap!; exit 1' 2 3 15&lt;BR /&gt;&lt;BR /&gt;then execution stops after the echo. This should fix you.&lt;BR /&gt;&lt;BR /&gt;Clay&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Oct 2001 19:08:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2591998#M925100</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2001-10-09T19:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: Traps in scripts with functions..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2591999#M925101</link>
      <description>Hi David:&lt;BR /&gt;&lt;BR /&gt;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:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;trap 'echo "bye!";exit 1' INT HUP&lt;BR /&gt;function DO_IT&lt;BR /&gt;{&lt;BR /&gt;  echo "waiting IO...but you can't interrupt me..."&lt;BR /&gt;  read X&lt;BR /&gt;  echo $X&lt;BR /&gt;}&lt;BR /&gt;function DO_IT_TOO&lt;BR /&gt;{&lt;BR /&gt;  trap '' INT HUP&lt;BR /&gt;  echo "waiting IO...but you can't interrupt me!!!"&lt;BR /&gt;  read X&lt;BR /&gt;  echo $X&lt;BR /&gt;}&lt;BR /&gt;DO_IT_TOO&lt;BR /&gt;DO_IT&lt;BR /&gt;echo "...try to interrupt now (you can't)"&lt;BR /&gt;read X&lt;BR /&gt;exit 0&lt;BR /&gt;#.end.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 09 Oct 2001 19:23:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2591999#M925101</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2001-10-09T19:23:23Z</dc:date>
    </item>
    <item>
      <title>Re: Traps in scripts with functions..</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2592000#M925102</link>
      <description>Much thanks to James and Clay.. 10 points each as it appears you were both replying at the same time!..  fledgling admin forgot the exit..&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;David..</description>
      <pubDate>Tue, 09 Oct 2001 20:37:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/traps-in-scripts-with-functions/m-p/2592000#M925102</guid>
      <dc:creator>David Snider</dc:creator>
      <dc:date>2001-10-09T20:37:43Z</dc:date>
    </item>
  </channel>
</rss>

