LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script asking for confirmation? (https://www.linuxquestions.org/questions/programming-9/shell-script-asking-for-confirmation-375475/)

bikov_k 10-21-2005 12:40 PM

shell script asking for confirmation?
 
Hi, I am writing a script to create automatically email accounts. The input is from a file passed as parameter or from pipe.
The script should extract account name and person's details from each line and then ask for confirmation before creating the account.
<Enter> to proceed, <Space> to skip, <Esc> to abort

I found some instructions and examples for getting input with 'read' and '$<' but then how do I read only one character and dump(disregard) the rest?
Also how do I check what character was entered. All of those are special characters. Should I use ASCII codes?

Thanks!

Koko

unSpawn 10-21-2005 01:42 PM

<Enter> to proceed, <Space> to skip, <Esc> to abort
Sorry I aint answering your question (involves building a wrapper) but why make it hard if you can get away easily using regular keys for [a]dd and [s]kip and the CTRL+C exit combo (Bash: trap INT)?

bikov_k 10-21-2005 02:17 PM

>Sorry I aint answering your question (involves building a wrapper) but why make it hard if you >can get away easily using regular keys for [a]dd and [s]kip and the CTRL+C exit combo (Bash: >trap INT)? [/B][/QUOTE]

Yep, I agree with you. Thanks for the help!

/bin/bash 10-23-2005 06:06 AM

Control characters make it more difficult, I would do something like this:
Code:

#!/bin/bash

read -n1 -p "<C>ontinue, <S>kip, <A>bort Please choose one. "
echo
case $REPLY in
  c | C)
  echo "You want to proceed..."
  ;;
  s | S)
  echo "You want to skip..."
  ;;
  a | A)
  echo "You want to abort..."
  ;;
  * )
  echo "You don't know what you want to do..."
  ;;
esac

NOTE: the -n option to read tells read how many characters to accept.

bigearsbilly 10-24-2005 06:06 AM

there is a shell function that does it for you.
It's called select
you give it a list, it gives you a numbered menu.

e.g:
Code:

#!/bin/bash

yes()
{
        echo doing mail thang to $account
        echo
}

no()
{
        echo skipping $account
        echo
}

for account in bill fred bert;do
     
        echo do it to $account \?
        select choice in yes no exit ;do
          $choice
          break
        done
done

[code]
l

/bin/bash 11-03-2005 08:32 AM

The thing I don't like about select is it only gives you numerical choices, e.g.
1) Continue
2) Skip
3) Abort
To me that makes it easier for the user to make a wrong choice.

bigearsbilly 11-04-2005 02:16 AM

if the user is *that* stupid then best delete them
;-)


All times are GMT -5. The time now is 05:10 AM.