手机版
你好,游客 登录 注册
背景:
阅读新闻

Shell脚本编程初体验

[日期:2015-06-09] 来源:Linux中国  作者:Linux [字体:大 中 小]

循环

是时候来改进我们的脚本了。如果用户一整天都在接电话,如果每次都要去运行,这岂不是很麻烦?让我们让这些活动都永无止境地循环去吧:

  1. [root@localhost ~]# cat note.sh
  2. #!/bin/bash
  3. whiletrue
  4. do
  5. read -p "Phone number: " phone
  6. now=`date "+%Y.%m.%d %H:%M:%S"`
  7. read -p "Name: " name
  8. read -p "Issue: " issue
  9. echo "$now/$phone/$name/$issue">>data.txt
  10. done

我已经交换了read phone和now=date行的位置。这是因为我想要在输入电话号码后再获得时间。如果我把它放在循环的首行,那么循环一次后,变量 now 就会在数据存储到文件中后马上获得时间。而这并不好,因为下一次呼叫可能在20分钟后,甚至更晚。

  1. [root@localhost ~]#./note.sh
  2. Phone number:123
  3. Name:Jim
  4. Issue:Script still not works.
  5. Phone number:777
  6. Name:Daniel
  7. Issue: I broke my monitor
  8. Phone number:^C
  9. [root@localhost ~]# cat data.txt
  10. 2015.04.2321:38:56/123/Jim/Script hanging.
  11. 2015.04.2321:43:50/321/Susane/Mouse was stolen
  12. 2015.04.2321:47:55/123/Jim/Script still not works.
  13. 2015.04.2321:48:16/777/Daniel/I broke my monitor
  14. [root@localhost ~]#

注意: 要从无限循环中退出,你可以按[Ctrl]+[C]键。Shell会显示^表示 CTRL 键。

 

使用管道重定向

让我们添加更多功能到我们的“弗兰肯斯坦(Frankenstein)”,我想要脚本在每次呼叫后显示某个统计数据。比如说,我想要查看各个号码呼叫了我几次。对于这个,我们应该cat文件data.txt:

  1. [root@localhost ~]# cat data.txt
  2. 2015.04.2321:38:56/123/Jim/Script hanging.
  3. 2015.04.2321:43:50/321/Susane/Mouse was stolen
  4. 2015.04.2321:47:55/123/Jim/Script still not works.
  5. 2015.04.2321:48:16/777/Daniel/I broke my monitor
  6. 2015.04.2322:02:14/123/Jimmy/New script also not working!!!
  7. [root@localhost ~]#

现在,所有输出我们都可以重定向到cut命令,让cut来把每行切成一块一块(我们使用分隔符“/”),然后打印第二个字段:

  1. [root@localhost ~]# cat data.txt | cut -d"/"-f2
  2. 123
  3. 321
  4. 123
  5. 777
  6. 123
  7. [root@localhost ~]#

现在,我们可以把这个输出重定向打另外一个命令sort:

  1. [root@localhost ~]# cat data.txt | cut -d"/"-f2|sort
  2. 123
  3. 123
  4. 123
  5. 321
  6. 777
  7. [root@localhost ~]#

然后只留下唯一的行。要统计唯一条目,只需添加-c键到uniq命令:

  1. [root@localhost ~]# cat data.txt | cut -d"/"-f2 | sort | uniq -c
  2. 3123
  3. 1321
  4. 1777
  5. [root@localhost ~]#

只要把这个添加到我们的循环的最后:

  1. #!/bin/bash
  2. whiletrue
  3. do
  4. read -p "Phone number: " phone
  5. now=`date "+%Y.%m.%d %H:%M:%S"`
  6. read -p "Name: " name
  7. read -p "Issue: " issue
  8. echo "$now/$phone/$name/$issue">>data.txt
  9. echo "===== We got calls from ====="
  10. cat data.txt | cut -d"/"-f2 | sort | uniq -c
  11. echo "--------------------------------"
  12. done

运行:

  1. [root@localhost ~]#./note.sh
  2. Phone number:454
  3. Name:Malini
  4. Issue:Windows license expired.
  5. =====We got calls from=====
  6. 3123
  7. 1321
  8. 1454
  9. 1777
  10. --------------------------------
  11. Phone number:^C

当前场景贯穿了几个熟知的步骤:

  • 显示消息
  • 获取用户输入
  • 存储值到文件
  • 处理存储的数据

但是,如果用户有点责任心,他有时候需要输入数据,有时候需要统计,或者可能要在存储的数据中查找一些东西呢?对于这些事情,我们需要使用switches/cases,并知道怎样来很好地格式化输出。这对于在shell中“画”表格的时候很有用。


via: http://linoxide.com/linux-shell-script/guide-start-learning-shell-scripting-scratch/

作者:Petras Liumparas 译者:GOLinux 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-06/118574.htm

linux
相关资讯       shell编程  Shell脚本编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款