如何强制为 bash 脚本设置选项间隔
How to make it manditory for options to be spaced for bash scripts
我有以下脚本。
我想修改它,这样如果我要用两个选项调用 temp.sh
,我将不得不 space 它们。即:像 temp.sh -fc30
这样的脚本调用应该是无效的,而应该是 temp.sh -f -c 30
ARGS=$(getopt -o c:f -l "charlie:fox" -n "temp.sh" -- "$@");
#bad args
if [ $? -ne 0 ];
then
exit 1
fi
eval set --"$ARGS";
while true; do
case "" in
-c|--charlie)
shift;
if [ -n "" ]; then
echo "-c =: ";
shift;
fi
;;
-f|--fox)
shift;
echo "fox used";
;;
--)
shift;
break;
;;
esac
done
只是不要使用 getopt
。
#!/bin/bash
# parse options
while [[ $# -gt 0 ]]; do
case in
-c|--charlie)
echo " = "
shift
;;
-f|--fox)
echo "fox used"
;;
--)
shift
break
esac
shift
done
# do script
我有以下脚本。
我想修改它,这样如果我要用两个选项调用 temp.sh
,我将不得不 space 它们。即:像 temp.sh -fc30
这样的脚本调用应该是无效的,而应该是 temp.sh -f -c 30
ARGS=$(getopt -o c:f -l "charlie:fox" -n "temp.sh" -- "$@");
#bad args
if [ $? -ne 0 ];
then
exit 1
fi
eval set --"$ARGS";
while true; do
case "" in
-c|--charlie)
shift;
if [ -n "" ]; then
echo "-c =: ";
shift;
fi
;;
-f|--fox)
shift;
echo "fox used";
;;
--)
shift;
break;
;;
esac
done
只是不要使用 getopt
。
#!/bin/bash
# parse options
while [[ $# -gt 0 ]]; do
case in
-c|--charlie)
echo " = "
shift
;;
-f|--fox)
echo "fox used"
;;
--)
shift
break
esac
shift
done
# do script