[英]unexpected End of File error in if else statement
I keep getting unexpected End of file error while running a if else statement 运行if else语句时,我不断收到意外的文件结尾错误
#! /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. 我也尝试了fi而不是elseif。 But i still get the same error.
但是我仍然遇到同样的错误。 Can someone help me fix that?
有人可以帮我解决这个问题吗?
This is how you write an if-statement in bash: 这是在bash中编写if语句的方式:
if - then - fi 如果-那么-fi
if [ conditional expression ]
then
statement1
statement2
fi
if - then - else - fi 如果-那么-否则-FI
If [ conditional expression ]
then
statement1
statement2
else
statement3
statement4
fi
if - then - elif - else - fi 如果-则-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
IMPROVED 改善的
The if is syntax is not correct. if is语法不正确。 In the
if
there should be a program ( bash internal or external) run, which returns an exit code. 在,
if
应该有一个程序(内部或外部bash )运行,则返回退出代码。 If it is 0
then if is true, otherwise it is false. 如果为
0
,则为true,否则为false。 You can use grep
or any other utility, like test
or /usr/bin/[
. 您可以使用
grep
或任何其他实用程序,例如test
或/usr/bin/[
。 But bash has a built-in test
and [
. 但是bash具有内置的
test
和[
。
So [ "$var" -eq 1 ]
returns 0 if $var
equals 1, or return 1 if $var
not equals 1. 所以,
[ "$var" -eq 1 ]
如果返回0 $var
等于1,或者如果返回1 $var
不等于1。
In your case I would suggest to use case
instead of if-then-elif-else-fi
notation. 在您的情况下,我建议使用
case
代替if-then-elif-else-fi
表示法。
case $x in
1) something;;
2) other;;
*) echo "Error"; exit 1;;
easc
Or even use select
. 甚至使用
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或2或^ D-完成输入)。
1) Proband
2) mincount
Enter an option: _
Then it checks the answer in the case
part. 然后,检查
case
部分的答案。 Meanwhile $promin
contains the string, $REPLY
contains the entered answer. 同时
$promin
包含字符串, $REPLY
包含输入的答案。 It also can be used in case
. 也可以用于
case
。
I just changed your code and I think it works now. 我刚刚更改了您的代码,我认为它现在可以工作。
I think the problem is you should fi instead of endif ... 我认为问题是您应该使用fi而不是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. 我不知道您是否需要if-else语句或两个if statemnts。 The above has an if-else.
上面有一个if-else。 If you need two if statements, then insert a line of code below the "echo "$proband_file"" line with the text:
如果需要两个if语句,则在“ echo“ $ proband_file”“行下方插入以下文本行:
fi
Then replace the line "elif (($promin == 2)); then" with the following code: 然后将“ elif(($ promin == 2));然后”行替换为以下代码:
if (($promin == 2)); then
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.