Why arguments passed from one function to another with $@ are "lost" in this shell script?

huangapple go评论69阅读模式
英文:

Why arguments passed from one function to another with $@ are "lost" in this shell script?

问题

I'm trying to write some shell scripts (to use with Docker and VSCode Dev Containers) but I'm totally new to this. I'm trying to replicate the utility script found here which is, indeed, quite simple:

# Checks if command exists, installs it if not
# check_command <command> "<apt packages to install>" "<apk packages to install>" "<dnf/yum packages to install>"
check_command() {
    command_to_check=$1
    shift
    if type "${command_to_check}" > /dev/null 2>&1; then
        return 0
    fi
    install_packages "$@"
    return $?
}

# Installs packages using the appropriate package manager (apt, apk, dnf, or yum)
# install_packages "<apt packages to install>" "<apk packages to install>" "<dnf/yum packages to install>"
install_packages() {
    if type apt-get > /dev/null 2>&1; then
        apt_get_update_if_needed
        apt-get -y install --no-install-recommends $1
    elif type apk > /dev/null 2>&1; then
        apk add $2
    elif type dnf > /dev/null 2>&1; then
        dnf install -y $3
    elif type yum > /dev/null 2>&1; then
        yum install -y $3
    else
        echo "Unable to find package manager to install ${command_to_check}"
        exit 1
    fi
}

One is supposed to include utils.sh and run check_command curl to get curl installed if needed. In my install.sh it's what I'm doing:

#!/usr/bin/env bash
set -e

# Import common utils
. ./utils.sh

 # Verify dependencies
apt_get_update_if_exists
check_command curl
curl -o /usr/local/foo https://foo.example

This script fails:

> ./install.sh: line 10: curl: command not found

Adding debug lines inside check_command show that install_packages "$@" is empty, that is argument "curl" isn't passed correctly to install_packages.

Any help understanding why this is happening is much appreciated.

英文:

I'm trying to write some shell scripts (to use with Docker and VSCode Dev Containers) but I'm totally new to this. I'm trying to replicate the utility script found here which is, indeed, quite simple:

# Checks if command exists, installs it if not
# check_command <command> "<apt packages to install>" "<apk packages to install>" "<dnf/yum packages to install>"
check_command() {
    command_to_check=$1
    shift
    if type "${command_to_check}" > /dev/null 2>&1; then
        return 0
    fi
    install_packages "$@"
    return $?
}

# Installs packages using the appropriate package manager (apt, apk, dnf, or yum)
# install_packages "<apt packages to install>" "<apk packages to install>" "<dnf/yum packages to install>"
install_packages() {
    if type apt-get > /dev/null 2>&1; then
        apt_get_update_if_needed
        apt-get -y install --no-install-recommends $1
    elif type apk > /dev/null 2>&1; then
        apk add $2
    elif type dnf > /dev/null 2>&1; then
        dnf install -y $3
    elif type yum > /dev/null 2>&1; then
        yum install -y $3
    else
        echo "Unable to find package manager to install ${command_to_check}"
        exit 1
    fi
}

One is supposed to include utils.sh and run check_command curl to get curl installed if needed. In my install.sh it's what I'm doing:

#!/usr/bin/env bash
set -e

# Import common utils
. ./utils.sh

 # Verify dependencies
apt_get_update_if_exists
check_command curl
curl -o /usr/local/foo https://foo.example

This script fails:

> ./install.sh: line 10: curl: command not found

Adding debug lines inside check_command show that install_packages "$@" is empty, that is argument "curl" isn't passed correctly to install_packages.

Any help understanding why this is happening is much appreciated.

答案1

得分: 0

你的check_command函数调用install_packages并传递了所有的参数,但实际上只传递了一个参数。因此,install_packages也只会接收一个参数,但你却在引用$2$3。此外,你可能希望将它们放在双引号内。

英文:

Your check_command function which calls install_packages with all its arguments is only called with a single argument. Naturally install_packages will also only receive one but you're making it reference $2 and $3. Also you might want to place them inside double quotes.

huangapple
  • 本文由 发表于 2023年2月6日 17:53:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定