曲径通幽论坛

标题: shift 命令脚本应用举例 [打印本页]

作者: beyes    时间: 2009-1-12 22:32
标题: shift 命令脚本应用举例
默认情况下,脚本的位置参数为 9个,使用 shift n 允许操作超出 9个位置参数的情况。

shift 命令使脚本的参数左移。

下面设有 3 个文件: test-1.txt , test-2.txt , test-3.txt

它们的内容分别为:

test-1.txt:
[beyes@localhost shift_dir]$ cat test-1.txt
1
2
3
4

test-2.txt:
[beyes@localhost shift_dir]$ cat test-2.txt
1
2
3

test-2.txt:
[beyes@localhost shift_dir]$ cat test-3.txt
7
8
9

脚本程序如下
#!/bin/bash
#opt2
usage()    #参数输入不符要求提示
{
        echo "usage:`basename $0` filenames"
}
totalline=0

if [ $# -lt 2 ]; then
usage           #位置参数不能少于 2 个
        exit
fi

while [ $# -ne 0 ]
do
line=`cat $1 | wc -l`   #第一个位置参数对应文件的内容行数
        echo "$1 : ${line}"
        totalline=$[ $totalline+$line ] #累加每个文件内容行数的
        shift                   #左移位置参数
done
echo "---------------------------------"
echo "total:${totalline}"





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2