我无法匹配 if 语句中的“--debug”。
我的目标是符合 POSIX 标准的脚本。
CHANNELS=;
set -- stable beta dev master --debug
echo "DEBUG: Before while $@";
while [ $# -gt 0 ]; do
echo "DEBUG: Inside while $1";
if [ ! $1 = -* ]; then
echo "DEBUG: Inside if $1";
CHANNELS="$CHANNELS $1";
fi
shift;
done
echo "DEBUG: After while $CHANNELS";
实际 -> $CHANNELS 具有“stable beta dev master --debug”
预期 -> $CHANNELS 应具有“stable beta dev master”
答案1
模式匹配是通过case
POSIX shell 中的构造完成的:
CHANNELS=
set -- stable beta dev master --debug
echo "DEBUG: Before while $@";
while [ "$#" -gt 0 ]; do
echo "DEBUG: Inside while $1";
case $1 in
(-*) ;;
(*)
echo "DEBUG: Inside case (*)"
CHANNELS="$CHANNELS $1";;
esac
shift
done
echo "DEBUG: After while $CHANNELS";