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

发布于 2024-11-28 07:46:16 字数 1040 浏览 3 评论 0原文

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

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

我陷入了如何将未知数量的参数传递给下一个 shell 命令的困境。

(另外,不知道如何检查参数是否存在,只有在不存在时if [ -z ... ]

此代码最多适用于两个参数,但显然不是正确的方法继续吧...

#!/bin/bash

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

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

        cp -R out/jsdoc $d
fi

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

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

#!/bin/bash

d=$PWD

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

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

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

./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.

(also, don't know how to check if param exists, only if not 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 "$1" ]
then echo no param
else
        d=$PWD
        cd ~/projects/jsdoc-toolkit/

        # this bit is obviously not the right approach
        if [ -z "$2" ]
        then java -jar jsrun.jar app/run.js -a -t=templates/jsdoc/ $d/$1
        else java -jar jsrun.jar app/run.js -a -t=templates/jsdoc/ $d/$1 $d/$2
        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 ;)

#!/bin/bash

d=$PWD

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叹梦 2024-12-05 07:46:17

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

#!/bin/bash
if (( $# == 0 )); then
  echo "usage: $0 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"

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: $0 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"
梦毁影碎の 2024-12-05 07:46:17

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

-n is 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.

挽手叙旧 2024-12-05 07:46:17

通过仔细使用 IFS 变量和特殊的 $@ 参数,可以避免特定于 Bash 的功能(在本例中为数组)。

#!/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}"

没有必要重置 $@,但这样做可以避免在多个地方弄乱 IFS。如果不经过 $@,则必须在扩展 $NEW_ARGV 的任何地方设置 IFS

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

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

#!/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}"

It's not necessary to reset $@, but doing so avoids messing with IFS in multiple places. Without going through $@ one must set IFS everywhere one expands $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.

要走干脆点 2024-12-05 07:46:16

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

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

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

for i in $*;
do
    params=" $params $d/$i"
done
your_cmd $params
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文