英文:
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.s
h 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.s
h 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论