echo 在 if 中起作用吗?

echo 在 if 中起作用吗?

考虑以下函数:

function testForBinary {
    someBin=$(command -v binary)
        # Test if binary is installed
        if [[ -n $someBin ]]; then
            installSuccess="Success"
            echo ${installSuccess}
            return
        else
            # This should never be reached
            return 1
        fi
}

取自 (语境):

function installAndTestForDialog() {
# dialog allows me to create a pretty installer
# It is a required dependency since XXXXX
# Name Removed as I'm attempting modularization
# of a script from Github that's one huge Script

# See https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then
    local dialogBin
    local updated=false
    local dialogInstallSuccess

    dialogBin=$(command -v dialog)
    if [[ -z $dialogBin ]]; then
        printf "Installing dialog... "
        if [[ $updated -eq 0 ]]; then
            apt-get update > /dev/null 2>&1
            updated=true
        fi
        # Debian Based OS
        apt-get -y install dialog > /dev/null 2>&1

        # Test if dialog is installed to verify installation
        # above was successful.
        dialogBin=$(command -v dialog)
        if [[ -n $dialogBin ]]; then
            # I cannot get this to echo success
            # if I echo from here
            dialogInstallSuccess="Success"
            # Moving it here doesn't help either  Why??
            echo ${dialogInstallSuccess}
            return
        else
            # This should never be reached
            return 1
        fi
    fi    
}    

我试图将其installSuccess视为布尔值,但我做错了什么。如果我按照上面的方式编写函数,然后添加:

isInstalled=$(testForBinary)
echo "$isInstalled"

isInstalled返回一个空行。我知道这不是真的,因为当我command -v binary在函数之外运行时,binary结果的路径。

输出 (语境):

Function and Variable Output Test
=====================
# These are other tests
# I'm performing, but note
# the blank line, which should
# print Success 
2.9-MODULAR
192.168.1.227
false
false
(blank line)

答案1

这不需要像您所做的那样复杂和脆弱。

installAndTestForDialog() {
  if command -v dialog &> /dev/null; then
    return 0
  else
    apt-get update &> /dev/null
    apt-get -y install dialog &> /dev/null
  fi
}

如果对话框已经安装,该函数将返回 0。否则它将尝试安装它并返回apt-get install命令的退出代码。


正如@muru 在评论中指出的那样,这可以简化为:

if ! command -v dialog; then
    apt-get update
    apt-get install -y dialog
fi > /dev/null 2>&1

或者只是尝试安装它,而不去检查它是否已经安装:

{ apt-get update && apt-get -y --no-upgrade install dialog ;} >/dev/null 2>&1

如果已安装,该--no-upgrade选项将阻止升级。dialog

答案2

function installAndTestForDialog {
# dialog allows me to create a pretty installer
# It is a required dependency since Pinecraft 1.1

# See https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then
    local dialogBin
    local updated=false
    local dialogInstallSuccess

    dialogBin=$(command -v dialog)
    if [[ -z "$dialogBin" ]]; then
        printf "Installing dialog... "
        if [[ $updated -eq 0 ]]; then
            apt-get update > /dev/null 2>&1
            updated=1
            apt-get -y install dialog > /dev/null 2>&1
        fi
    # This was the issue.  The install of dialog
    # cannot be included with the post install test
    # Moving it up, to "break the nesting" solves the issue 
    fi
        # Test if dialog is installed to verify installation
        # above was successful.
        dialogBin=$(command -v dialog)
        if [[ -n "$dialogBin" ]]; then
            dialogInstallSuccess="Success"
            echo "${dialogInstallSuccess}"
            return
        else
            # This should never be reached
            return 1
        fi
        # Moved the fi that was here up. See comment above
}

上述函数产生了所需的结果。我讨厌我写的东西语法正确但逻辑错误

相关内容