2

We have to make a script with one until loop to repeatedly ask the user for a number. - If the number is not 50, display the message, “Wrong number; try again.” - ask for another number. - If the number is 50, display the message, “You got it!” and stop.

Here is what I have so far:

    echo -n "please choose a number: "
    read number
    until [$number -eq 50];
    do
         if [$number !=50]
         then 
               echo "Wrong number; try again" 
               read wrong 
         else 
               echo -n "Please choose a number: "
               read newnum
         fi
    done 

I'm not sure how to loop the "Please choose a number" statement. I always end up with the error:

      syntax error near unexpected token 'fi' 

I've been looking online, but there are no clear examples of input output UNTIL loops

10
  • How did you try?
    – cuonglm
    Nov 17, 2014 at 7:37
  • 1
    Yeah you can, probably. Nov 17, 2014 at 7:38
  • 2
    You have many problems with your script: missing space around arguments of [ ... ] constructs, you wrote number to get the value of the variable instead of $number, you use a different variable name later newnum instead of number, you don't put double quotes around tests like so: "$number". You need to follow a shell scripting tutorial
    – Celada
    Nov 17, 2014 at 7:47
  • I input my code that I tried running. I'm sorry I'm a beginner at Shell Scripting. Nov 17, 2014 at 7:48
  • @Celada I'm not sure where to begin. The examples my professor showed us do not look at all like what we're supposed to be doing. Do you know of any shell scripting tutorial I can see? I've been looking at all the ones that contain UNTIL loops. Nov 17, 2014 at 7:53

5 Answers 5

4
echo -n "please choose a number: "

read number
until [ $number -eq 50 ]
do
     # if [ $number -ne 50 ]
     # then 
           echo "Wrong number; try again" 
           read number 
     # fi
done 

echo You got it\!

The first read reads into variable number. The until loop loops until $number equals 50.

The test inside the loop is unnecessary , since the until only enters when the number is -ne (not equal to) 50.

The main problem you had was that read inside the loop must update the same variable (number) as the until construct checks.

The spacing is important too, because the word (surrounded by whitespace) after if is the name of a command. [$number evaluates to whatever was inputed, but prefixed by '[', which is most likely not the name of an existing command. For instance, if the first guess was 42, the if command would try to execute the command [42, causing an error like [42: not found.

Also, comparing numbers is done with -eq, -lt, -ge, -gt, -ge, -ne.

= and =! are for strings.

3
  • Let me to advise remove 1st read (3rd line) - insid'e the loop is enough if you remain if for echo "Wrong...".
    – Costas
    Nov 17, 2014 at 10:11
  • 1
    @Costas: I think having the extra read statement outside the loop is cleaner than testing number twice per loop.
    – PM 2Ring
    Nov 17, 2014 at 10:19
  • @MattBianco: You should put " around $number - it makes the error message nicer if the user enters the empty string or a string containing more than one word. Also, if you put " around the final echo arg you don't need to escape the !
    – PM 2Ring
    Nov 17, 2014 at 10:23
0

In the other answer you got some more general advise on your program, so I will point you to the actual mistake, which is in the line with the if

if [$number !=50]

It should be:

if [ $number -ne 50 ]

I changed three things here

  • I replaced != with -ne. The first one is invalid
  • I added a space between [ and $number. (Check by correcting the first step what happens if you don't).

Of course you get a weird program with unexpected behavior, but it'll run :)

0

I'd do it like this:

#!/usr/bin/env bash

while :; do
    read -p "Please choose a number: " number
    [ "$number" -eq 50 ] 2>/dev/null
    case $? in
        0) break ;;
        1) echo "Wrong number; try again." ;;
        2) echo "Not a valid number; try again." ;;
    esac
done

echo "You got it!"

Where while can be replaced with until, if desired. But I get the feeling that the professor would not be pleased if musicstrings submitted this program. :)

0

Another solution, concise.

read -p "please choose a number: " number

until ((number == 50)); do
    read -p "Wrong number; try again : " number
done

echo "You got it!"
0

Your syntax error near unexpected token 'fi' cannot be explained with the code you posted unless both then and else are followed by invisible characters like a CR character (as when files come from Microsoft OS where the line delimiter is CRLF instead of just LF. But then if it were a DOS formatted file, I'd expect other error messages because of the CR after do and done.

Edit: looking at the revision history of your question, the error seems to correspond to a mistake in a prior version of your script.

You have a number of errors with your [ command syntax, unquoted variables, as already noted, but I'd like to add that the syntax of an until loop is:

until
  condition-command-list
do
  action-command-list
done

The condition-command-list can be any number of commands like the action-command-list, so you can do

until
  printf 'please choose a number: '
  read number || [ -n "$number" ] || exit
  [ "$number" -eq 50 ]
do
  echo >&2 'Wrong number; try again'
done

The || exit part is to exit the script upon EOF (otherwise it would loop forever if the script's stdout was closed as when you run echo 49 | your-script).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .