Skip to main content
added 196 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

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

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.

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
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

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.