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?
IMPROVED
The if is syntax is not correct. In the if
there should be a program (bash 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 bash 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
.