获取命令输出;如果包含X,则运行另一个命令

时间:2016-01-06 13:14:09

标签: bash if-statement command-line command .bash-profile

我想编写一个bash脚本:

  1. 运行命令bux
    • a)如果bux的输出包含have,则不执行任何操作
    • b)如果bux的输出包含X,请运行命令Y
    • c)如果bux的输出包含Z,请运行命令A
    • 它只包含一个这些东西,而不是多个x

2 个答案:

答案 0 :(得分:1)

这是一个应该做你想要的脚本(假设bux,Y和A是bash脚本):

#!/bin/bash
OUTPUT=`source bux`
if [[ "$OUTPUT" =~ have ]]; then
   :
elif [[ "$OUTPUT" =~ X ]]; then
   source Y  
elif [[ "$OUTPUT" =~ Z ]]; then
   source A
fi

如果您想要执行程序(假设bux,Y和A在路径中):

#!/bin/bash
OUTPUT=`bux`
if [[ "$OUTPUT" =~ have ]]; then
    :
elif [[ "$OUTPUT" =~ X ]]; then
    Y
elif [[ "$OUTPUT" =~ Z ]]; then
    A
fi  

答案 1 :(得分:1)

像这样的case语句中的

glob模式:

case $(bux) in
    *have*)
        echo 'do nothing?'
        ;;
    *X*)
        Y
        ;;
    *Z*)
        A
        ;;
    *)
        echo 'default case.'  # display an error ...?
        ;;
esac

显然,如果您愿意,模式可能会更复杂,但这似乎可以满足您的要求。

相关问题