曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 6818|回复: 0
打印 上一主题 下一主题

[概念] shift 命令脚本应用举例

[复制链接]

4917

主题

5879

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34382
跳转到指定楼层
楼主
发表于 2009-1-12 22:32:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
默认情况下,脚本的位置参数为 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}"
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-5-15 06:54 , Processed in 0.079253 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表