[bash] how is proper way to validate user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] how is proper way to validate user input
# 1  
Old 09-24-2012
(SOLVED) [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:
Code:
    echo "type file source and it full path [e.g: /srv/src/source.csv or "/srv/src dir/my source.csv"]:"
    read INPUTFILE
    if [ -z $INPUTFILE ] || [ ! -f $INPUTFILE ];
    then
     echo "ERROR: you didn't enter a file source or file source is not exist."
     exit 1
    fi
 
    echo "enter directory for output file [e.g: /home/admin/dst/]:"
    read OUTPUTFILE
    if [ ! -d "$OUTPUTFILE ] ;
    then
     echo "ERROR: directory is no exist."
     exit 1    
    fi

    echo "enter number of header lines of $INPUTFILE :"
    read HEADERLINE
    if [ -z $HEADERLINE ];
    then
     echo "ERROR: please enter number of header line."
     exit 1
    fi
    
    tail -n +`expr $HEADERLINE + 1` $INPUTFILE > /tmp/trim0.tmp
    <next command>
    <next command>
    <next command>
    printf "%s\n" "$outputdir/summary.csv is created"

as you can see if the user fail to input the correct variable, script will exit and user should run it again and start the process from begining.

my question: what is the proper way to ask user to enter the correct variable if put wrong variable or he press enter before he provide any input?

e.g:
Code:
type file source and it full path [e.g: /srv/src/source.csv or "/srv/src dir/my source.csv"]:
<user only press enter>
ERROR: you didn't enter a file source or file source is not exist."
type file source and it full path [e.g: /srv/src/source.csv or "/srv/src dir/my source.csv"]:
/srv/src/source.csv

enter directory for output file [e.g: /home/admin/dst/]: 
/home/admin/dst/

enter number of header lines of $INPUTFILE :
<user only press enter>
ERROR: please enter number of header line.
enter number of header lines of $INPUTFILE :
3

/home/admin/dst/summary.csv is created


Last edited by makan; 09-24-2012 at 05:13 AM..
# 2  
Old 09-24-2012
use a while loop.

Code:
while true
do
   echo "enter directory for output file [e.g: /home/admin/dst/]:"
    read OUTPUTFILE
    if [ ! -d $OUTPUTFILE ]  ;
    then
         echo "ERROR: invalid directory"
     else
         echo "Directory validated successfully.."
         break
     fi
done

hope this helps
This User Gave Thanks to Arun_Linux For This Post:
# 3  
Old 09-24-2012
oic....

thank you for your help Arun_Linux Smilie

:: update ::
correction for my own script:
1.
Code:
 echo "type file source and it full path [e.g: /srv/src/source.csv or "/srv/src dir/my source.csv"]:"

should change to
Code:
 echo "type file source and it full path [e.g: /srv/src/source.csv or /srv/src dir/my\ source.csv]:"

2.
Code:
if [ -z $INPUTFILE ] || [ ! -f $INPUTFILE ];

should change to
Code:
if [ -z "${INPUTFILE}" ] || [ ! -f "${INPUTFILE}" ];

those will help me to handle source filename that contain space.

Last edited by makan; 09-24-2012 at 06:05 AM.. Reason: correction
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

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

5. 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

6. 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

7. Shell Programming and Scripting

Bash Script verify user input is not empty and is equal to a value

I need to create a script that has a user enter a value. I want to verify that the value is either 1,2, or 3. If it is not then I want them to try entering it again. I am using a while loop to force them to retry. I am able to test the input against 1,2, and 3, but when I test agains an... (4 Replies)
Discussion started by: spartiati
4 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. UNIX for Dummies Questions & Answers

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: read answer case $answer in *) echo "OK" ;; *) echo "This is a number" read answer ;; *) echo... (2 Replies)
Discussion started by: 602chrislys
2 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