gpt4 book ai didi

bash - 如何有条件地向 shell 脚本添加标志?

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

我想在我的 bash 脚本中运行带有或不带 -v 标志的命令,具体取决于是否定义了环境变量 $VERBOSE。像这样:

#!/bin/bash

/usr/local/bin/mongo-connector \
-m $MONGO_HOST \
-t $NEO_URI \
[if $VERBOSE then -v ] \
-stdout

最佳答案

为了可读性,考虑使用数组来存储参数。

args=(
-m "$MONGO_HOST"
-t "$NEO_URI"
-stdout
)
if [[ -v VERBOSE ]]; then
args+=(-v)
fi
/usr/local/bin/mongo-connector "${args[@]}"

关于bash - 如何有条件地向 shell 脚本添加标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42985611/

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