0

the error is at line 21...

if [ $weight1 -gt $weight2 ];
    then echo "Weight 2 should be greater than Weight 1."
else
    if [ $weight1 -ge 20 ] && [ $weight1 -le 39 ];
        then echo "Weight 1 is bike."
    else 
        echo "Weight 1 is either bicycle, car, van or lorry."
    fi
fi
3
  • 6
    Which line is 21? Commented Feb 10, 2014 at 7:36
  • 1
    The code, as posted, works for me. Please post the code that fails. Commented Feb 10, 2014 at 7:39
  • 1
    we don't know where the problem is? and where the line 21 is? Commented Feb 10, 2014 at 7:54

2 Answers 2

6

I done\'t see the issue, on my tests then keyword is worked for your case, anyway some noted on your code:

  1. Put then on the line of if operator:

    if [ $weight1 -ge 20 ] && [ $weight1 -le 39 ]; then
    
  2. Enclose your variables into quotes:

    if [ "$weight1" -ge 20 ] && [ "$weight1" -le 39 ]; then
    
  3. I recommend to use -a key, instead to two calls to [ app:

    if [ "$weight1" -ge 20 -a "$weight1" -le 39 ]; then
    

NOTE: The error Syntax error near unexpected token 'then' can rise up mostly in a few cases:

  1. When you specify then keyword outside of if case:

    weight1=21
        then echo "Weight 1 is bike."
    else
        echo "Weight 1 is either bicycle, car, van or lorry."
    fi
    
  2. when youve specified then twice:

    if [ "$weight1" -ge 20 -a "$weight1" -le 39 ]; then
    then echo "Weight 1 is bike."
    
  3. when you've put then to improper place, for example, after the else operator:

    else
    then
        echo "Weight 1 is either bicycle, car, van or lorry."
    fi
    
Sign up to request clarification or add additional context in comments.

1 Comment

@Dropout I hadn't seen ; pardon me
0

just

chmod +x test.sh

first line: add #!/bin/bash

then do:

./test.sh

It is working whithout problem

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.