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

标签 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

最佳答案

这个怎么样:

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

关于bash - 命令返回数据到变量的返回状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006952/

相关文章:

windows - 使用 Windows 未定义的环境变量?

javascript - 尝试使用变量减少 javascript 的重复

jquery - 将变量传递给 .animate() 函数 JQuery

arrays - 检查元素是否存在于 Bash 数组中

linux - 如何同时设置 niceness 和 process affinity?

linux - 使用shell脚本在指定模式后将多行插入文件

shell - Tomcat 即服务

regex - 重命名文件或移动文件(如果在 Linux bash 中以逗号分隔)

bash - 通过 ssh 调用交互式 bash 脚本

bash 变量中的 json 字符串