bash错误"命令未找到"使用if时使用"或"操作者

时间:2016-08-03 13:28:28

标签: bash shell unix sh

所以我试试这个,如果UNIX中的elif代码使用" bash脚本"它给了我这个输出

script[11]: pc4: command not found
script[14]: pc4: command not found

代替" -o"我之前有过" |"作为"或"运营商,但那不起作用。此外,我之前有括号,但是... ...

#!/bin/bash
host=`hostname`
test1="pc4"
test2="pc5"
prod1="t01"
prod2="t02"
prod3="t03"
prod4="t04"
path="/home/test1/test2/test3"

if "$host" = "$test1" -o "$host" = "$test2"
   then
       echo "test loaded"
elif "$host" = "$prod1" -o "$host" = "$prod2" -o "$host" = "$prod3" -o "$host" = "$prod4"
   then
       echo "prod loaded"
else
       echo "bad host"
fi

编辑:修复它,它必须是:

if [ "$host" = "$test1" -o "$host" = "$test2" ];
...
...
elif [ "$host" = "$prod1" -o "$host" = "$prod2" -o "$host" = "$prod3" -o "$host" = "$prod4" ];

感谢所有人!

5 个答案:

答案 0 :(得分:3)

你不是在打电话给一个人; -otest命令的运算符,但不建议使用它。

if test "$host" = "$test1" -o "$host" = "$test2"
then
   echo "test loaded"
elif test "$host" = "$prod1" -o "$host" = "$prod2" -o "$host" = "$prod3" -o "$host" = "$prod4"
then
   echo "prod loaded"
else
   echo "bad host"
fi

而是使用对 shell 运算符test加入的||的多次调用。 (为了便于阅读,可以在||之后轻松添加换行符。)

if test "$host" = "$test1" || 
   test "$host" = "$test2"
then
    echo "test loaded"
elif test "$host" = "$prod1" ||
     test "$host" = "$prod2" ||
     test "$host" = "$prod3" || 
     test "$host" = "$prod4"
then
    echo "prod loaded"
else
    echo "bad host"
fi

最后,您可能需要考虑使用case语句:

case $host of
    "$test1"|"$test2" ) echo "test loaded" ;;
    "$prod1"|"$prod2"|"$prod3"|"$prod4") echo "prod loaded" ;;
    *) echo "bad host" ;;
esac

答案 1 :(得分:1)

还可以尝试:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="viewport" content="user-scalable=no, width=device-width">
<style>
    body {position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        overflow: auto;
        background-image: url('images/rl-background-grid.jpg');
        background-repeat: no-repeat;
        background-size: cover;}
    .navigation {bottom: 0px;
        height: 100px;
        position: absolute;
        width: 100%;
        box-sizing: border-box;
        font-size: 18px;
        color: #000000;}
</style>
</head>
<body>
<div class="navigation">
This is a test
</div>
</body>
</html>

虽然只有&#34; =&#34;字符串相等比较需要&#34; =&#34;也用作赋值运算符。因此,为了减少可能的混淆,作为一种“最佳实践”,我会使用&#34; ==&#34;用于字符串相等。恰巧&#34; ==&#34;只是&#34; =&#34;的同义词但我不想将同一个算子用于多种用途 注意:我并不暗示POSIX兼容性。

答案 2 :(得分:0)

您需要将ifelif条件放在括号中:

#!/bin/bash
host=`hostname`
test1="pc4"
test2="pc5"
prod1="t01"
prod2="t02"
prod3="t03"
prod4="t04"
path="/home/test1/test2/test3"

if [ "$host" = "$test1" -o "$host" = "$test2" ]
   then
       echo "test loaded"
elif [ "$host" = "$prod1" -o "$host" = "$prod2" -o "$host" = "$prod3" -o "$host" = "$prod4" ]
   then
       echo "prod loaded"
else
       echo "bad host"
fi

请注意[之前和之后的空格至关重要。

答案 3 :(得分:0)

-otest命令或[命令支持的语法。

如图所示,请用[]括起您的条件:

if [ "$host" = "$test1" -o "$host" = "$test2" ];
then
       echo "test loaded"
elif [ "$host" = "$prod1" -o "$host" = "$prod2" -o "$host" = "$prod3" -o "$host" = "$prod4" ];
then
       echo "prod loaded"
else
       echo "bad host"
fi

请参阅此link

答案 4 :(得分:0)

试试这个;

#!/bin/bash
host=`hostname`
test1="pc4"
test2="pc5"
prod1="t01"
prod2="t02"
prod3="t03"
prod4="t04"
path="/home/test1/test2/test3"

if [ "$host" = "$test1" ] || [ "$host" = "$test2" ] ; then
echo "test loaded"
elif [ "$host" = "$prod1" ] || [ "$host" = "$prod2" ] || [ "$host" = "$prod3" ] || [ "$host" = "$prod4" ] 
   then
       echo "prod loaded"
else
       echo "bad host"
fi