【问题标题】:bash if statement with command and sys var带有命令和 sys var 的 bash if 语句
【发布时间】:2013-09-18 09:51:51
【问题描述】:

关于信息,我在 solaris 9 上,使用 bash 脚本。 手动运行脚本工作正常,但通过 cron,他无法通过 第一次验证:

if [ `/usr/bin/echo $MACHTYPE | grep -i "solaris"` ];then

我尝试了几十种变体。但似乎没有一个工作。 我可以跳过那部分,但稍后我会遇到类似的问题,这个 if 语句很有趣,因为它包含一个使用系统变量的命令。我尝试了另一个版本,将输出与字符串进行比较(可能更难?)但我被卡住了。

如果有人可以说明使用什么以及在哪里使用:( [ ' " 因为有时 [[ 无法识别。所有的变化都是基于我在这里和其他地方发现的。格式化的可能性似乎很大。 (我省略了另一个'then echo .. fi')

[mvf@odin:/usr/mvf/bin/scripts] $ if ['/usr/bin/echo $MACHTYPE == "solaris"']
> then echo "hello"
> fi
bash: [/usr/bin/echo $MACHTYPE == "solaris"]: No such file or directory
[mvf@odin:/usr/mvf/bin/scripts] $ if ['/usr/bin/echo $MACHTYPE == 'solaris'']    
bash: [/usr/bin/echo $MACHTYPE == solaris]: No such file or directory
[mvf@odin:/usr/mvf/bin/scripts] $ if ["/usr/bin/echo $MACHTYPE == 'solaris'"] 
bash: [/usr/bin/echo solaris == 'solaris']: No such file or directory
[mvf@odin:/usr/mvf/bin/scripts] $ if ["$MACHTYPE == 'solaris'"]\             
bash: syntax error near unexpected token `fi'
[mvf@odin:/usr/mvf/bin/scripts] $ if ["$MACHTYPE == 'solaris'"] 
bash: [solaris == 'solaris']: command not found
[mvf@odin:/usr/mvf/bin/scripts] $ if ['$MACHTYPE == 'solaris'']     
bash: [$MACHTYPE == solaris]: command not found
[mvf@odin:/usr/mvf/bin/scripts] $ if ['$MACHTYPE == solaris']  
bash: [$MACHTYPE == solaris]: command not found
[mvf@odin:/usr/mvf/bin/scripts] $ if ["$MACHTYPE == solaris"] 
bash: [solaris == solaris]: command not found
[mvf@odin:/usr/mvf/bin/scripts] $ if [["$MACHTYPE" == "solaris"]]              
bash: [[solaris: command not found
[mvf@odin:/usr/mvf/bin/scripts] $ if [[$MACHTYPE == solaris]]      
bash: [[solaris: command not found
[mvf@odin:/usr/mvf/bin/scripts] $ if [["$MACHTYPE" == solaris]]
bash: [[solaris: command not found
[mvf@odin:/usr/mvf/bin/scripts] $ if [["/usr/bin/echo $MACHTYPE" == solaris]]
bash: [[/usr/bin/echo solaris: No such file or directory
[mvf@odin:/usr/mvf/bin/scripts] $ if [[($MACHTYPE) == solaris]]   
bash: syntax error near unexpected token `[[($MACHTYPE)'
[mvf@odin:/usr/mvf/bin/scripts] $ if [[($MACHTYPE) == solaris]]
bash: syntax error near unexpected token `[[($MACHTYPE)'
[mvf@odin:/usr/mvf/bin/scripts] $ if [["/usr/bin/echo $(MACHTYPE)" == solaris]]
bash: MACHTYPE: command not found
bash: [[/usr/bin/echo : No such file or directory
[mvf@odin:/usr/mvf/bin/scripts] $ if [["$(MACHTYPE)" == solaris]]              
bash: MACHTYPE: command not found
bash: [[: command not found
[mvf@odin:/usr/mvf/bin/scripts] $ if ["$(MACHTYPE)" == solaris]  
bash: MACHTYPE: command not found
[: missing `]'
[mvf@odin:/usr/mvf/bin/scripts] $ if [$(MACHTYPE) == solaris]  
bash: MACHTYPE: command not found
[: missing `]'
[mvf@odin:/usr/mvf/bin/scripts] $ if [[ $(echo $MACHTYPE) == solaris]]; then         
bash: [[: command not found
[mvf@odin:/usr/mvf/bin/scripts] $ if (( $(echo $MACHTYPE) == solaris)); then    
bash: solaris: command not found
[mvf@odin:/usr/mvf/bin/scripts] $ if (( $(echo /$MACHTYPE) == solaris)); then
> echo "hello"
> fi
bash: /solaris: No such file or directory
[mvf@odin:/usr/mvf/bin/scripts] $ if (( $(echo /$MACHTYPE) == solaris)); then
> 
> fi
bash: syntax error near unexpected token `fi'
[mvf@odin:/usr/mvf/bin/scripts] $ if (( $($MACHTYPE) == solaris)); then      
bash: solaris: command not found
bash: ==: command not found

[mvf@odin:/usr/mvf] $ if (( $(echo \$MACHTYPE) == solaris)); then 
bash: $MACHTYPE: command not found
[mvf@odin:/usr/mvf] $ if (( $('echo \$MACHTYPE') == solaris)); then
> echo "hello"
> fi
bash: echo \$MACHTYPE: command not found
bash: ==: command not found
[mvf@odin:/usr/mvf] $ if (( $('echo $MACHTYPE') == solaris)); then 
bash: echo $MACHTYPE: command not found
bash: ==: command not found
[mvf@odin:/usr/mvf] $ if (( $('/usr/bin/echo $MACHTYPE') == solaris)); then
bash: /usr/bin/echo $MACHTYPE: No such file or directory
bash: ==: command not found
[mvf@odin:/usr/mvf] $ if (( $(/usr/bin/echo $"MACHTYPE") == solaris)); then 
bash: $MACHTYPE: command not found

【问题讨论】:

    标签: bash variables if-statement command


    【解决方案1】:

    我尝试了不同的解决方案将变量传递给 cron,但只有一个有效:

    创建一个带有变量声明的 .cronfile,然后 直接在 cron 运行的脚本中获取文件: (我还在脚本中声明了 PATH,因为 cron PATH 不完整)

    . $HOME/.cronfile
    rest of the script
    

    所以没有语法问题:)

    谢谢丹尼特!

    【讨论】:

      【解决方案2】:

      你是说你的台词

      if [ `/usr/bin/echo $MACHTYPE | grep -i "solaris"` ];then echo "hello"; fi
      

      在您自己运行时有效,但在 cron 运行脚本时无效。这意味着您的线路没问题,实际上它在登录 shell 中对我有用。

      那么 cron 运行它有什么不同呢?

      this post 关于 cron 失败的原因。我想到的一个是 cron 需要所有内容的显式路径,包括 grep。所以你可能需要 /bin/grep 而不是 grep。如果不是这样,我希望那里的东西有帮助。

      Aleks 的测试表达更吸引人。让我们知道什么可以解决您的问题。

      【讨论】:

        【解决方案3】:

        使用 bash 通配符:

        if [[ $MACHTYPE = *solaris* ]]; then
        

        使用正则表达式:

        if [[ $MACHTYPE =~ solaris ]]; then
        

        纯sh解决方案:

        case $MACHTYPE in
            *solaris*)
                echo "Yes, it is solaris"
                ;;
            *)
                echo "No, something else"
                ;;
        esac
        


        最后是最糟糕的解决方案:

        if /usr/bin/echo "$MACHTYPE" | grep -i 'solaris'; then
        

        对我来说也是 /bin/echo,但 echo 应该也可以。


        似乎暴力破解对你不起作用,所以你最好阅读所有这些:
        Bash Guide: Tests and Conditionals
        Bash hackers: test command
        Bash hackers: conditional expressions

        【讨论】:

        • 问题是系统变量没有通过。我已经尝试过 bash globbing。但是感谢您提供的信息;)
        猜你喜欢
        • 2018-04-09
        • 2012-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多