Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CTRL-C Question 1

Status
Not open for further replies.

Einstein47

Programmer
Nov 29, 2001
737
US
I have a question about CTRL-C.

When I run the following script from the command-line it will show me the daily log, and then when I hit Ctrl-C it pauses then restarts the tail on the daily log (this is because each day has a new daily log, and I just need to hit Ctrl-C and the current day's log is tailed).

Code:
while [ 1 -eq 1 ]; do
clear
tail -f daily.log
echo "Hit CTRL-C Again to quit"
sleep 5
done

However, when I put that exact code in a script file, it doesn't work. CTRL-C causes the script to end. Is there a way to trap the CTRL-C just durring the tail -f part but then re-enable the default behavior after the echo statement?

Einstein47
“Evil abounds when good men do nothing.“ - Nelson R.
[[]Starbase47.com]
 
Hi

If you use [tt]bash[/tt] or [tt]ksh[/tt] :
Code:
while [red]:[/red]; do
  clear
  [red]trap : SIGINT[/red]
  tail -f daily.log
  [red]trap - SIGINT[/red]
  echo "Hit CTRL-C Again to quit"
  sleep 5
done

Feherke.
 
You may try this:
...
trap ':' 2
tail -f daily.log
echo "Hit CTRL-C Again to quit"
trap 'break' 2
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV - That did it - thanks tons.

feherke - my system didn't understand [red]trap : SIGINT[/red]. It just stopped at that line everytime - I had to specifically code the [red]2[/red] to make it work.

Funny that [red]man trap[/red] returned - [blue]Manual entry for trap not found or not installed[/blue]. That is why I didn't think [red]trap[/red] was a valid command.

Einstein47
“Evil abounds when good men do nothing.“ - Nelson R.
[[]Starbase47.com]
 
Hi

Einstein47, it is not your system, is the shell. I noticed it after posting, that [tt]bash[/tt] accepts both [tt]INT[/tt] and [tt]SIGINT[/tt] but [tt]ksh[/tt] accepts only [tt]INT[/tt].

By the way, [tt]trap[/tt] is a built-in shell function.
Code:
[blue]master #[/blue] type trap
trap is a shell builtin

Feherke.
 
And because it's a shell builtin, you can get information about it by using bash's built-in help command. That is, type [tt]help trap[/tt] instead of [tt]man trap[/tt].

And never assume a command is invalid just because no one bothered to provide a man page for it. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top