BASH validate user input


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers BASH validate user input
# 1  
Old 07-27-2009
BASH validate user input

Hey, im trying to validate a user input and need some help. The input needs to be just a single letter. Im using a case to so this eg:

Code:
read answer
case $answer in
     [A-Za-z]*) echo "OK"
        ;;
     [0-9]*) echo "This is a number"
                read answer
        ;;
        *)   echo "Please enter a letter ! !"
              read answer
        ;;
esac

I need to stop multiple letters and repeat letters being entered. Any ideas?? Thanks for any help.
# 2  
Old 07-27-2009
this appears to be much the same as the question asked in

https://www.unix.com/unix-dummies-que...-alphabet.html

you guys doing the same course ;-)

And to answer your question...you could do it in a variety oif ways...maybe you could look at the length of the input...or use dd to "read" in one char at a time...etc
# 3  
Old 07-28-2009
Ha thanks, that sorted it out!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to validate user's input..?

$Input_filename=$ARGV; if (!-d $Input_filename && ! -e $Input_filename) { print "USAGE: Please enter '$ABCD/def/dsed.txt' as an arguement \n"; exit; } 1. Input Is suppose to be something like "$ABCD/def/dsed.txt". if the input is wrong the script should throw an ERROR message.... (2 Replies)
Discussion started by: Rashid Khan
2 Replies

2. Shell Programming and Scripting

BASH - read does not wait for user input in some circumstances

Hello. I am running 2 scripts : script_1 and script_2 These scripts are run as root Script 2 contains : #!/bin/bash # # ~/bin/script_2 # E_BAD_PARAM=115 # date2stamp () { date --date "$1" +%Y-%m-%d___%H:%M:%S } # USER_NAME=$1 NB_PARAM=$# PARAM0=$0 (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Using read to prompt for editable user input in Bash 3

Below is a simple script to prompt for user input while suggesting an editable default value at the prompt: shortname=user1 read -e -i $shortname -p "Please enter the username you would like to add: " input USERNAME="${input:-$shortname}" Please enter the username you would like to add:... (3 Replies)
Discussion started by: woodson2
3 Replies

4. Shell Programming and Scripting

[bash] how is proper way to validate user input

hi all, i have a script that need user input provide all variables that needed to complete a job. this is my current script: echo "type file source and it full path :" read INPUTFILE if || ; then echo "ERROR: you didn't enter a file source or file source is not... (2 Replies)
Discussion started by: makan
2 Replies

5. Shell Programming and Scripting

Help with Bash user input

I am starting to learn how to use bash and I would like the script to do the following: Asks the user for his/her name Asks the user for one number Asks the user for another number Then it adds the two numbers, Also multiply the two numbers I have the part where you get the name, and I... (3 Replies)
Discussion started by: boyboy1212
3 Replies

6. Shell Programming and Scripting

Is there a simpler way to validate user input for float?

I'm trying to only read price (FLOAT (i.e 1.10, 3.14, etc etc)) If the input is just an integer, I will add a .00 behind. (i.e 3 becomes 3.00 , 20 becomes 20.00) If the input is without 2 decimal places, I'll add a 0. (i.e 3.1 becomes 3.10) I tried using the below code, it works but I don't... (6 Replies)
Discussion started by: andylbh
6 Replies

7. Shell Programming and Scripting

Bash user input

Hi all, I currently have a script which uses read -p for user interaction. e.g. read -p "New user? " user Is it possible to have it so if the user enters nothing and just presses return it can resort to a specified value instead? Thanks! :) (5 Replies)
Discussion started by: JayC89
5 Replies

8. Shell Programming and Scripting

BASH Varible nesting and user input

Well, I think I've managed to take two different issues and conglomerate them into and embarrasing mess. #!/bin/bash # Set some variables dir1=/path/that/isnt/variable/$variabledir/dir/ dir2=/path/that/isnt/variable/$variabledir/important/"$variabledir"-subdirectory/path/ echo "Gimme... (7 Replies)
Discussion started by: karlp
7 Replies

9. Shell Programming and Scripting

Bash : how do i check the user input and make sure is only chracter or only number ?

Bash : how do i check the user input and make sure is only character or only number ? (7 Replies)
Discussion started by: CheeSen
7 Replies

10. Shell Programming and Scripting

validate input from user for file name

Hello, This may have been addressed already somewhere, however I am looking for the easiest/shortest way to validate a response from a user for a file name. The file name should not have any of the following characters ~`!@#$%^&*()_-+={|\:;"'<,>.?/ Further the response should not have any... (2 Replies)
Discussion started by: jerardfjay
2 Replies
Login or Register to Ask a Question