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

时间:2014-10-17 14:15:57

标签: bash command-line-arguments

大家好我想回答以下问题,但我很难解决问题:

  

编写一个脚本,打印已传递给它的所有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}"

谢谢

1 个答案:

答案 0 :(得分:1)

最简单的方式

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