Bash if then else 语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53419917/
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
Bash if then else syntax
提问by csStudent
I'm trying to do test automation with a bash script using if then else statements but I'm running into a few errors. For one, when I try to execute it I'm doing something wrong with the variable assignment with j and k, because it tells me that the command j and the command k aren't found when I try to execute. How do you correctly create variables? The most confusing thing though is when I try to execute the script I get an error telling me I have an unexpected token near fi, and then it just says 'fi'. What am I doing wrong here?
我正在尝试使用 if then else 语句使用 bash 脚本进行测试自动化,但我遇到了一些错误。一方面,当我尝试执行它时,我对 j 和 k 的变量赋值做错了,因为它告诉我在我尝试执行时找不到命令 j 和命令 k。你如何正确地创建变量?但最令人困惑的是,当我尝试执行脚本时,我收到一个错误消息,告诉我在 fi 附近有一个意外的标记,然后它只是说“fi”。我在这里做错了什么?
#!/bin/bash
j = 0
k = 0
echo Test1:
echo -ne "0\nIn\nUG\n" | /u/cgi_web/Tuition/cost
echo Test2:
echo -ne "0\nOut\nUG\n" | /u/cgi_web/Tuition/cost
echo Test3:
echo -ne "0\nIn\nGR\n" | /u/cgi_web/Tuition/cost
echo Test4:
echo -ne "0\nOut\nGR\n" | /u/cgi_web/Tuition/cost
for i in {1..17}
do
echo Test$((i+4)):
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
$j = 1
echo -ne "$i\nIn\nUG\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 0 ] then
$k = 1
echo -ne "$i\nIn\nGR\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 1 ] then
$j = 0
echo -ne "$i\nOut\nUG\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 0 ] && [ "$k" -eq 1 ] then
$k = 0
echo -ne "$i\nOut\nGR\n" | /u/cgi_web/Tuition/cost
fi
done
EDIT: I figure out the variable issue with j and k, I had to remove the spaces in the statement.
编辑:我找出了 j 和 k 的变量问题,我不得不删除语句中的空格。
采纳答案by csStudent
To help anyone who might look at this for help in the future, I figured I'd answer my own question with all the syntax errors I found from my own testing and with the helpful responses of others. To start the variable assignment:
为了帮助将来可能会寻求帮助的任何人,我想我会用我在自己的测试中发现的所有语法错误以及其他人的有用回答来回答我自己的问题。开始变量赋值:
j = 0
you can't have spaces in between, so it would be:
你之间不能有空格,所以它是:
j=0
Also if statements need a space between if and the bracket and need a semicolon after the last bracket before then. Therefore my incorrect if statement
此外,if 语句需要在 if 和括号之间有一个空格,并且在 then 之前的最后一个括号之后需要一个分号。因此我不正确的 if 语句
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
becomes
变成
if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]; then
or instead of a semicolon you can have a new line between the bracket, so it would become
或者你可以在括号之间换行而不是分号,所以它会变成
if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]
then
回答by Nunchy
Bash if statements require a semi-colon before the then
:
Bash if 语句需要在 前加一个分号then
:
if [ condition ] || [ condition ]; then
# code
elif [ condition ] && [ condition ]; then
# code
fi
For example.
例如。