You are not logged in.

#1 2009-10-29 23:44:19

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Bash script question, accepting user input while running

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

#2 2009-10-30 00:04:07

urist
Member
Registered: 2009-02-22
Posts: 248

Re: Bash script question, accepting user input while running

Ctrl+C?

Or do you want to save some result that the script produces?

Offline

#3 2009-10-30 02:32:27

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Re: Bash script question, accepting user input while running

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

#4 2009-10-30 02:42:27

sHyLoCk
Member
From: /dev/null
Registered: 2009-06-19
Posts: 1,197

Re: Bash script question, accepting user input while running

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

~ Regards,
sHy
ArchBang: Yet another Distro for Allan to break.
Blog | GIT | Forum (。◕‿◕。)

Offline

#5 2009-10-30 03:02:33

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: Bash script question, accepting user input while running

#!/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

#6 2009-10-30 03:17:59

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Re: Bash script question, accepting user input while running

sHyLoCk wrote:
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 sad.

Offline

#7 2009-10-30 03:33:26

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: Bash script question, accepting user input while running

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

#8 2009-10-30 03:38:12

sHyLoCk
Member
From: /dev/null
Registered: 2009-06-19
Posts: 1,197

Re: Bash script question, accepting user input while running

EDIT: nvm perbh's script looks good

Last edited by sHyLoCk (2009-10-30 03:38:35)


~ Regards,
sHy
ArchBang: Yet another Distro for Allan to break.
Blog | GIT | Forum (。◕‿◕。)

Offline

#9 2009-10-30 19:18:13

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Re: Bash script question, accepting user input while running

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

Board footer

Powered by FluxBB