bash - 使用带有条件的选项标志(shell 脚本)
问题描述
我的 shell 脚本文件中有这个命令:
docker exec dev-wordpress $phpunitPath \
--configuration $configurationPath \
--testsuit $testsuit \
--group $group \
--testdox
如果我将“testsuit”和“group”设置为命令行选项,它就可以工作。'testsuit' 和 'group' 选项只有在这些变量有价值时才应该使用。'testdox' 的相同问题可以通过 'if-else' 解决,但当我想对 3 个不同的选项做同样的事情时,这不是一个好方法。
如果 $group 变量中没有值,如何避免使用“--group”选项?
#!/bin/zsh
phpunit="/var/www/html/wp-content/plugins/irea/api/src/vendor/bin/phpunit"
configuration="/var/www/html/wp-content/plugins/irea/api/tests/phpunit.xml"
testdox=
filter=
testsuite=
group=
while [ "$1" != "" ]; do
case $1 in
--group ) shift
group="--group $1"
;;
--testsuite ) shift
testsuite="--testsuite $1"
;;
--filter ) shift
filter="--filter $1"
;;
--testdox ) testdox="--testdox"
;;
esac
shift
done
docker exec irea-wordpress $phpunit \
--configuration $configuration \
$testsuite \
$group \
$filter \
$testdox
解决方案
您可以使用参数扩展:
docker exec dev-wordpress "$phpunitPath" \
--configuration "$configurationPath" \
${testsuit:+--testsuit "$testsuit"} \
${group:+--group "$group"} \
--testdox
这个脚本应该适用于 $testsuit 和 $group。
我没有注意到您可能对其他两个变量有问题。
我更新了脚本,也许你可以再试一次。
推荐阅读
- ssh - 来自 ssh 会话的 pygame:如何在附加屏幕上显示?
- filesystems - 如何通过具有 DELETE 访问权限的 id 打开文件?
- reactjs - React:删除具有相同键和相同道具的组件之一时的混淆结果
- java - 如何关闭底部导航或调用另一个空活动?
- c# - 路由中的 Aws lambda 访问认知身份(通过 api 网关 websocket api)
- php - 使用 Nativescript 和 PHP 上传图片
- c# - 将 JSON 对象映射到 C# 类属性数组
- mysql - 在 Kubernetes POD 中执行 MySQL 命令
- javascript - 在redux中发送动作,如果在switch的reducer指令中有几种情况
- algorithm - 我被时间复杂度困住了