Shell(使用shift命令处理命令行参数)
(2018-08-31 15:26:38)
shift命令的语法:
shift [n]
n必须是一个小于或等于“$#"的非负整数。
如果n为0,位置参数将不会改变。
如果没有指定n,那么它将被默认设为1。
如果n大于$#,位置参数不会改变。
如果n大于$#或小于0,此命令的返回状态码将大于0,否则为0。
每次移位之后,特殊变量$#的值也会调整。而特殊变量$0并不参与移位操作。
读取特殊变量$1的值,然后运行命令shift,再次读取特殊变量$1的值,我们将得到特殊变量$2的值。然后再次运行命令shift,再次读取特殊变量$1的值,将得到特殊变量$3的值,以此类推。只要$#的值不为0,我们就可以在while循环中进行迭代,获取特殊变量$1的值,运行shift命令,然后再次读取$1的值,来依次获得所有传递的命令行参数。
[root@aa ~]# cat getCMLParam.sh
#!/bin/bash -
# 如果命令行参数的个数不为0,则继续while循环,否则退出循环
while [ $# -ne 0 ]
do
#
打印特殊变量$1的值,及特殊变量$#的值
echo
"Current Parameter: $1, Remaining $#."
#
将位置参数左移一位
shift
done
[root@aa ~]# ./getCMLParam.sh one two three four five
Current Parameter: one, Remaining 5.
Current Parameter: two, Remaining 4.
Current Parameter: three, Remaining 3.
Current Parameter: four, Remaining 2.
Current Parameter: five, Remaining 1.
-------------------------------判断$1是否为空------------------------------
[root@aa ~]# cat getCMLParam.sh
#!/bin/bash -
# 如果特殊变量$1的值不为空,则继续执行while循环,否则退出循环
while [ -n "$1" ]
do
#
打印特殊变量$1的值,及特殊变量$#的值
echo
"Current Parameter: $1, Remaining $#."
#
将位置参数左移一位
shift
done
[root@aa ~]# ./getCMLParam.sh one two three four five
Current Parameter: one, Remaining 5.
Current Parameter: two, Remaining 4.
Current Parameter: three, Remaining 3.
Current Parameter: four, Remaining 2.
Current Parameter: five, Remaining 1.
-------------------------shift 5-------------------------------------
[root@aa ~]# cat shift5param.sh
#!/bin/bash -
# 如果特殊变量$1的值不为空,则继续执行while循环,否则退出循环
while [ -n "$1" ]
do
echo
"Current parameter: $1, Remaining $#."
shift
5
if
[ $? -ne 0 ]
then
break
fi
done
[root@aa ~]# ./shift5param.sh one two three four five six seven
Current parameter: one, Remaining 7.
Current parameter: six, Remaining 2.
shift [n]
n必须是一个小于或等于“$#"的非负整数。
如果n为0,位置参数将不会改变。
如果没有指定n,那么它将被默认设为1。
如果n大于$#,位置参数不会改变。
如果n大于$#或小于0,此命令的返回状态码将大于0,否则为0。
每次移位之后,特殊变量$#的值也会调整。而特殊变量$0并不参与移位操作。
读取特殊变量$1的值,然后运行命令shift,再次读取特殊变量$1的值,我们将得到特殊变量$2的值。然后再次运行命令shift,再次读取特殊变量$1的值,将得到特殊变量$3的值,以此类推。只要$#的值不为0,我们就可以在while循环中进行迭代,获取特殊变量$1的值,运行shift命令,然后再次读取$1的值,来依次获得所有传递的命令行参数。
[root@aa ~]# cat getCMLParam.sh
#!/bin/bash -
# 如果命令行参数的个数不为0,则继续while循环,否则退出循环
while [ $# -ne 0 ]
do
done
[root@aa ~]# ./getCMLParam.sh one two three four five
Current Parameter: one, Remaining 5.
Current Parameter: two, Remaining 4.
Current Parameter: three, Remaining 3.
Current Parameter: four, Remaining 2.
Current Parameter: five, Remaining 1.
-------------------------------判断$1是否为空------------------------------
[root@aa ~]# cat getCMLParam.sh
#!/bin/bash -
# 如果特殊变量$1的值不为空,则继续执行while循环,否则退出循环
while [ -n "$1" ]
do
done
[root@aa ~]# ./getCMLParam.sh one two three four five
Current Parameter: one, Remaining 5.
Current Parameter: two, Remaining 4.
Current Parameter: three, Remaining 3.
Current Parameter: four, Remaining 2.
Current Parameter: five, Remaining 1.
-------------------------shift 5-------------------------------------
[root@aa ~]# cat shift5param.sh
#!/bin/bash -
# 如果特殊变量$1的值不为空,则继续执行while循环,否则退出循环
while [ -n "$1" ]
do
done
[root@aa ~]# ./shift5param.sh one two three four five six seven
Current parameter: one, Remaining 7.
Current parameter: six, Remaining 2.

加载中…