本文介绍了如何在bash脚本中有条件地添加额外的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个脚本可以调用其他程序.以下是 program.sh .看起来似乎毫无意义,但我省略了许多细节,并且...让我说我想保持原样.

There's a script that calls the other program. The following is program.sh. It might look non-sense but I'm omitting lots of details and… Let's say I wanna stick to the structure as is.

#!/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 .实现此目标的最佳方法是什么?

I wanna change the arguments passed to /usr/bin/foo by the argument passed to program.sh. So for example, if you call program.sh --quiet then /usr/bin/foo -A -B -C -X. What's the best way to achieve this?

推荐答案

使用数组.

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

这篇关于如何在bash脚本中有条件地添加额外的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:27
查看更多