编写一个脚本,打印传递给它的所有10个参数.UNIX TERMINAL



嗨,伙计们,我正试图回答以下问题,但我很难解决:

编写一个脚本,打印所有传递给它的10个参数。一旦脚本打印了参数,将参数偏移2,然后再次打印

这是我的代码

#!/bin/sh
echo "$1 is now $1"
shift 
echo "$2 is now $2"
shift 
echo "$3 is now $3"
echo "$4 is now $4"
echo "$5 is now $5"
echo "$6 is now $6"
echo "$7 is now $7"
echo "$8 is now $8"
echo "$9 is now $9"
echo "${10} is now ${10}"

这是我在unix终端中的代码

#!/bin/sh
echo "$1 is now $1"
shift 
echo "$2 is now $2"
shift 
echo "$3 is now $3"
echo "$4 is now $4"
echo "$5 is now $5"
echo "$6 is now $6"
echo "$7 is now $7"
echo "$8 is now $8"
echo "$9 is now $9"
echo "${10} is now ${10}"

感谢

最简单的方法

x=0
for i in "$@"; do echo "$$((++x)) is $i"; done
shift
shift
x=0
for i in "$@"; do echo "$$((++x)) is now $i";done

最新更新