Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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

I just changed your code and I think it works now.

I think the problem is you should fi instead of endif...

#!/bin/sh
echo "1: Proband\n2: mincount\nEnter an option:"
read promin
echo $promin
if [ $promin -eq "1" ]
then
    echo "Enter the proband file name\n"
    read proband_file
    echo "$proband_file"
elif [ $promin -eq "2" ]
then
    echo "enter the min count number\n"
    read mincount
    echo "$mincount mincount"
fi
#! /bin/bash
echo -e "1: Proband\n2: mincount\nEnter an option:"
read promin
echo $promin
if (($promin == 1)); then
echo -e "Enter the proband file name\n"
read proband_file
echo "$proband_file"
elif (($promin == 2)); then
echo -e "Enter the min count number\n"
read mincount
echo "$mincount mincount"
fi

I don't know if you need an if-else statement or two if statemnts. The above has an if-else. If you need two if statements, then insert a line of code below the "echo "$proband_file"" line with the text:

fi

Then replace the line "elif (($promin == 2)); then" with the following code:

if (($promin == 2)); then
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top