shell 练习题(6)

  1. 显示当前系统上所有用户的shell,要求,每种shell只显示一次;

    # cut -d: -f7 /etc/passwd | sort -u
    
  2. 取出/etc/passwd文件的第7行;

    # head -7 /etc/passwd | tail -1
    
  3. 显示第3题中取出的第7行的用户名;

    # head -7 /etc/passwd | tail -1 | cut -d: -f1
    
    # head -7 /etc/passwd | tail -1 | cut -d: -f1 | tr 'a-z' 'A-Z'
    
  4. 统计/etc目录下以P或p开头的文件个数;

    # ls -d /etc/[Pp]* | wc -l
    
  5. 写一个脚本,用for循环实现显示/etc/init.d/functions、/etc/rc.d/rc.sysinit和/etc/fstab各有多少行;

    for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab; do
        wc -l $fileName
    done
    
    #!/bin/bash
    for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab; do
        lineCount=`wc -l $fileName | cut -d' ' -f1`
        echo "$fileName: $lineCount lines."
    done
    
    #!/bin/bash
    for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab; do
        echo "$fileName: `wc -l $fileName | cut -d' ' -f1` lines."
    done
    
  6. 写一个脚本,将上一题中三个文件的复制到/tmp目录中;用for循环实现,分别将每个文件的最近一次的修改时间改为2016年12月15号15点43分;

    for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab; do
        cp $fileName /tmp
        baseName=`basename $fileName`
        touch -m -t 201109151327 /tmp/$baseName
    done
    
  7. 写一个脚本, 显示/etc/passwd中第3、7和11个用户的用户名和ID号;

    for lineNo in 3 7 11; do
        userInfo=`head -n $lineNo /etc/passwd | tail -1 | cut -d: -f1,3`
        echo -e "User: `echo $userInfo | cut -d: -f1`\nUid: `echo $userInfo |cut -d: -f2`"
    done
    
  8. 显示/proc/meminfo文件中以大小写s开头的行;

    # grep "^[sS]" /proc/meminfo
    # grep -i "^s" /proc/meminfo
    
  9. 取出默认shell为非bash的用户;

    # grep -v "bash$" /etc/passwd | cut -d: -f1
    
  10. 取出默认shell为bash的且其ID号最大的用户;

    # grep "bash$" /etc/passwd | sort -n -t: -k3 | tail -1 | cut -d: -f1

 (答案后续完善)

猜你喜欢

转载自blog.csdn.net/weixin_38280090/article/details/82113401