Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to get the user input recursively until the user provides valid input

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-30-2012
i.srini89 i.srini89 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 10 May 2012, 2:45 AM EDT
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
How to get the user input recursively until the user provides valid input

Hi,


Code:
echo "Enter file name of input file list along with absolute path : "
read inputFileList
 
if [ -f $inputFileList ]
then
    for string in `cat inputFileList`
    do 
       echo $string
    done
else
     echo " file does not exist"
fi

From the above code, if the user enters a invalid file then "file does not exist" is given and the program is done.
How to prompt the user for input continuously untill the user enters the proper file name with absolute path without exiting the program i.e. "Enter file name of input file list along with absolute path : " prompt should be given as long as the user gives a invalid filename.

Using a loop is one way. any other way or better ways?
Thanks,
Srini
Sponsored Links
    #2  
Old Unix and Linux 04-30-2012
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 17 October 2017, 6:44 AM EDT
Location: India
Posts: 1,976
Thanks: 31
Thanked 574 Times in 547 Posts
What's wrong with a loop?


Code:
#! /bin/bash
while :
do
    echo "Enter file name of input file list along with absolute path : "
    read inputFileList

    if [ -f $inputFileList ]
    then
        cat $inputFileList
        break
    else
        echo "File does not exist. Try again."
    fi
done


Quote:
Originally Posted by i.srini89 View Post

Code:
echo "Enter file name of input file list along with absolute path : "
read inputFileList
 
if [ -f $inputFileList ]
then
    for string in `cat inputFileList`
    do 
       echo $string
    done
else
     echo " file does not exist"
fi

The part highlighted in red looks redundant. You may shorten it to just cat $inputFileList
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Valid input compared with input ckozler Shell Programming and Scripting 2 07-15-2010 09:59 AM
Question about user input rrahmegni Shell Programming and Scripting 1 07-23-2009 07:39 AM
awk user input gefa Shell Programming and Scripting 17 10-30-2007 05:01 AM
Getting user input stevefox Shell Programming and Scripting 3 02-16-2007 01:09 AM
Accepting User Input scott78 Shell Programming and Scripting 1 01-13-2004 01:54 PM



All times are GMT -4. The time now is 10:57 AM.