我是Unix shell脚本的新手,想要写一些小脚本的帮助.
我为我的脚本定义了以下概要:
install.sh [-h|-a path|[-k path][-f path][-d path][-e path]]
Run Code Online (Sandbox Code Playgroud)
即,用户可以请求一些help (-h),将所有内容安装到指定的位置(-a path),或者将一个或多个组件(-k, -f, -d -e)安装到适当的路径.如果没有参数,则应显示帮助.
提前致谢.
您可以使用getopts解析命令行bash.下面是使用getopts从Bash/Parsing命令行参数中获取的示例(显然,您必须根据需要调整选项).
#!/bin/bash
#Set a default value for the $cell variable
cell="test"
#Check to see if at least one argument was specified
if [ $# -lt 1 ] ; then
echo "You must specify at least 1 argument."
exit 1
fi
#Process the arguments
while getopts c:hin: opt
do
case "$opt" in
c) cell=$OPTARG;;
h) usage;;
i) info="yes"
n) name=$OPTARG;;
\?) usage;;
esac
done
Run Code Online (Sandbox Code Playgroud)
相关的SO问题如何在bash中解析命令行参数?
有关更多信息,请getopts在此手册页上搜索bash.