如何有条件地将标志添加到shell脚本? [英] How to conditionally add flags to shell scripts?
本文介绍了如何有条件地将标志添加到shell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在bash脚本中运行带有或不带有 -v
标志的命令,具体取决于是否定义了环境变量$ VERBOSE.像这样:
I want to run a command inside my bash script with or without a -v
flag depending on if the environment variable $VERBOSE is defined. Something like this:
#!/bin/bash
/usr/local/bin/mongo-connector \
-m $MONGO_HOST \
-t $NEO_URI \
[if $VERBOSE then -v ] \
-stdout
推荐答案
出于可读性考虑,请考虑使用数组存储参数.
For readability, consider using an array to store the arguments.
args=(
-m "$MONGO_HOST"
-t "$NEO_URI"
-stdout
)
if [[ -v VERBOSE ]]; then
args+=(-v)
fi
/usr/local/bin/mongo-connector "${args[@]}"
这篇关于如何有条件地将标志添加到shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文