Linux shell getopts 用法
从Shell Script的命令行当中解析参数
getopts optstring name [arg...]
命令描述optstring列出了对应的Shell Script可以识别的所有参数。比如,如果 Shell Script可以识别-a,-f以及-s参数,则optstring就是afs,如果对应 的参数后面还跟随一个值,则在相应的optstring后面加冒号。比如,a:fs 表示a参数后面会有一个值出现,-a value的形式。另外,getopts执行匹配 到a的时候,会把value存放在一个叫OPTARG的Shell Variable当中。如果 optstring是以冒号开头的,命令行当中出现了optstring当中没有的参数 将不会提示错误信息。
name表示的是参数的名称,每次执行getopts,会从命令行当中获取下一个 参数,然后存放到name当中。如果获取到的参数不在optstring当中列出, 则name的值被设置为?。
命令行当中的所有参数都有一个index,第一个参数从1开始,依次类推。 另外有一个名为OPTIND的Shell Variable存放下一个要处理的参数的index。
#!/bin/bash
help ()
{
echo ' ================================================================ '
echo ' script need six paramenters'
echo ' usage: ./script -a Name -v v3.0.0 -i 192.168.1.0 -p 8080 -e ppt -r cn'
echo ' ================================================================'
}
Name=""
Version=""
IP=""
port=""
env=""
Region=""
while getopts :a:v:i:p:e:r: opt;do
case $opt in
a)
Name=$OPTARG
;;
v)
Version=$OPTARG
;;
i)
IP=$OPTARG
;;
p)
port=$OPTARG
;;
e)
env=$OPTARG
;;
r)
Region=$OPTARG
;;
*)esac
done
if [ "$Name" == "" ] || [ "$Version" == "" ] || [ "$IP" == "" ] || [ "$port" == "" ] || [ "$env" == "" ] || [ "$Region" == "" ]; then
echo "OOPS:command start required"
help
exit 1
fi
servername="$Name"-"$Version"
echo "$servername"
url="$IP":"$port"
cat << EOF > ./nginx__"$servername".conf
location /$servername {
rewrite /$servername/(.*)\$1 break;
proxy_pass $url;
proxy_redirect off;
proxy_set_header Host: \$host;
}
EOF
➜ Downloads ./b.sh -a tomcat -v v3.0.0 -i 192.168.64.33 -p 8080 -e test -r cn
a option index is 2
tomcat-v3.0.0
➜ Downloads cat nginx_conf.d_tomcat-v3.0.0.conf
location /tomcat-v3.0.0 {
rewrite /tomcat-v3.0.0/(.*)$1 break;
proxy_pass 192.168.64.33:8080;
proxy_redirect off;
proxy_set_header Host: $host;
也可以用shellcheck检查下语法
shellcheck --help
Usage: shellcheck [OPTIONS...] FILES...
-a --check-sourced Include warnings from sourced files
-C[WHEN] --color[=WHEN] Use color (auto, always, never)
-i CODE1,CODE2.. --include=CODE1,CODE2.. Consider only given types of warnings
-e CODE1,CODE2.. --exclude=CODE1,CODE2.. Exclude types of warnings
-f FORMAT --format=FORMAT Output format (checkstyle, diff, gcc, json, json1, quiet, tty)
--list-optional List checks disabled by default
--norc Don't look for .shellcheckrc files
-o check1,check2.. --enable=check1,check2.. List of optional checks to enable (or 'all')
-P SOURCEPATHS --source-path=SOURCEPATHS Specify path when looking for sourced files ("SCRIPTDIR" for script's dir)
-s SHELLNAME --shell=SHELLNAME Specify dialect (sh, bash, dash, ksh)
-S SEVERITY --severity=SEVERITY Minimum severity of errors to consider (error, warning, info, style)
-V --version Print version information
-W NUM --wiki-link-count=NUM The number of wiki links to show, when applicable
-x --external-sources Allow 'source' outside of FILES
--help Show this usage summary and exit
相关推荐
-
PHP8种变量类型的详细讲解2025-02-22 00:32:24
-
php+apache 和 php+nginx的区别2025-02-22 00:21:27
-
PHP:与workerman结合实现定时任务2025-02-22 00:15:57
-
Nginx的Rewrite规则与实例2025-02-22 00:15:39
-
MySql中身份证字段的简单脱敏介绍2025-02-22 00:15:36