Operating System - HP-UX
1751820 Members
4957 Online
108782 Solutions
New Discussion юеВ

Using "Ctrl-C" in Scripting

 
SOLVED
Go to solution
Ryan Bernard
Frequent Advisor

Using "Ctrl-C" in Scripting

Hi anybody know how we can use "Ctrl-C" in a script?,

It required in the script to break out from certain processes.

Thanks alot for your help~
13 REPLIES 13
Wim Rombauts
Honored Contributor
Solution

Re: Using "Ctrl-C" in Scripting

In vi, while in insert mode, first pres Ctrl-V and then Ctrl-C. That will insert the Ctrl-C in your script.
Dennis Handly
Acclaimed Contributor

Re: Using "Ctrl-C" in Scripting

If you type Control-C, your script should be aborted. If you want to trap it, you can use "trap command INTR".

If you want to send a control-C to another process you can use:
kill -INTR PID
Sagar Sirdesai
Trusted Contributor

Re: Using "Ctrl-C" in Scripting

Hi
Please use the trap command in your script

trap "exit" 2

This exit the script whenever it receives a
Ctlr C

Sagar
Dennis Handly
Acclaimed Contributor

Re: Using "Ctrl-C" in Scripting

You may have to use INT where I had INTR.
Laurent Menase
Honored Contributor

Re: Using "Ctrl-C" in Scripting

or Do you want on ctrlC kill some of children processes?



myproc1 &
firstpid=$!
myproc2 &
secondpid=$!
myproc3 &
thirdpid=$!

trap "kill -SIGINT $secondpid" 2

Then if you CtrlC it will kill only myproc2


James R. Ferguson
Acclaimed Contributor

Re: Using "Ctrl-C" in Scripting

Hi Ryan:

You can change what the trap does, too. You might want to disable the Ctrl_C interrupt (INT) in a critical region of your script's code and then change it to "normal" behavior. For example:

# cat ./myctrl_c
#!/usr/bin/sh
function paint
{
typeset -i n
while (( n < 9 ))
do
echo $1
(( n=n+1 ))
sleep 1
done
}
trap 'echo That was not allowed!' INT
paint "+"
echo "...try interrupting now"
trap 'echo CTRL_C sensed;exit' INT
paint "-"
exit 0

See the 'sh-posix' manpages for more information, too.

Regards!

...JRF...
Ryan Bernard
Frequent Advisor

Re: Using "Ctrl-C" in Scripting

Thanks alot for your recommendations. Currently trying out the recommendations.

1 more thing, how do we actually do a 60sec wait state?
Sagar Sirdesai
Trusted Contributor

Re: Using "Ctrl-C" in Scripting

Hi'Use the sleep command

sleep 60

This will wait for 60 seconds

Sagar
Sagar Sirdesai
Trusted Contributor

Re: Using "Ctrl-C" in Scripting

Hi Ryan

Please assign points if you have got the answers you needed

Sagar