【问题标题】:How can I get this simple Bash command substitution to work?我怎样才能让这个简单的 Bash 命令替换工作?
【发布时间】:2014-02-20 02:30:47
【问题描述】:

我正在尝试让一个简单的 Bash 命令替换工作,但遇到了麻烦。我对 Bash 脚本相当陌生。我已经阅读了许多示例和 Bash 手册的相关部分,但仍然无法让以下代码工作。

到目前为止我所拥有的;

#!/bin/sh

checkString="$(/usr/bin/dscl . -read /Groups/_lpoperator GroupMembership)"

echo "$checkString"

这是结果;

localuser@tsu1:~/Desktop$ sh installcheck.sh 
No such key: GroupMembership

localuser@tsu1:~/Desktop

No such key: GroupMembership 后面的空行是"$checkString" 的回声。无论我如何格式化,$checkString 总是显示为空。我期望在$checkString 中的是No such key: GroupMembershipdscl 命令的任何结果。

我确定我遗漏了一些明显的东西,但我似乎无法弄清楚。对此的任何帮助将不胜感激。

非常感谢。

【问题讨论】:

    标签: string bash command output substitution


    【解决方案1】:

    你需要将stderr重定向到stdout

    checkString="$(/usr/bin/dscl . -read /Groups/_lpoperator GroupMembership 2>&1)"
    echo "$checkString"
    

    【讨论】:

    • 嗨。感谢您的快速回复。但这给了我; No such key: GroupMembership 2>&1 但是您的回答使我找到了正确的方法; checkString="$(/usr/bin/dscl . -read /Groups/_lpoperator GroupMembership 2>&1)"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多