返回将数据返回到变量的命令的状态

时间:2015-06-23 15:23:39

标签: bash shell variables return-value

如何从命令中获取状态,该命令被分配到变量中?

例如:

#! /bin/bash

### GET PID
GETPID=$(ps aux | grep "bash" | grep -v "grep" | awk '{print $2 }')

if [ "$?" = "0" ]; then
    echo "status OK"
else 
    echo "status NOT OK"
fi

1 个答案:

答案 0 :(得分:2)

这个怎么样:

PID=($(pidof bash))
if [[ ${#PID[@]} -gt 0 ]]; then
    echo "status OK"
else
    echo "status not OK"
fi