Linux shell按行读文件
2015-10-27 22:20
549 查看
写在前面
这里采用的测试文件的内容如下:$ cat read_test.txt 1 a a,b,d,f 2 b alsdjf,apple,kdjf 3 c 163.2.201.1 4 d www.google.com 5 e http://blog.csdn.net/xia7139
另外,这里的脚本都会放在test.sh中运行,当然,我不说你也可以看出来^_^
Linux按行读文件的方法
Linux中按行读文件主要有三种方法,如下:重定向
while read line do echo $line done < read_test.txt $ sh test.sh 1 a a,b,d,f 2 b alsdjf,apple,kdjf 3 c 163.2.201.1 4 d www.google.com 5 e http://blog.csdn.net/xia7139
管道
cat read_test.txt | while read line do echo $line done $ sh test.sh 1 a a,b,d,f 2 b alsdjf,apple,kdjf 3 c 163.2.201.1 4 d www.google.com 5 e http://blog.csdn.net/xia7139
反引号取命令执行结果
for line in `cat read_test.txt` do echo $line done $ sh test.sh 1 a a,b,d,f 2 b alsdjf,apple,kdjf 3 c 163.2.201.1 4 d www.google.com 5 e http://blog.csdn.net/xia7139
总结:
这里不难看出,第三种方式中,除了换行符,空格和tab等也会被当成“行”间隔,使用时应该注意。
同时读入多个变量
Shell中可以将一中空格隔开的多个字段,同时分别读入多个变量,非常方便。代码如下:cat read_test.txt | while read number char content do echo "No.:$number char:$char content:$content" done $ sh test.sh No.:1 char:a content:a,b,d,f No.:2 char:b content:alsdjf,apple,kdjf No.:3 char:c content:163.2.201.1 No.:4 char:d content:www.google.com No.:5 char:e content:http://blog.csdn.net/xia7139
也可以采用下面的方式,得到的效果完全相同:
while read number char content do echo "No.:$number char:$char content:$content" done < read_test.txt
实战:查看用户及其所属组的脚本
Linux中的用户信息和组信息都是以文本文件的形式存储在/etc/passwd和
/etc/group文件中,通过读取这些文件,可以将用户和它们的组信息以更加友好的方式展现出来。下面是代码:
#!/bin/bash #This is a script to list all users in the Unix system. #Tested through under Bash. # #By lfqy. #Finished on 20141220_1512 # #Running step: #chmod a+x user_print.sh #./user_print.sh #Print a table head with printf, String Left Aligning with fixed length. printf "%-7s %-4s %-13s %-15s\n" User UID "PrimaryGroup" "SecondaryGroup" #Get the user info, user name, uid and gid, from /etc/passwd awk -F: '$3>=500 {print $1,$3,$4}' /etc/passwd | while read user uid gid do #Get the primary group name from /etc/group, using gid. priGro=`awk -F: '$3=="'$gid'" {print $1}' /etc/group` secGro='' #Get all the group not reserved for operating system. #For every group, test if it is the secondary group of $user. for gro_mem in `awk -F: 'BEGIN{OFS=":"}$3>="'$gid'" {print $1,$4}' /etc/group` do #Get the group member secMem=":${gro_mem#*:}" #Get the group name groName=${gro_mem%%:*} #Testing, ':' existing for the case lfqy and lfqy0 if [[ $secMem = *",$user"* ]] || [[ $secMem = *":$user"* ]] then secGro=$secGro","$groName #echo "secGro:" $secGro fi done printf "%-7s %-4s %-13s %s\n" $user $uid $priGro ${secGro#*,} done $ sh user_print_final.sh User UID PrimaryGroup SecondaryGroup lfqy 500 lfqy
运行环境:
CentOS
Release 6.4 (Final)
Kernel Linux 2.6.32-358.el6.x86_64
GNOME 2.28.2
上面的脚本实际上是我好长时间之前练手写的,现在看来有些命令的用法,我记得也不是特别清楚了。如有疑问,自行Google。
相关文章推荐
- Jmeter之Bean shell使用(一)
- 初学shell,今天遇到由wget下载到本地的网页源代码的乱码问题,无聊的写了一个转码的脚本
- 【shell文字】mysql每日备份shell文字
- SSH Secure Shell Client远程连接到虚拟机的UBUNTU系统
- shell脚本中的[]/[[]]区别
- Eclipse 安装 EasyShell 插件(打开项目所在本地磁盘位置)
- 使用Xshell连接到Linux
- shell脚本判断文件类型
- shell三件利器——管道,重定向和backtick
- GitHub的使用(Git Shell)
- linux的环境变量文件enviroment 、profile、bashrc和.bashrc以及.bash_profile
- ubuntu下sshd服务与SSH SECURE SHELL CLIENT的使用
- MySQL问题解决:-bash:mysql:command not found
- shell脚本检查alert警告日志中是否存在ORA-错误
- Bash 为何要发明 shopt 命令
- Shell脚本遍历文件夹
- 升级10.11后使用CocoaPod出现-bash: pod: command not found 解决办法
- caffe 使用shell自动生成train.txt & val.txt
- SSH Secure Shell Client用public key认证登录
- The study of crack(1.Shelling FSG v2.0)