bash Shell 脚本中的布尔表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48774/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:28:22  来源:igfitidea点击:

Boolean Expressions in Shell Scripts

bashshellexpression

提问by Adam Ernst

What's the "right" way to do the following as a boolean expression?

作为布尔表达式执行以下操作的“正确”方法是什么?

for i in `ls /resources`; do
    if [ $i != "database.db" ]
    then
        if [ $i != "tiles" ]
        then
            if [ $i != "map.pdf" ]
            then
                if [ $i != "map.png" ]
                then
                    svn export -q /resources/$i ../MyProject/Resources/$i
...

采纳答案by pixelbeat

The other solutions have a couple of common mistakes: http://www.pixelbeat.org/programming/shell_script_mistakes.html

其他解决方案有几个常见的错误:http: //www.pixelbeat.org/programming/shell_script_mistakes.html

  1. for i in $(ls ...)is redundant/problematic just do: for i in $1/resources*; do ...

  2. [ $i != file1 -a $1 != file2 ]This actually has 2 problems.

    a. The $iis not quoted, hence names with spaces will cause issues

    b. -ais inefficient if stating files as it doesn't short circuit (I know the above is not stating files).

  1. for i in $(ls ...)是多余的/有问题的只是做: for i in $1/resources*; do ...

  2. [ $i != file1 -a $1 != file2 ]这实际上有两个问题。

    一种。在$i没有加引号,因此用空格会导致一些问题名

    -a如果stating 文件没有短路,则效率低下(我知道上面不是stating 文件)。

So instead try:

因此,请尝试:

for i in /resources/*; do
    if [ "$i" != "database.db" ] &&
       [ "$i" != "tiles" ] &&
       [ "$i" != "map.pdf" ] &&
       [ "$i" != "map.png" ]; then
        svn export -q "$i" "../MyProject/Resources/$(basename $i)"
    fi
done

回答by Mo.

Even shorter:

更短:

for i in `ls /resources`; do
  if [ $i != databse.db -a $i != titles -a $i != map.pdf ]; then
    svn export -q /resources/$i ../MyProject/Resources/$i
  fi
done;

The -ain the if expression is the equivalent of the boolean AND in shell-tests. For more see man test

-a在if表达式是等效的布尔AND在壳测试。更多见man test

回答by Fred Yankowski

Consider using a casestatement:

考虑使用case语句:

for i in $(ls /resources); do
    case $i in
        database.db|tiles|map.pdf|map.png)
           ;;
        *)
           svn export -q /resources/$i ../MyProject/Resources/$i;;
    esac
done

回答by frizz

for i in `ls /resources`; do
    if [ $i != "database.db" ] && [ $i != "tiles" ] && [ $i != "map.pdf" ] && [ $i != "map.png" ]; then
        svn export -q /resources/$i ../MyProject/Resources/$i

回答by Evan Langlois

For future reference, the new [[ test operator is preferred. The accepted answer is close and everything mentioned applies, but that answer will require lots of quoting and calls to multiple tests.

为了将来参考,首选新的 [[ 测试运算符。接受的答案很接近,提到的所有内容都适用,但该答案需要大量引用和调用多个测试。

The preferred method would be something like:

首选方法类似于:

for i in /resources/*; do
    if [[ $i != "database.db" && $i != "tiles" &&
                $i != "map.pdf" && $i != "map.png" ]]; then
        svn export -q "$i" "../MyProject/Resources/$(basename $i)"
    fi
done