0

I am looking to create a shell script which can take (read) 1 and 2 as inputs. I tried this below script but something looks to be missing. Here are my requirements :

  1. It should echo when no input is passed.
  2. It should accept values 1 or 2
  3. It should throw error if any other value is passed other that 1 or 2.
  4. when any one of above cases is not met it should go back to read and ask user for input again.

I might be wrong using while loop. But this is what I acheived so far.

#!bin/sh
while :
echo "enter value for a:";read -r a;  do
         if [ -z "${a}" ]; then
         echo "That was empty, do it again!"
        elif [ "${a}" -eq "1" ]  || [ "${a}" -eq "2" ]; then
                ## echo "Checking now..."
                echo "value passed is :$a"
                echo "Condition success"
             elif [ "${a}" -ge "3" ]
        then
        echo "Select either option :1 or :2 "
    fi
break
done

Output

When I pass 1

. Test_input2.sh
enter value for a:
1
value passed is :1
Condition success

When I pass nothing

. Test_input2.sh
enter value for a:

That was empty, do it again!

I want the be able to prompt the user for input untill he gives me the expected input i.e. 1 or 2.How can I put this into loop ?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Use a `case` statement; it'll make your life much easier. – Charles Duffy Dec 05 '16 at 21:41
  • ...beyond that -- if you want to be writing a `sh` script, use `#!/bin/sh` as your shebang. If you want to be writing a bash script, use the `bash` tag, not the `sh` tag. – Charles Duffy Dec 05 '16 at 21:42
  • ...and follow http://stackoverflow.com/help/how-to-ask and http://stackoverflow.com/help/mcve -- include not just code, but a description of *exactly* what's wrong, and take out any parts that aren't necessary to show the problem. (Does your `read` command work? Then just take it out, and hardcode a number. Does your `while` loop work? Then take it out, and only run once; etc). – Charles Duffy Dec 05 '16 at 21:43
  • yes it works. This is what I get when I run my script. Linux1$ . Test_input1.sh enter value for a: 5 -bash: [: missing `]' out of the loop we go – Arvind Toorpu Dec 05 '16 at 22:14
  • yup, you're missing whitespace there: that is, you're passing `3]` as a single argument, not passing `]` as the argument after `3` as it needs to be. http://shellcheck.net/ will find this class of problems for you without having humans involved. – Charles Duffy Dec 05 '16 at 22:16
  • (It's important to realize that `[` is not syntax, it's a *command*; there's actually a `/bin/[` on your disk, and the shell builtin by the same name operates with the same semantics. So just as you can't run `ls/tmp` instead of `ls /tmp`, you can't run `["$foo"` instead of `[ "$foo"`). – Charles Duffy Dec 05 '16 at 22:18
  • ...and your clarifications as to what the actual error is? Those need to be **in the question**, not in comments. The "edit" button is there for a reason; use it. – Charles Duffy Dec 05 '16 at 22:19
  • There is no edit button to edit question or may be I cant find it. – Arvind Toorpu Dec 05 '16 at 22:36
  • After the question text, below the tags, above the comments, next to "share". – Charles Duffy Dec 05 '16 at 22:39
  • ...so, now that you fixed the error I pointed out, it's unclear to me what you're asking. Is the "output" given your current output, or your intended output? It might make sense to go back and review the prior links (re: "how to ask" and MCVE documentation). – Charles Duffy Dec 05 '16 at 23:16
  • ...if what you want to know is how to break out of the loop, that's what `break` is for -- put it in whatever condition you want to... well... break out of the loop on, and don't call it *except* then. – Charles Duffy Dec 05 '16 at 23:19
  • It is always exiting the loop because you have a break statement just before the done. – Jerry Jeremiah Dec 05 '16 at 23:19
  • Closed this as a duplicate. If you edit it in a way that makes it clearly a unique question that's helpful to others, let me know, and I'll revert that. – Charles Duffy Dec 05 '16 at 23:22
  • Charles Duffy, how can this be a duplicate as I my intent was not to just learn how to break out of script ?? I have clearly mentioned everything out in my question. How ever I found the error in my script. Thanks for your help. – Arvind Toorpu Dec 06 '16 at 15:01

0 Answers0