zoukankan      html  css  js  c++  java
  • shell脚本练习

    写一个脚本:
    1、设定变量FILE的值为/etc/passwd
    2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,形如:
    Hello, root, your shell: /bin/bash
    3、统计一共有多少个用户

    #!/bin/bash
    FILE="/etc/passwd"
    num=`wc -l < $FILE`
    echo "User count:$num"
    for i in `seq 1 $num`;do
        Username=`head -$i $FILE |tail -1 | cut -d: -f1`
        Shell=`head -$i $FILE | tail -1 | cut -d: -f7`
        echo "Hello,$Username,your shell: $Shell"
    done 

    写一个脚本:
    1、添加10个用户user1到user10,密码同用户名;统计这10个用户的ID号之和;

    #!/bin/bash
    for i in `seq 1 10`; do
        useradd user$i
        echo "user$i" | passwd --stdin user$i
        userid=`grep "user$i" /etc/passwd | cut -d: -f3`
        sumid=$[ $sumid + $userid ]
    done
    echo "ID COUNT:$sumid"

    写一个脚本,分别显示当前系统上所有默认shell为bash的用户和默认shell为/sbin/nologin的用户,并统计各类shell下的用户总数。显示结果形如:
    BASH,3users,they are:
    root,redhat,gentoo

    NOLOGIN, 2users, they are:
    bin,ftp

    #!/bin/bash
    BASH_C=`grep '/bin/bash' /etc/passwd | wc -l`
    NOLOGIN_C=`grep '/sbin/nologin' /etc/passwd | wc -l`
    echo "BASH,$BASH_C'users',they are:"
    for i in `seq 1 $BASH_C`;do
        BASH_N="$BASH_N`grep '/bin/bash' /etc/passwd | head -$i | tail -1 | cut -d: -f1`,"    
    done
    echo $BASH_N | sed 's/,$//'
    echo "NOLOGIN,$NOLOGIN_C'users',they are:" for i in `seq 1 $NOLOGIN_C`;do NOLOGIN_N="$NOLOGIN_N`grep '/sbin/nologin' /etc/passwd | head -$i | tail -1 | cut -d: -f1`," done echo $NOLOGIN_N | sed 's/,$//'

    写一个脚本:
    1) 显示一个菜单给用户:
    d|D) show disk usages.
    m|M) show memory usages.
    s|S) show swap usages.
    *) quit.
    2) 当用户给定选项后显示相应的内容;
    3) 当用户选择完成,显示相应信息后,不退出;而让用户再一次选择,再次显示相应内容;除了用户使用quit;

    #!/bin/bash
    #创建函数s s(){
    echo '---------------------------Options------------------------------' echo 'd|D) show disk usages.' echo 'm|M) show memory usages.' echo 's|S) show swap usages.' *) quit.
    #输入一个选项参数S read
    -p 'Please input your select:' S }
    #调用函数s s
    #创建一个while循环,只要是选项是dmsDMS,则进入case,若不是,则直接while循环结束
    while [[ $S == [dmsDMS] || $S == 'quit' || $S == * ]];do case $S in [dD]) df -lh; [mM]) free -m -l | grep -v '^Swap';; [sS]) free -m -l | grep '^Swap';;
    quit)
       exit;;
    *)
    echo '---------------------------Warning!------------------------------'
    echo 'Please input your correct select,eg:[dDmMsS|quit]'
    ;;
    esac
    s done

     选项的使用

    #!/bin/bash
    help(){
        echo "-m memory"
        echo "-s swap"  
        echo "-d disk space" 
        echo "-q quit"
    }
    help
    while getopts msdqh select
    do
        case $select in
        m) free -m -l | grep -v '^Swap';;
        s) free -m -l | grep '^Swap';;
        d) df -lh;;
        q) exit;;
        h)help;;
        *)
         echo "Please select help options -h ";;
        esac
    done
    [星空刺] |-->一颗星辰一闪即逝,支撑它的唯有方向和目的
  • 相关阅读:
    腾讯//全排列
    腾讯//全排列
    腾讯//子集
    腾讯//子集
    腾讯///括号生成
    腾讯///括号生成
    腾讯//二叉树的最近公共祖先
    腾讯//二叉树的最近公共祖先
    腾讯//二叉搜索树的最近公共祖先
    腾讯//二叉搜索树的最近公共祖先
  • 原文地址:https://www.cnblogs.com/aaa103439/p/3346387.html
Copyright © 2011-2022 走看看