0

I am just starting to learn UNIX and i could not figure the problem here can you please point out the error

age=30

if [[ $age -lt  18 ]]

then
        echo "not eligible"

elif  [[ $age -gt 18 && $age -lt 60 ]]
        echo "eligible"
else
        echo "stay home"
fi
exit
3
  • 5
    Always paste your script into https://shellcheck.net, a syntax checker, or install shellcheck locally. Make using shellcheck part of your development process.
    – waltinator
    Sep 30, 2021 at 3:15
  • note: you don't need the $age -gt 18 ... the first if statement takes care of that
    – jsotola
    Sep 30, 2021 at 4:20
  • @jsotola Actually, leaving out $age -gt 18 && would make a difference if age is 18, but probably your suggestion is the correct one instead of telling the 18 year olds to stay at home with the over-60ies …
    – Philippos
    Sep 30, 2021 at 5:06

1 Answer 1

4

There are a couple of issues with your script, but the most pressing one is the lack of then after the second test.

You also have an unnecessary test for $age -gt 18, which additionally introduces a logical error. You already know that $age is less than 18 at that point, and you accidentally leave out the case of $age being precisely 18. It would be better to remove the $age -gt 18 test altogether.

The script will not need the explicit call to exit at the end but should have a #!-line at the top pointing to the appropriate shell interpreter (probably bash).

You may find https://www.shellcheck.net/ a helpful site for finding the most basic errors in scripts.


#!/bin/bash

age=30

if [[ $age -lt 18 ]]; then
        echo 'not eligible'
elif [[ $age -lt 60 ]]; then
        echo 'eligible'
else
        echo 'stay home'
fi

You must log in to answer this question.

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