测试用例:
./ReadArguments.sh -AAA aaa -BBB bbb -CCC ccc -DDD dddd
File Name: ./ReadArguments.sh
AAA: aaa
BBB: bbb
CCC: ccc
DDD: dddd
详细脚本:
cat ReadArguments.sh
#!/bin/sh
echo "File Name: $0"
AAA=""
BBB=""
CCC=""
DDD=""
HandleInputArguments()
{
if [ x"-AAA" = x"$1" ]; then
AAA=$2
fi
if [ x"-BBB" = x"$1" ]; then
BBB=$2
fi
if [ x"-CCC" = x"$1" ]; then
CCC=$2
fi
if [ x"-DDD" = x"$1" ]; then
DDD=$2
fi
}
OldArgument=""
for var in "$@"
do
c=${var:0:1}
if [ "-" = "$c" ]; then
OldArgument=$var
else
if [ x"" != x"${OldArgument}" ]; then
HandleInputArguments ${OldArgument} "${var}"
fi
fi
done
echo "AAA: "$AAA
echo "BBB: "$BBB
echo "CCC: "$CCC
echo "DDD: "$DDD