脚本后面所接的变量是否能够进行偏移 (shift) 呢?什么是偏移啊?我们直接以底下的范例来说明好了, 用范例说明比较好解释!
#!/bin/bash
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
echo -e "You'll shift one num,please wait... \n"
shift
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
echo -e "You'll shift shift 3 num,please wait... \n"
shift 3
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
执行结果:
[oracle@SOR_SYS~]$ sh shift.sh one two three four five six
Total parameter number is ==> 6
Your whole parameter is ==> 'one two three four five six'
You'll shift one num,please wait...
Total parameter number is ==> 5
Your whole parameter is ==> 'two three four five six'
You'll shift shift 3 num,please wait...
Total parameter number is ==> 2
Your whole parameter is ==> 'five six'
光看结果你就可以知道啦,那个 shift 会移动变量,而且 shift 后面可以接数字,代表拿掉最前面的几个参数的意思。 上面的运行结果中,第一次进行 shift 后他的显示情况是『one two three four five six』,所以就剩下五个啦!第二次直接拿掉三个,就变成『two three four five six 』啦! 这样这个案例可以了解了吗?理解了 shift 的功能了吗?