syn*_*nic 7 bash shell-script arguments
我有一个带有case语句的 bash 脚本:
case "$1" in
bash)
docker exec -it $(docker-compose ps -q web) /bin/bash
;;
shell)
docker exec -it $(docker-compose ps -q web) python manage.py shell
;;
test)
docker exec -it $(docker-compose ps -q web) python manage.py test "${@:2}"
;;
esac
Run Code Online (Sandbox Code Playgroud)
在test命令中,我想传递 的默认参数apps,但前提是用户没有传递除testbash 脚本以外的任何参数。
因此,如果用户像这样运行脚本:
./do test
Run Code Online (Sandbox Code Playgroud)
它应该运行命令
docker exec -it $(docker-compose ps -q web) python manage.py test apps
Run Code Online (Sandbox Code Playgroud)
但是,如果他们像这样运行脚本:
./do test billing accounts
Run Code Online (Sandbox Code Playgroud)
它应该运行命令
docker exec -it $(docker-compose ps -q web) python manage.py test billing accounts
Run Code Online (Sandbox Code Playgroud)
如何在第一个参数之后测试参数是否存在?
我会尝试使用 bash 变量替换:
test)
shift
docker exec -it $(docker-compose ps -q web) python manage.py test "${@-apps}"
;;
Run Code Online (Sandbox Code Playgroud)
其他方法是检查$*而不是$1:
case $* in
bash)
...
test)
docker exec -it $(docker-compose ps -q web) python manage.py test apps
;;
test\ *)
docker exec -it $(docker-compose ps -q web) python manage.py test "${@:2}"
;;
Run Code Online (Sandbox Code Playgroud)