gpt4 book ai didi

bash - 如何在 bash 脚本中有条件地添加额外参数?

转载 作者:行者123 更新时间:2023-11-29 09:20:09 25 4
gpt4 key购买 nike

有一个调用其他程序的脚本。下面是program.sh。这可能看起来毫无意义,但我省略了很多细节,而且……假设我想坚持原样的结构。

#!/usr/bin/env bash
function run_this {
/usr/bin/foo -A -B -C
}
run_this

我想通过传递给 program.sh 的参数更改传递给 /usr/bin/foo 的参数。因此,例如,如果您调用 program.sh --quiet,则调用 /usr/bin/foo -A -B -C -X。实现这一目标的最佳方法是什么?

最佳答案

使用数组。

cmd=(/usr/bin/foo -A -B -C)
if somecond; then
cmd+=(-X)
fi
"${cmd[@]}"

关于bash - 如何在 bash 脚本中有条件地添加额外参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36901106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com