zoukankan      html  css  js  c++  java
  • shell学习笔记(1):利用IFS打印用户和默认shell

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

  • 相关阅读:
    推荐Windows下SVN服务器端和客户端工具软件
    QT的一些小知识
    Qt
    Qt
    ADB
    HTTP
    Python
    项目附
    项目
    架构
  • 原文地址:https://www.cnblogs.com/Shirlies/p/4168890.html
Copyright © 2011-2022 走看看