bash if else 语句中出现意外的文件结束错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17155995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unexpected End of File error in if else statement
提问by Vignesh
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. But i still get the same error. Can someone help me fix that?
我也试过 fi 而不是 elseif 。但我仍然得到同样的错误。有人可以帮我解决这个问题吗?
回答by Fredrik Pihl
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
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
回答by TrueY
IMPROVED
改进
The if is syntax is not correct. In the ifthere should be a program (bashinternal or external) run, which returns an exit code. If it is 0then if is true, otherwise it is false. You can use grepor any other utility, like testor /usr/bin/[. But bashhas a built-in testand [.
if is 语法不正确。在if应该有一个程序(bash的内部或外部)的运行,它返回一个退出代码。如果是,0则如果为真,否则为假。您可以使用grep或 任何其他实用程序,例如test或/usr/bin/[。但是bash有一个内置的test和[.
So [ "$var" -eq 1 ]returns 0 if $varequals 1, or return 1 if $varnot equals 1.
因此,[ "$var" -eq 1 ]如果$var等于 1,则返回 0,如果不等于 1,则返回$var1。
In your case I would suggest to use caseinstead of if-then-elif-else-finotation.
在你的情况下,我建议使用case而不是if-then-elif-else-fi符号。
case $x in
1) something;;
2) other;;
*) echo "Error"; exit 1;;
easc
Or even use select. Example:
甚至使用select. 例子:
#!/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 casepart. Meanwhile $promincontains the string, $REPLYcontains the entered answer. It also can be used in case.
然后它检查case部分中的答案。同时$promin包含字符串,$REPLY包含输入的答案。它也可以用于case.
回答by WonderLi
I just changed your code and I think it works now.
我刚刚更改了您的代码,我认为现在可以使用了。
I think the problem is you should fiinstead 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
回答by davidstar
#! /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:
我不知道您是否需要一个 if-else 语句或两个 if 语句。上面有一个if-else。如果您需要两个 if 语句,则在“echo "$proband_file"” 行下方插入一行代码和文本:
fi
Then replace the line "elif (($promin == 2)); then" with the following code:
然后将“elif (($promin == 2)); then”行替换为以下代码:
if (($promin == 2)); then

