unexpected End of File error in if else statement

13,620

Solution 1

IMPROVED

The if is syntax is not correct. In the if there should be a program ( internal or external) run, which returns an exit code. If it is 0 then if is true, otherwise it is false. You can use grep or any other utility, like test or /usr/bin/[. But has a built-in test and [.

So [ "$var" -eq 1 ] returns 0 if $var equals 1, or return 1 if $var not equals 1.

In your case I would suggest to use case instead of if-then-elif-else-fi notation.

case $x in 
1) something;;
2) other;;
*) echo "Error"; exit 1;;
easc

Or even use select. Example:

#!/bin/bash

PS3="Enter an option: "
select promin in "Proband" "mincount";do [ -n "$promin" ] && break; done
echo $promin

case "$promin" in
  Proband) read -p "Enter the proband file name: " proband_file; echo "$proband_file";;
  mincount) read -p "Enter the min count number: " mincount; echo "$mincount mincount";;
  *) echo Error; exit 1;;
esac

This will print the "Enter an option: " prompt and wait until a proper answer is presented (1 or 2 or ^D - to finish the input).

1) Proband
2) mincount
Enter an option: _

Then it checks the answer in the case part. Meanwhile $promin contains the string, $REPLY contains the entered answer. It also can be used in case.

Solution 2

This is how you write an if-statement in bash:

if - then - fi

if [ conditional expression ]
then
    statement1
    statement2
fi

if - then - else - fi

If [ conditional expression ]
then
    statement1
    statement2
else
    statement3
    statement4
fi

if - then - elif - else - fi

If [ conditional expression1 ]
then
    statement1
    statement2
elif [ conditional expression2 ]
then
    statement3
    statement4
else
    statement5
fi

Example of a conditional expression:

#!/bin/bash
count=100
if [ $count -eq 100 ]
then
  echo "Count is 100"
fi
Share:
13,620
Vignesh
Author by

Vignesh

Updated on June 05, 2022

Comments

  • Vignesh
    Vignesh almost 2 years

    I keep getting unexpected End of file error while running a if else statement

    #! /bin/bash
    echo -e "1: Proband\n 2: mincount\n Enter an option:"
    read promin
    echo $promin
    if ($promin == 1) then
    echo -e "Enter the proband file name\n"
    read proband_file
    echo "$proband_file"
    endif
    if ($promin == 2) then
    echo -e "enter the min count number\n"
    read mincount
    echo "$mincount mincount"
    endif
    

    I tried fi instead of elseif too. But i still get the same error. Can someone help me fix that?

    • SethMMorton
      SethMMorton almost 11 years
      This looks like csh. It might work if you change the shebang at the top to #! /bin/csh.
  • Vignesh
    Vignesh almost 11 years
    I followed the structure and now i get ./testtest.sh: line 11: [1: command not found ./testtest.sh: line 17: [1: command not found
  • Fredrik Pihl
    Fredrik Pihl almost 11 years
    There are many errors in your script. See added example on howto compare integers. I.e. bash do not understand ==. Type help test for a list of all the comparison operators you can use.
  • Fredrik Pihl
    Fredrik Pihl almost 11 years
    Note that spaces are vital!
  • Gordon Davisson
    Gordon Davisson almost 11 years
    @Vignesh: [1: command not found means you're missing the space between the [ command (yes, it's a command) and its first argument, 1. You also need spaces between operators and arguments, and before the final ]. Also, you should almost always put variable references in double-quotes; if $promin is blank or contains multiple words, it'll confuse the test horribly; but "$promin" doesn't have this problem.