if 语句 bash 中的多个和 ors [英] Multiple and ors in if statement bash

查看:24
本文介绍了if 语句 bash 中的多个和 ors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些事情,我需要创建一个 if 语句,它将在 '&&' 中使用多个变量和'||'组合 .现在我正在尝试这个简单的任务,但我收到错误找不到命令".我想先&&两个计数比 ||它带有我在代码中设置的标志.以下是我正在尝试的代码

Hi i am working on somoething , i need to create a if statement which will take multiple variables in '&&' and '||' combination . for now i am trying to this this simple task but i am getting errors 'Command not found'. i want to first && two counts than || it with a flag i set in my code. following is the code i am trying

if [[$pcount -eq 0 ] && [$ucount -eq 0]] ||  [test $flag -eq 1]
then 
do my stuff 
else 
exit 1
fi

先谢谢你.

推荐答案

你不能嵌套 [ ... ] 因为 [ 是一个 shell 命令,而不是一个单独的句法结构.在 Bash 中,您可以使用支持嵌套的 [[ ... ]] 语法结构,而在可移植的 sh 中,您可以使用 ( ...) 括号优先:

You can't nest [ ... ] because [ is a shell command, not a separate syntactic construct. In Bash, you can use the [[ ... ]] syntactic construct which does support nesting, and in portable sh you can use the ( ... ) parentheses for precedence:

if ([ $pcount -eq 0 ] && [ $ucount -eq 0 ]) ||  [ $flag -eq 1 ]

补充说明:

  • [test condition] 没有意义,因为 [ 操作符已经是 test 的别名.编写[条件]测试条件.

  • [test condition] doesn't make sense, since the [ operator is already an alias for test. Write either [ condition ] or test condition.

[ 和测试之前留一个空格,否则 sh 不会将 [ 识别为命令名称.[ 是一个和其他命令一样的命令,与特殊的解析规则无关.

Leave a space before [ and the test, otherwise sh won't recognize the [ as a command name. [ is a command like any other and is not associated with special parsing rules.

这篇关于if 语句 bash 中的多个和 ors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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