我的shell脚本文件中包含以下命令:
docker exec dev-wordpress $phpunitPath \
--configuration $configurationPath \
--testsuit $testsuit \
--group $group \
--testdox
如果我将“ testsuit”和“ group”设置为命令行选项,则可以正常工作。 仅当那些变量具有值时,才应使用“ testsuit”和“ group”选项。 使用“ if-else”可以解决与“ testdox”相同的问题,但是当我要使用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
答案 0 :(得分:1)
您可以使用参数扩展:
docker exec dev-wordpress "$phpunitPath" \
--configuration "$configurationPath" \
${testsuit:+--testsuit "$testsuit"} \
${group:+--group "$group"} \
--testdox
此脚本对于$ testsuit和$ group应该很好用。
我没有注意到您可能对其他两个变量有问题。
我更新了脚本,也许您可以再试一次。
答案 1 :(得分:0)
我需要先将命令构建为字符串,然后使用eval运行它。
eval "docker exec irea-wordpress $phpunit --configuration $configuration $testsuite $group $filter $testdox"