You are not logged in.
I've written a bash script and would like to add the feature of allowing the user to quit whenever he/she wants (i.e. hitting 'q' exits the script at any point). I've searched around but am having trouble finding the answer to my problem.
-SMRT
Offline
Ctrl+C?
Or do you want to save some result that the script produces?
Offline
I want to be able to have the script perform an action and exit in response to the user input. I've tried "trap" but if I use ctrl-c in another terminal, it executes the trap command, which is rather annoying.
Offline
I want to be able to have the script perform an action and exit in response to the user input. I've tried "trap" but if I use ctrl-c in another terminal, it executes the trap command, which is rather annoying.
What about using case?
echo "Do you want to quit ? (y/n) : "
read data
case "$data" in
[nN]*) echo " Continuing...";;
[yY]*) exit ;;
*) echo "Bad option!"
esac
Offline
#!/bin/bash
.
.
cleanup() {
# do whatever you need to do to clean up the act
}
trap "{ cleanup; exit 1; }" SIGHUP SIGINT SIGTERM SIGQUIT SIGKILL
.
.
ooops I'm sorry - didn't notice you had allready tried trap ...
Last edited by perbh (2009-10-30 03:04:24)
Offline
iamsmrt wrote:I want to be able to have the script perform an action and exit in response to the user input. I've tried "trap" but if I use ctrl-c in another terminal, it executes the trap command, which is rather annoying.
What about using case?
echo "Do you want to quit ? (y/n) : " read data case "$data" in [nN]*) echo " Continuing...";; [yY]*) exit ;; *) echo "Bad option!" esac
That's a possibility, don't have time to try it atm but I forsee a problem. I have a bunch of sleep commands in the script, so would I be able to do something like:
read asdf
case $asdf
...
...
sleep 30m
And still have it respond to the input while it's "sleeping"? Hell, here's my script:
#!/bin/bash
# A program written while heavily procrastinating to help fight procrastination. Turns off the internet for t=STUDY then on again for t=BREAK, music playback in banshee is toggled to let the user know that the new study/break period has begun.
STUDY=30m
BREAK=10m
ETH0=$(/sbin/ifconfig | awk '/eth0/,/inet addr/' | wc -l)
WLAN0=$(/sbin/ifconfig | awk '/wlan0/,/inet addr/' | wc -l)
# Check if ethernet is in use
if [ $ETH0 -eq 2 ]; then
TRUTH=0
trap '/sbin/ifconfig eth0 up; exit' INT TERM EXIT
fi
# Check if wireless is in use
if [ $WLAN0 -eq 2 ]; then
TRUTH=1
trap '/sbin/ifconfig wlan0 up; exit' INT TERM EXIT
fi
# Study loop (ethernet)
while [ $TRUTH -eq 0 ]; do
echo ---STUDY TIME---
/sbin/ifconfig eth0 down
# banshee-1 --toggle-playing
sleep $STUDY
echo ---BREAK TIME---
/sbin/ifconfig eth0 up
# banshee-1 --toggle-playing
sleep $BREAK
done
# Study loop (wireless)
while [ $TRUTH -eq 1 ]; do
echo ---STUDY TIME---
/sbin/ifconfig wlan0 down
# banshee-1 --toggle-playing
sleep $STUDY
echo ---BREAK TIME---
/sbin/ifconfig wlan0 up
# banshee-1 --toggle-playing
sleep $BREAK
done
I really wish CTRL-C in other terminals wouldn't trip the trap .
Offline
well, you _could_ use trap to catch the signals (which will happen even when you are sleeping) - you don't neccessarily have to exit.
If you want to wake it up on ctrl/c but ignore the others, then that's easy enough ...
trap 'echo "I am sleeping, dont disturb ... zzzzzz";' SIGHUP SIGQUIT SIGTERM SIGKILL
trap 'wakeup;' SIGINT
wakeup() [
echo -en "do you want to exit (y/n)? " >&2
read a
case $a in
y*|Y*) exit 1;;
*) ;;
esac
}
Last edited by perbh (2009-10-30 03:35:23)
Offline
Offline
Ok, I'm not sure what's going on anymore. If you look at my script, I am using a trap and everything works fine and dandy until I open another terminal and hit CTRL-C. For some reason, this activates the trap, when I clearly don't want it to. I wrote a script to test the behavior of trap and well, I can't repeat the behavior, not really sure why.
I guess my temporary solution will be to run it in the background and kill it instead. Not really how I want the script to run but it's better than nothing I suppose.
Last edited by iamsmrt (2009-10-30 19:18:35)
Offline