LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-21-2005, 01:40 PM   #1
bikov_k
Member
 
Registered: Sep 2004
Distribution: Fedora Core 5
Posts: 33

Rep: Reputation: 15
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
 
Old 10-21-2005, 02:42 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,699
Blog Entries: 54

Rep: Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962
<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)?
 
Old 10-21-2005, 03:17 PM   #3
bikov_k
Member
 
Registered: Sep 2004
Distribution: Fedora Core 5
Posts: 33

Original Poster
Rep: Reputation: 15
>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!
 
Old 10-23-2005, 07:06 AM   #4
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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.
 
Old 10-24-2005, 07:06 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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
 
Old 11-03-2005, 09:32 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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.
 
Old 11-04-2005, 03:16 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
if the user is *that* stupid then best delete them
;-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 07:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
Bash script - confirmation option satimis Programming 4 10-21-2004 08:29 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 04:00 PM


All times are GMT -5. The time now is 12:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration