Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:26:21+00:00 2026-06-01T11:26:21+00:00

I need to make a bash script that takes user input and does some

  • 0

I need to make a bash script that takes user input and does some validation.

The input is small. I just need to get a user choice “yes” or “no” and a file path and make sure the input is indeed a filepath and that he entered yes or no.

How can I validate the user input?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:26:22+00:00Added an answer on June 1, 2026 at 11:26 am

    from your bash prompt type

    help read


    example:
    step 1.

    pete@ruthie ~ $ read -p “Yes or No :” ANSWER

    Yes or No :Yes

    pete@ruthie ~ $ echo $ANSWER

    Yes

    step 2.

    case $ANSWER in 
        Y* | y* ) echo "ANSWER is yes" ;; 
        N* | n*) echo ANSWER is no;; 
         *) echo "Unclear Response" ;; 
    esac
    

    ANSWER is yes

    Or all on one line:

    case $ANSWER in Y* | y* ) echo "ANSWER is yes" ;; N* | n*) echo ANSWER is no;; *) echo "Unclear Response" ;; esac
    

    ANSWER is yes

    So something vaguely like::

       read -p "Enter File Name :" FILE_TO_CHECK 
    
        if [ ! -e "$FILE_TO_CHECK" ]; then 
            echo -e "There is no file called $FILE_TO_CHECK \n please check spelling"
        else
            echo  "Congratulations found $FILE_TO_CHECK. "
             ls -lah $FILE_TO_CHECK
             read -p "Process $FILE_TO_CHECK ? Y or N : PROCEED
               case $PROCEED in
                 Y* | y*)
    ##call function or system command to do something with this data
                   foo $FILE_TO_CHECK
    
                 ;;
                * )
                 echo -e "Note $USER\n No action taken by $0 on $FILE_TO_CHECK"
                 ;;
                esac
    
        fi
    

    The test command is your friend. You really need to learn its ways ASAP.

    help [

    and

    help test

    The “help bashthing” command is a great quick reminder.

    see also: the Advanced Bash Scripting Guide (ABS)
    http://tldp.org/LDP/abs/html/

    FWIW:
    This is a very simple example and wont accommodate much in the way of unexpected user input.
    I would probably call functions for each step of this as then I could conditionally represent the user with another chance.

    The case tests nested like this is plain ugly 🙂 === hard to get neat and read later

    There are other ways to achieve all this try zenity for a GTK style GUI.

    The A.B.S is excellent but dont try to eat it all at once !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a bash script to get some podcasts. The problem is that some
I've made a simple bash script that need to keep it's super-user privileges throughout
In a bash script I need to verify that the user inputs actual numbers
I need to make sure that user can run only one instance of my
I just need some advice. I already know how to play with bash, ruby,
I am trying to write a bash script for testing that takes a parameter
I'm trying to make bash script that's a little bit too hard for me.
I need to make a radiolist in bash script using dialog interface, for example
I am trying to make a bash script that will transfer a file from
I need to create a bash script that will work on a mac. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.