2
【笔记】Shell 获取命令行参数
source link: https://loli.fj.cn/2023/08/16/Shell%E8%8E%B7%E5%8F%96%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Shell 获取命令行参数
获取命令行参数
demo.sh -h 127.0.0.1 -p 80
获取 Shell 程序名
echo $0
demo.sh
echo $1
echo $2
-h
127.0.0.1
获取所有参数总数
echo $#
4
根据标识符获取参数
shift
while [ $# -gt 0 ];
do
case $1 in
-h) echo $2
shift
;;
-p) echo $2
shift
;;
esac
shift
done
127.0.0.1
80
getopts
while getopts a:b:c: opt;
do
case $opt in
h) echo $OPTARG
;;
p) echo $OPTARG
;;
?) echo "没有这个参数"
;;
esac
done
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK