Ale*_*lls 9 shell bash shell-script arguments
我有以下 Bash 代码:
function suman {
if test "$#" -eq "0"; then
echo " [suman] using suman-shell instead of suman executable.";
suman-shell "$@"
else
echo "we do something else here"
fi
}
function suman-shell {
if [ -z "$LOCAL_SUMAN" ]; then
local -a node_exec_args=( )
handle_global_suman node_exec_args "$@"
else
NODE_PATH="${NEW_NODE_PATH}" PATH="${NEW_PATH}" node "$LOCAL_SUMAN" --suman-shell "$@";
fi
}
Run Code Online (Sandbox Code Playgroud)
当suman
用户在没有参数的情况下执行命令时,就会命中:
echo " [suman] using suman-shell instead of suman executable.";
suman-shell "$@"
Run Code Online (Sandbox Code Playgroud)
我的问题是 - 如何将参数附加到“$@”值?我需要简单地做一些类似的事情:
handle_global_suman node_exec_args "--suman-shell $@"
Run Code Online (Sandbox Code Playgroud)
显然这是错误的,但我不知道该怎么做。我不是在寻找什么-
handle_global_suman node_exec_args "$@" --suman-shell
Run Code Online (Sandbox Code Playgroud)
问题是它handle_global_suman
适用于$1
and$2
如果我--suman-shell
进入$3
,那么我必须更改其他代码,并且宁愿避免这种情况。
初步回答:
local args=("$@")
args+=("--suman-shell")
if [ -z "$LOCAL_SUMAN" ]; then
echo " => No local Suman executable could be found, given the present working directory => $PWD"
echo " => Warning...attempting to run a globally installed version of Suman..."
local -a node_exec_args=( )
handle_global_suman node_exec_args "${args[@]}"
else
NODE_PATH="${NEW_NODE_PATH}" PATH="${NEW_PATH}" node "$LOCAL_SUMAN" "${args[@]}";
fi
Run Code Online (Sandbox Code Playgroud)
Ign*_*ams 13
将参数放入一个数组中,然后附加到该数组中。
args=("$@")
args+=(foo)
args+=(bar)
baz "${args[@]}"
Run Code Online (Sandbox Code Playgroud)