在条件下使用选项标志(shell 脚本)
Using option flag with condition (shell script)
我的 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 [ "" != "" ]; do
case in
--group ) shift
group="--group "
;;
--testsuite ) shift
testsuite="--testsuite "
;;
--filter ) shift
filter="--filter "
;;
--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。
我没有注意到您可能对其他两个变量有问题。
我更新了脚本,也许你可以再试一次。
我需要先将命令构建为字符串,然后 运行 使用 eval 将其构建。
eval "docker exec irea-wordpress $phpunit --configuration $configuration $testsuite $group $filter $testdox"
我的 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 [ "" != "" ]; do
case in
--group ) shift
group="--group "
;;
--testsuite ) shift
testsuite="--testsuite "
;;
--filter ) shift
filter="--filter "
;;
--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。
我没有注意到您可能对其他两个变量有问题。
我更新了脚本,也许你可以再试一次。
我需要先将命令构建为字符串,然后 运行 使用 eval 将其构建。
eval "docker exec irea-wordpress $phpunit --configuration $configuration $testsuite $group $filter $testdox"