简体   繁体   English

if 语句中的 bash 运算符

[英]bash operators in if-statement

Im new to bash and can't understand how the operator quite work here.我是 bash 新手,无法理解操作员在这里的工作方式。 The goal is to creating a script which ask user to input "y" to run the command or "n" to not run.目标是创建一个脚本,要求用户输入“y”来运行命令或“n”不运行。

by using == :通过使用==

All inputs get passed such as (y, n, a, abc, etc..)所有输入都被传递,例如 (y, n, a, abc, etc..)

if (($TFENGINE_VAR==y))

by using = :通过使用=

All inputs get rejected such as (y, n, a, abc, etc..)所有输入都被拒绝,例如 (y, n, a, abc, etc..)

if (($TFENGINE_VAR=y))

echo "run tfengine? (y/n)"
read TFENGINE_VAR
if (($TFENGINE_VAR==y))
then
  echo "running tfengine command.."
  sleep 1
  tfengine --config_path=main.hcl --output_path=terraform/ -delete_unmanaged_files
else
  echo "continue.."
  continue
fi

Expressions surrounded with (( )) are evaluated as numeric expressions.(( ))包围的表达式被计算为数值表达式。

You need to use the test command, which returns 0 if test is successful, and a number different from 0 if not.您需要使用test命令,如果测试成功则返回0 ,如果不成功则返回一个与0不同的数字。

There are several options to the test command. test 命令有几个选项。 In your case, you want to compare strings, so use each parameter surrounded with double quotes.在您的情况下,您想要比较字符串,因此请使用用双引号括起来的每个参数。

Also, when comparing strings you don't use == as the comparison operator.此外,在比较字符串时,您不使用==作为比较运算符。

if test "$TFENGINE_VAR" = "y"
then
   ...

Take a look at test man page: http://man.he.net/?topic=test&section=all看看测试手册页: http ://man.he.net/?topic=test&section=all

Your question is:你的问题是:

how the operator quite work here操作员在这里如何工作

The ((...)) is an arithmetic expression. ((...))是一个算术表达式。 Inside, all strings are interpreted as variables, and undefined variables are interpreted as equal 0. The exit status of ((...)) is non-zero if the expression is equal to zero, and the exit status is zero if the expression inside is equal to non-zero.在内部,所有字符串都被解释为变量,未定义的变量被解释为等于 0。如果表达式等于 0,则((...))的退出状态为非零,如果表达式等于 0,则退出状态为零inside 等于非零。

Because y variable is not defined, y is equal to 0. The (( $TFENGINE_VAR == y )) does the same as (( $TFENGINE_VAR == 0 )) .因为未定义y变量,所以y等于 0。 (( $TFENGINE_VAR == y ))(( $TFENGINE_VAR == 0 ))相同。

If you want to compare strings , you would use [[ or [ or test command.如果要比较字符串,可以使用[[[test命令。

All inputs get rejected such as所有输入都被拒绝,例如

Sure, = is assignment inside arithmetic expression.当然, =是算术表达式中的赋值。 If you input abc , then (( $TFENGINE_VAR = y )) becomes (( abc = 0 )) and it assigns the value 0 to variable abc .如果您输入abc ,则(( $TFENGINE_VAR = y ))变为(( abc = 0 ))并将值0分配给变量abc Note that variable expansion happens before the expansion of variables inside (( .请注意,变量扩展发生(( .

$ TFENGINE_VAR=abc
$ (($TFENGINE_VAR=y))
$ echo $abc
0
$ y=12345
$ (($TFENGINE_VAR=y))
$ echo $abc
12345

The result of assignment is equal to the value assigned, 0 in this case, so ((...)) exits with non-zero exit status, failure, so if branches to else part.分配的结果等于分配的值,在这种情况下为0 ,因此((...))以非零退出状态退出,失败,因此if分支到else部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM