linux shell 将变量参数附加到命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6962861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:30:43  来源:igfitidea点击:

linux shell append variable parameters to command

linuxbashshelljsdoc

提问by Billy Moon

I am trying to get a bash script that generates JSDoc for given parameters like this

我正在尝试获取一个 bash 脚本,该脚本为这样的给定参数生成 JSDoc

./jsdoc.sh file.js another.js maybe-a-third.js

I am getting stuck on how to pass an unknown quantity of parameters to the next shell command.

我被困在如何将未知数量的参数传递给下一个 shell 命令。

(also, don't know how to check if param exists, only if not exitst if [ -z ... ])

(另外,不知道如何检查 param 是否存在,仅当不存在 exitst 时if [ -z ... ]

This code works for up to two parameters, but obviously not the right way to go about it...

这段代码最多适用于两个参数,但显然不是正确的方法......

#!/bin/bash

# would like to know how to do positive check
if [ -z "" ]
then echo no param
else
        d=$PWD
        cd ~/projects/jsdoc-toolkit/

        # this bit is obviously not the right approach
        if [ -z "" ]
        then java -jar jsrun.jar app/run.js -a -t=templates/jsdoc/ $d/
        else java -jar jsrun.jar app/run.js -a -t=templates/jsdoc/ $d/ $d/
        fi

        cp -R out/jsdoc $d
fi

Any other pointers of how I could achieve this would be appreciated.

任何有关我如何实现这一目标的其他指示将不胜感激。

Edit: Updated script according to @skjaidev's answer - happy days ;)

编辑:根据@skjaidev 的回答更新了脚本 - 快乐的日子;)

#!/bin/bash

d=$PWD

for i in $*; do
    params=" $params $d/$i"
done

if [ -n "" ]; then
        cd ~/projects/jsdoc-toolkit/
        java -jar jsrun.jar app/run.js -a -t=templates/jsdoc/ $params
        cp -R out/jsdoc $d
fi

采纳答案by jman

$* has all the parameters. You could iterate over them

$* 具有所有参数。你可以迭代它们

for i in $*;
do
    params=" $params $d/$i"
done
your_cmd $params

回答by pyroscope

-nis the inverse of -z, and "$@"is the canonical way to pass all parameters on to a subcommand. This and more can be found via man bash.

-n是 的倒数-z"$@"是将所有参数传递给子命令的规范方式。这和更多可以通过找到man bash

回答by glenn Hymanman

To handle arguments that contain whitespace, use "$@"to iterate, and store the for later use in an array.

要处理包含空格的参数,请使用"$@"迭代并将其存储在数组中以备后用。

#!/bin/bash
if (( $# == 0 )); then
  echo "usage: 
#!/bin/sh

dir=$(pwd)

NEW_ARGV=""
# carefully modify original arguments individually and separate them with newlines
# in a new variable (in case they contain spaces)
NEW_ARGV=""
for var in "${@}"
do
    NEW_ARGV="${NEW_ARGV}
${dir}/${var}"
done

SV_IFS=${IFS}
# temporarily set IFS to a newline as per NEW_ARGV setup
IFS="
"
# reset $@ with the modified newline-separated arguments
set -- ${NEW_ARGV}
IFS=${SV_IFS}

# for testing, demonstrate each param is preserved
c=0
for i in "${@}"
do
    c=`expr ${c} + 1`
    echo "args-via-var #${c}: \"${i}\""
done

cd ~/projects/jsdoc-toolkit/
java -jar jsrun.jar app/run.js -a -t=templates/jsdoc/ "${@}"
cp -R out/jsdoc "${dir}"
file ..." exit fi dir=$(pwd) declare -a params for file in "$@"; do params+=( "$dir/$file" ); done cd ~/projects/jsdoc-toolkit/ java -jar jsrun.jar app/run.js -a -t=templates/jsdoc/ "${params[@]}" cp -R out/jsdoc "$dir"

回答by Greg A. Woods

Bash-specific features (arrays in this case) can be avoided through careful use of the IFSvariable and the special $@parameter.

通过仔细使用IFS变量和特殊$@参数,可以避免 Bash 特定的功能(在这种情况下是数组)。

##代码##

It's not necessary to reset $@, but doing so avoids messing with IFSin multiple places. Without going through $@one must set IFSeverywhere one expands $NEW_ARGV.

没有必要 reset $@,但这样做可以避免IFS在多个地方搞乱。无需经过$@一必须在IFS任何地方设置一个扩展$NEW_ARGV

Those with an eye for detail will note that this method does not preserve parameters when they contain newlines. It would be possible to use any control character in place of newline, except of course NUL, and perhaps ASCII FS (file separator, aka ctrl-\) would be both meaningful and very unlikely to occur in a valid filename.

那些关注细节的人会注意到,当参数包含换行符时,此方法不会保留参数。可以使用任何控制字符代替换行符,当然,除了NULASCII FS(文件分隔符,又名ctrl-\)可能既有意义又不太可能出现在有效文件名中。