Shirlies
宁静专注认真的程序媛~

参考资料为:linux shell脚本攻略 作者sarath Lakshman 人民邮电出版社

shell:读取文件的每一行内容并输出 的写法1

目的:读取passwd文件,获得用户名和其默认的shell,并打印出来

首先cp /etc/passwd ./passwd_cp
shell脚本get_usr_shell.sh内容如下:

#!/bin/bash

function get_line_usr_shell(){
#    echo $#

    line="$1"
    oldIFS=$IFS

    IFS=":"
    count=0

    for item in $line
    do
        [ $count -eq 0 ] && user=$item
        [ $count -eq 6 ] && shell=$item
        let count++
    done
    IFS=$oldIFS
    echo $user\'s shell is $shell
}

if [ $# -gt 1 ]
then
    echo "parameter is more one in main"
    exit 1
fi

while read line
do
#    echo $line
    get_line_usr_shell $line
done < $1
shell脚本

然后在终端输入./get_usr_shell passwd_cp即可得到结果:

posted on 2014-12-17 11:00  Shirlies  阅读(558)  评论(0编辑  收藏  举报