bash 一个简单的“if”语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10646465/
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
A simple 'if' statement
提问by imMAW
My problem can be simplified down to making the following script work (which takes one command line argument):
我的问题可以简化为使以下脚本工作(需要一个命令行参数):
#!/bin/bash
if ["" == "0"]; then
echo "good"
else
echo "bad"
fi
This should print goodwhen I run script 0, but I can't get it to. I've tried various combinations of quotes around the numbers, and I've tried =, ==, and -eq. So... bash, how does it work?
当我运行script 0 时,这应该打印得很好,但我无法做到。我已经尝试了数字周围的各种引号组合,我也尝试过 =、== 和 -eq。那么... bash,它是如何工作的?
回答by David W.
The [is actually a command. Do a ls /bin/[or an ls /usr/bin/[. You'll see it's actually an executable file.
该[实际上是一个命令。做一个ls /bin/[或一个ls /usr/bin/[。你会看到它实际上是一个可执行文件。
The [...]is from the old Bourne shell days. The ifcommand executes the statement, and if the exit code of that statement is a zero, the statement is considered trueand the if clause is executed. If the exit code is not zero, the else clause is executed (if present).
这[...]是来自旧的伯恩贝壳时代。该if命令执行该语句,如果该语句的退出代码为零,则认为该语句为真并执行 if 子句。如果退出代码不为零,则执行 else 子句(如果存在)。
Try these:
试试这些:
$ date
Fri May 18 00:04:03 EDT 2012
echo $? #Prints the exit code of the date command
0
$ date -Q #Shouldn't work, I hope...
date: illegal option -- q
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
$ echo $? #Exit code for the date command
1
You can see that dateis a valid command and returns an exit code of 0 (the value of $?), but date -Qisn't valid, and returns an exit code of 1.
您可以看到这date是一个有效命令并返回退出代码 0( 的值$?),但date -Q无效并返回退出代码 1。
Now let's try them in the ifstatement:
现在让我们在if语句中尝试它们:
if date
then
echo "I've successfully printed out the date!"
else
echo "I made a mistake in the command"
fi
Now try this:
现在试试这个:
if date -q
then
echo "I've successfully printed out the date!"
else
echo "I made a mistake in the command"
fi
Originally, the [...]was an alias for the testcommand. The following are equivalent:
最初,这[...]是test命令的别名。以下是等效的:
if test -f /bin/ls #Does a file called /bin/ls exist?
then
echo "There's a /bin/ls file"
fi
and
和
if [ -f /bin/ls ]
then
echo "There's a /bin/ls file"
fi
This is why it's very important to put spaces around the [and ]. Because these are actually commands. In BASH, there's built into the shell, but they are commands. That's also why all the test parameters (things like -f, -z, and -eq) all are prefixed with dashes. They were originally parameters for the testcommand.
这就是为什么在[和周围放置空格非常重要的原因]。因为这些实际上是命令。在 BASH 中,shell 内置,但它们是命令。这也是为什么所有的测试参数(东西喜欢-f,-z和-eq)所有与破折号前缀。它们最初是test命令的参数。
回答by Tiago Peczenyj
Use a space between the bracket and the argument
在括号和参数之间使用空格
$ cat x
#!/bin/bash
if [ "" == "0" ]; then
echo "good"
else
echo "bad"
fi
$ bash x 0
good
回答by cdarke
Use double parentheses for arithmetic comparisons, then you don't need to worry about quotes and spacings, for example:
使用双括号进行算术比较,那么就不用担心引号和空格了,例如:
#!/bin/bash
if (( == 0)); then
echo "good"
else
echo "bad"
fi
General rule: use (( ))for arithmetics and [[ ]]for text and patterns.
As others have said, [ is old Bourne shell syntax and there are few reasons for using it any more.
一般规则:(( ))用于算术以及[[ ]]文本和模式。
正如其他人所说,[ 是旧的 Bourne shell 语法,几乎没有理由再使用它了。

