shell脚本while read line的使用
#### 题目要求
计算文档a.txt中每一行中出现的数字个数并且要计算一下整个文档中一共出现了几个数字。例如a.txt内容如下:
12aa*lkjskdj
alskdflkskdjflkjj
我们脚本名字为 ncount.sh, 运行它时:
bash ncount.sh a.txt
输出结果应该为:
2
0
sum:2
#### 参考答案
```
#!/bin/bashsum=0while read linedoline_n=`echo $line|sed 's/[^0-9]//g'|wc -L`echo $line_nsum=$[$sum+$line_n]done < $1echo "sum:$sum" |
如果你还记得管道的用法,这个结构应该不难理解吧。command命令的输出作为read循环的输入,这种结构长用于处理超过一行的输出,当然awk也很擅长做这种事
shell脚本while read line的使用的更多相关文章
- Shell 脚本报错 line x: [xxx: command not found
[root@VM-0-6-centos sh_scripts]# bash val.sh username: hello world! val.sh: line 5: [hello: command ...
- Shell脚本:while read line无法读取最后一行的问题
[1]Shell脚本:while read line无法读取最后一行的问题 刚刚利用shell脚本处理日志文件时,发现了一个问题:while read line无法读取到最后一行 通过编辑器可以看到待 ...
- linux脚本错误: line *: [: missing `]',linux编写shell脚本时的注意点
转载:https://www.cnblogs.com/bovenson/p/4548079.html 关于shell的一些注意点,粘贴自拉钩教育精选评论:测试开发核心技术 46 讲-->第6讲 ...
- Linux shell脚本编程(三)
Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...
- shell脚本俄罗斯方块游戏
亲自测试了一个大牛写的shell脚本,感兴趣可以看看,效果如下:
- 利用Shell脚本将MySQL表中的数据转化为json格式
脚本如下: #!/bin/bash mysql -s -phello test >.log <<EOF desc t1; EOF lines="concat_ws(',', ...
- 【转】70个经典的 Shell 脚本面试问题
我们为你的面试准备选择了 70 个你可能遇到的 shell 脚面问题及解答.了解脚本或至少知道基础知识对系统管理员来说至关重要,它也有助于你在工作环境中自动完成很多任务.在过去的几年里,我们注意到所有 ...
- shell脚本调试
转自:http://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/ 一. 前言 shell编程在unix/linux世界中使用得非常广泛,熟 ...
- 【转】常用的shell脚本
from:http://blog.sina.com.cn/s/blog_4152a9f501013r6c.html 常用的shell脚本 (2012-10-10 22:09:07) 转载▼ 标签: 杂 ...
随机推荐
- 关于eclipse常用的一些快捷键
Ctrl+Alt+H :查看方法被哪些代码调用了 Ctrl + Shif +O :自动引导类包 Ctrl+Shift+/ : 加上段注释 Ctrl+Shift+\ : 取消段注释 ALT+/ ...
- Python 写了个小程序,耗时一天,结果才100多行
from selenium import webdriver import selenium.webdriver.support.ui as ui from selenium.webdriver.co ...
- [Chrome] chrome 自动跳转到https
关键字眼: - static_upgrade_mode: FORCE_HTTPS - You cannot visit www.xxx.dev right now because the websit ...
- java实现人民币数字转大写(转)
原文:http://www.codeceo.com/article/java-currency-upcase.html 0 希望转换出来的结果为: 零元零角零分 1234 希望转换出来的结果为: 壹仟 ...
- LearnOpenGL
---------------------------------------------- LearnOpenGL ----------------------------------------- ...
- 【kafka学习之一】 kafka初识
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4一.kafka是什么? (1)kafka是一个高吞吐的分部式消息系统.( ...
- nginx ../logs/nginx.pid" failed (2: No such file or directory)
[1]nginx.pid相关 (1)可能出现两种场景: 1.1 nginx.pid文件不存在 发生现象:nginx: [error] open() "/usr/local/lib/ubcsr ...
- SQLSERVER 查询系统中的所有表的数量
SELECT a.name, b.rows FROM sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.id WHERE (a.type = ...
- java面向对象总结(一)
1. 对象的概念及面向对象的三个基本特征 面向对象的三大核心特性 面向对象开发模式更有利于人们开拓思维,在具体的开发过程中便于程序的划分,方便程序员分工合作,提高开发效率.面向对象程序设计有以下优点. ...
- 重写用户模型时报错AttributeError: type object ‘自定义类’ has no attribute ‘USERNAME_FIELD’
view中导入:from django.contrib.auth.models import AbstractBaseUser settings.py中设置了:AUTH_USER_MODEL='app ...