Passing ctrl + c in shell scripting

Hi,

How to pass ctrl + c in shell scripting.

Regards,
shausy

Please refer to the many examples on this forum for trapping signals

for eg.

Hope this helps

Another one from the web is

catch Ctrl-C from shell script

If you are trying to send a control-C, that's just sending a SIGINT.

kill -s SIGINT <PID>

also

trap exit INT

Thanks for all your response.

Let me tell my requirement clearly,
I have script which runs some commands followed by
....
tail -f /var/log/messages (only way to end this command by passing ctrl + c)
and again i want to run the commands
.....

So i want to pass ctrl + c inside my script just to complete one command and start executing next command.

I tried with trap and kill its completely kill the script, so the remaining part of my script not executing.

Thanks in advance,
Shausy

it seems that the answer to that question might depend on the shell you are using

Apparently if you use csh then you are in luck

10.9 Built-in Shell Commands

Please read above link. My understanding of your issue is limited to about 30 minutes spent on google but heres my attempt for bash shell

#!/bin/bash
# user interrupt trap demo
function int_handler {
  echo "-- Tail interrupted"
RUN SECOND SET OF COMMANDS AFTER TAIL
 exit
}
# Establish interrupt handler
PERFORM TASKS BEFORE TAIL COMMAND
trap int_handler INT
# Do some work...
TAIL COMMAND
 

---------- Post updated at 02:54 AM ---------- Previous update was at 02:30 AM ----------

I made a slight modification to the code and tried to run it on my solaris system. Please have a look

#!/bin/bash
# user interrupt trap demo
function int_handler {
echo "-- Tail interrupted"
}
# Establish interrupt handler
echo PERFORM TASKS BEFORE TAIL COMMAND
trap int_handler INT
# Do some work...
tail -f /var/log/syslog
echo RUN SECOND SET OF COMMANDS AFTER TAIL

Maybe you can have a look at this thread :

give a try to smthg like

doTail() {
while :
do
     trap break INT
     tail -F "${1}"
done
}
doTail /var/log/messages