一些shell指令碼練習

來源:互聯網
上載者:User

一些shell指令碼練習 寫一個指令碼:1、設定變數FILE的值為/etc/passwd2、依次向/etc/passwd中的每個使用者問好,並顯示對方的shell,形如: Hello, root, your shell: /bin/bash3、統計一共有多少個使用者  #!/bin/bashFILE="/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/bashfor 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 ]doneecho "ID COUNT:$sumid" 寫一個指令碼,分別顯示當前系統上所有預設shell為bash的使用者和預設shell為/sbin/nologin的使用者,並統計各類shell下的使用者總數。顯示結果形如:BASH,3users,they are:root,redhat,gentoo NOLOGIN, 2users, they are:bin,ftp  #!/bin/bashBASH_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`,"    doneecho $BASH_Necho "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`,"    doneecho $NOLOGIN_N 寫一個指令碼:1) 顯示一個菜單給使用者:d|D) show disk usages.m|M) show memory usages.s|S) show swap usages.*) quit.2) 當使用者給定選項後顯示相應的內容;3) 當使用者選擇完成,顯示相應資訊後,不退出;而讓使用者再一次選擇,再次顯示相應內容;除了使用者使用quit;  #!/bin/bash#建立函數ss(){echo '---------------------------Options------------------------------'echo 'd|D) show disk usages.'echo 'm|M) show memory usages.'echo 's|S) show swap usages.'*) quit.#輸入一個選項參數Sread -p 'Please input your select:' S}#調用函數ss#建立一個while迴圈,只要是選項是dmsDMS,則進入case,若不是,則直接while迴圈結束while [[ $S == [dmsDMS] || $S == 'quit' || $S == * ]];docase $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]';;esacsdone  選項的使用  #!/bin/bashhelp(){    echo "-m memory"    echo "-s swap"      echo "-d disk space"     echo "-q quit"}helpwhile getopts msdqh selectdo    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 ";;    esacdone 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.