简体   繁体   English

如何在脚本中使用条件“和”条件?

[英]How can I use a condition 'and' condition in a script?

I'm trying to check if both conditions return the expected values. 我正在尝试检查两个条件是否都返回期望值。 I want to be sure that both return the expected value before continuing... My problematic line is: if [ [ $ansmob = "y" ] || [ $flagbook != "1" ] ]; 我想确保在继续操作之前,两者都返回期望值。我的问题是: if [ [ $ansmob = "y" ] || [ $flagbook != "1" ] ]; if [ [ $ansmob = "y" ] || [ $flagbook != "1" ] ];

read -r -p "Would you like to add $site.booking.local as well? [y/n] " ansbook
        if [ $ansbook = "y" ];
                then sed "s/ServerAlias.*/& $site.booking.local/" -i $workdir/$site$dom.conf
                flagbook="1"
        fi
read -r -p "Would you like to add m.$site.booking.local? [y/n] " ansmob
        if [ [ $ansmob = "y" ] || [ $flagbook != "1" ] ];
                then sed "s/& $site.booking.local/& $site.booking.local m.$site.booking.local/" -i $workdir/$site$dom.conf
                else
                sed "s/ServerAlias.* /& m.$site.booking.local/" -i $workdir/$site$dom.conf
                flagmobile="1"
        fi

Replace 更换

if [ [ $ansmob = "y" ] || [ $flagbook != "1" ] ];

with

if [ "$ansmob" = "y" ] || [ "$flagbook" != "1" ]

with bash's double brackets, you can use && and || 使用bash的双括号,您可以使用&&||

if [[ $ansmob = "y" || $flagbook -ne 1 ]]

Within double brackets, it's not strictly necessary to quote the variables: this command is smart about evaluating expressions with empty variables. 用双括号括起来,并不一定要用引号引起来:此命令很聪明,可以对带有空变量的表达式求值。

二进制&&运算符是AND运算的语法。

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

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