shell学习笔记(1):利用IFS打印用户和默认shell

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 09:13   11   0

参考资料为: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即可得到结果:

转载于:https://www.cnblogs.com/Shirlies/p/4168890.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP