./myscript.sh arg1 arg2 arg3
#!/bin/bash
for option in "$@"; do
case "${option}" in
-e)
echo "Option -e is selected."
# 在这里添加 -e 选项的操作
;;
-h)
echo "Option -h is selected."
# 在这里添加 -h 选项的操作
;;
*)
echo "Invalid option: ${option}"
exit 1
;;
esac
done
#!/bin/bash
while getopts ":eh" option; do
case "${option}" in
e)
echo "Option -e is selected."
# 在这里添加 -e 选项的操作
;;
h)
echo "Option -h is selected."
# 在这里添加 -h 选项的操作
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument."
exit 1
;;
esac
done
前面一个:当出现不存在选项时报错,后面:表示可以添加字符串