如何处理像这样的呼叫到我的shell脚本的最佳方法是什么?如何在unix shell中处理类似这样的东西?
./cars Mazda Toyota 2010 Honda BMW VW 2009
因此,它打印出:
马自达2010
丰田2010
本田2009年
宝马2009年
大众2009年
我不能使用数组,因为它是最简单的shell版本,所以它不支持它们。有任何想法吗?
如何处理像这样的呼叫到我的shell脚本的最佳方法是什么?如何在unix shell中处理类似这样的东西?
./cars Mazda Toyota 2010 Honda BMW VW 2009
因此,它打印出:
马自达2010
丰田2010
本田2009年
宝马2009年
大众2009年
我不能使用数组,因为它是最简单的shell版本,所以它不支持它们。有任何想法吗?
#!/bin/sh
CARS=
while [ $# -gt 0 ]; do
# Cheesy way to test if $1 is a year.
if [ "$1" -gt 0 ] 2> /dev/null; then
YEAR=$1
for CAR in $CARS; do
echo $CAR $YEAR
done
CARS=
else
# Add car to list
CARS="$CARS $1"
fi
# Process the next command-line argument.
shift
done
不错的答案。清晰易读。 – hlovdal 2009-11-28 22:51:28
#!/bin/sh
for i in "[email protected]"; do
case x"$i" in x[0-9]*)
for j in $justCars; do
echo $j $i
done
justCars=
continue
;;
esac
justCars="$justCars $i"
done
你可以使用数组
args="[email protected]"
echo $args | tr " " "\n" | awk 'BEGIN{d=1}
/^[0-9]+/{
for(i=1;i<=e;i++){ print a[i],$0 }
delete a
e=0
}
!/^[0-9]+/{ a[++e]=$0}'
输出
# ./shell.sh Mazda Toyota 2010 Honda BMW VW 2009
Mazda 2010
Toyota 2010
Honda 2009
BMW 2009
VW 2009
谁是最快的编写此Python脚本GAWK? – jldupont 2009-11-28 21:27:59
我想在unix shell中写这个不是Python – goe 2009-11-28 21:30:06