Linux shell按行读文件

2015-10-27  本文已影响269人  SpaceCat

写在前面

这里采用的测试文件的内容如下:

$ 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中按行读文件主要有三种方法,如下:

  1. 重定向

     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
    
  2. 管道

     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
    
  3. 反引号取命令执行结果

     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。

上一篇 下一篇

猜你喜欢

热点阅读