在BASH简单的逻辑运算符 [英] Simple logical operators in BASH

查看:189
本文介绍了在BASH简单的逻辑运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个变量和我要检查以下条件(用文字写出来,然后我在bash脚本编程失败的尝试):

 如果翻等于1,(varB等于T1或varB等于T2),那么做一点事完成。

在我失败的尝试,我想出了:

  IF(($翻== 1))及和放大器; ((($ varB ==T1))||(($ VARC ==T2)));
  然后
    规模= 0.05
  科幻


解决方案

你写的究竟是什么几乎工作(如果所有的变量是编号,它的工作),但它不是一个习惯的方法都没有。


  • (...)括号中指明的子shell 。里面有什么他们并非当然pression像许多其他语言。这是命令(就像括号外)的列表。这些命令在一个单独的子进程执行,因此,任何重定向,分配等的括号内进行具有括号外没有任何影响。

    • 拥有国内领先的美元符号, $(...)是的命令替换:有括号内的命令,该命令的输出用作经过加时赛扩展命令行(的一部分,除非替代是双引号之间,但这是<一个href=\"http://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters\">another故事)。


  • {...} 括号就像在括号中,他们组的命令,但他们只影响分析,而不是分组。该方案 X = 2; {X = 4; };回声$ X 打印4,而 X = 2; (X = 4);回声$ X 打印2.(括号也要求他们周围的空间和关闭之前一个分号,而括号没有。这只是一个语法怪癖。)

    • 拥有国内领先的美元符号, $ {VAR} 是的参数扩展,扩展到一个变量的值,可能出现的额外的转换。


  • ((...))双括号包围的的算术指令,就是对整数运算,具有类似的语法其他编程语言。这种语法主要用于分配和条件语句。

    • 同样的语法在运算前pressions使用 $((...)),它扩展到前pression的整数值。


  • [...] 双括号环绕的有条件恩pressions 的。有条件的前pressions大多建在运营商 -n $变量来测试一个变量是空的, -e $文件来测试,如果一个文件存在。还有字符串相等运算符:$字符串1=$字符串2(提防右手边是一个模式,例如 [ $ foo的= A *如果 $ foo的开始] 测试一个,而 [$富=A *]] 测试,如果 $ foo的完全 A * ),和熟悉的 &安培;!&安培; || 运营商否定,合取和析以及为分组括号。请注意,您需要周围的每个操作的空间(如 [$ X=$ Y]] ,而不是 [ $ X=$ Y]] 空格或像字符),和; 内外的括号(如 [[-n $ foo的] ,而不是 [ - N $ foo的] )。

  • [...] 单肘板与更多的怪癖(但老年人和更便携)有条件的前pressions的另一种形式。不要写任何现在;开始担心他们,当你发现包含它们的脚本。

这是写在bash测试中的惯用方式:

 如果[[$ VARA = 1&安培;&安培; ($ varB =T1|| $ VARC =T2)]];然后

如果您需要移植到其他炮弹,这将是方式(注意额外的报价和独立组括号周围的每一个人测试):

 如果[$ VARA= 1]安培;&安培; {[$ varB=T1] || [$ VARC=T2]; };然后

I have a couple of variables and I want to check the following condition (written out in words, then my failed attempt at bash scripting):

if varA EQUALS 1 AND ( varB EQUALS "t1" OR varB EQUALS "t2" ) then 

do something

done.

And in my failed attempt, I came up with:

if (($varA == 1)) && ( (($varB == "t1")) || (($varC == "t2")) ); 
  then
    scale=0.05
  fi

解决方案

What you've written actually almost works (it would work if all the variables were numbers), but it's not an idiomatic way at all.

  • (…) parentheses indicate a subshell. What's inside them isn't an expression like in many other languages. It's a list of commands (just like outside parentheses). These commands are executed in a separate subprocess, so any redirection, assignment, etc. performed inside the parentheses has no effect outside the parentheses.
    • With a leading dollar sign, $(…) is a command substitution: there is a command inside the parentheses, and the output from the command is used as part of the command line (after extra expansions unless the substitution is between double quotes, but that's another story).
  • { … } braces are like parentheses in that they group commands, but they only influence parsing, not grouping. The program x=2; { x=4; }; echo $x prints 4, whereas x=2; (x=4); echo $x prints 2. (Also braces require spaces around them and a semicolon before closing, whereas parentheses don't. That's just a syntax quirk.)
    • With a leading dollar sign, ${VAR} is a parameter expansion, expanding to the value of a variable, with possible extra transformations.
  • ((…)) double parentheses surround an arithmetic instruction, that is, a computation on integers, with a syntax resembling other programming languages. This syntax is mostly used for assignments and in conditionals.
    • The same syntax is used in arithmetic expressions $((…)), which expand to the integer value of the expression.
  • [[…]] double brackets surround conditional expressions. Conditional expressions are mostly built on operators such as -n $variable to test if a variable is empty and -e $file to test if a file exists. There are also string equality operators: "$string1" = "$string2" (beware that the right-hand side is a pattern, e.g. [[ $foo = a* ]] tests if $foo starts with a while [[ $foo = "a*" ]] tests if $foo is exactly a*), and the familiar !, && and || operators for negation, conjunction and disjunction as well as parentheses for grouping. Note that you need a space around each operator (e.g. [[ "$x" = "$y" ]], not [[ "$x"="$y" ]]), and a space or a character like ; both inside and outside the brackets (e.g. [[ -n $foo ]], not [[-n $foo]]).
  • [ … ] single brackets are an alternate form of conditional expressions with more quirks (but older and more portable). Don't write any for now; start worrying about them when you find scripts that contain them.

This is the idiomatic way to write your test in bash:

if [[ $varA = 1 && ($varB = "t1" || $varC = "t2") ]]; then

If you need portability to other shells, this would be the way (note the additional quoting and the separate sets of brackets around each individual test):

if [ "$varA" = 1 ] && { [ "$varB" = "t1" ] || [ "$varC" = "t2" ]; }; then

这篇关于在BASH简单的逻辑运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆