1、寫一個腳本,判斷當前系統用戶shell是否都為可登陸shell(即非/sbin/nologin),分別計算兩類用戶的個數(通過比較字符串實現)
#!/bin/bash # check the user could login by default or not,and how many it is # 20161211 declare logini declare nologinj for ((i=1;i<=$(cat /etc/passwd|wc -l);i++)) do defaultshell=$(head -$i /etc/passwd|sed '$!d'|cut -d':' -f7) if [ $defaultshell == /sbin/nologin ];then logini=$(($logini+1)) else nologinj=$(($nologinj+1)) fi done echo "the default login_user has $logini,the others has $nologinj" 或者 #!/bin/bash # # declare nologini declare othersj for ((i=1;i<=$(cat /etc/passwd|wc -l);i++)) do if [ $(head -$i /etc/passwd|sed '$!d'|sed 's/^.*://') == /sbin/nologin ] then nologini=$(($nologini+1)) else othersj=$(($othersj+1)) fi done echo "the default login users is $nologini,the others is $othersj"
2、寫一個腳本:1)獲取當前主機的主機名,保存在變量hostname中;2)判斷此變量的值是否是localhost,如果是,將當前主機名更改為www.mageedu.com;3)如果不是,則顯示主機名
#!/bin/bash # check the $HOSTNAME if it's localhost,then change it to www.mageedu.com ;else display it # 20161212 hostname=$(hostname) if [ $hostname == localhost ] ||[ $hostname == localhost.localdomain ] then hostname "www.mageedu.com" echo -e 'I change HOSTNAME is $hostname\n' else echo -e $hostname fi
3、寫一個腳本實現如下功能:1)傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在;2)如果存在,則顯示此設備上所有分區信息
#!/bin/bash # please input a device ,check if it's a block device ,so display the partition # read -p "Please input a block device,like:sda、sdb:" device if [ -b /dev/$device ] then echo "the $device is a block device:/dev/$device;and its partition like:" lsblk /dev/$device else echo "$device is not exist!" fi
4、寫一個腳本實現以下功能:腳本能接受一個參數1)如果參數1為quit,則顯示退出腳本,并執行正常退出;2)如果參數1為yes,則顯示繼續執行腳本;3)否則,參數1為其他任意值,均執行非正常退出
#!/bin/bash # while you input 'quit',the shell exit 0;input 'yes' the shell continue;input other command the shell exit 1 # read -p "please input yes,quit,*:" l case $l in yes) echo "the shell end with no error" exit 0 ;; quit) echo "the shell continue" continue ;; *) echo "the shell out within error" exit 1 ;; esac
5、寫一個腳本,實現以下功能,傳遞一個參數給腳本,此參數為gzip,bzip2或者xz三者之一:1)如果參數1的值為gzip,則使用tar和gzip歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.gz;2)如果參數1的值為bzip2,則使用tar和bzip2歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.bz2;3)如果參數1是xz,則使用tar和xz歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.xz;4)如果是其他值,則顯示錯誤的壓縮工具,并執行非正常退出
#!/bin/bash # tar /etc/* with bzip2,gzip,xz # if [ $# == 0 ] then echo " you must choose a option(like bzip2,gzip,xz)!" else case $1 in gzip) tar -czvf /backups/etc-20161212.tar.gz /etc/* ;; bzip2) tar -P -cjvf /backups/etc-20161212.tar.bz2 /etc/* ;; xz) tar -Jcvf /backups/etc-20161212.tar.xz /etc/* ;; esac ls -al /backups fi
6、寫一個腳本接受一個路徑參數:1)如果是普通文件,則說明其可被正常訪問;2)如果是目錄文件,則說明可對其使用cd命令;3)如果是符號鏈接文件,則說明是個訪問路徑;4)其他為無法判斷
#!/bin/bash # check the file or directory 's type # 20161213 if [ $# -eq 0 ];then echo "you must insert a file or directory!" elif [ -f $1 ];then echo "$1 is a file and you can read/write it" elif [ -d $1 ];then echo "$1 is a directory,you can access it with COMMAND 'cd'" elif [ -l $1 ];then echo "$1 is a link file" else echo "i can't fond what type it is" fi
7、寫一個腳本,取得當前主機的主機名,判斷1)如果主機名為空或者為localhost,或者為“(none)”,則將其命名為mailmagedu.com;2)負責,顯示現有的主機名即可
同題2
8、寫一個腳本,接受一個用戶名為參數:1)如果用戶的id號為0,則顯示為管理員;2)如果用戶的id號大于0且小于500,則顯示其為系統用戶;3)否則,顯示其為普通用戶
#!/bin/bash # check the user's type,it's operater or ordinery user # 20161213 if [ $# -eq 0 ];then echo "Please input a username!just one" elif `id -u $1>>/dev/null 2>&1`;[ $? != 0 ];then echo "the user $1 isn't fond" && exit 1 elif [ `id -u $1` -eq 0 ];then echo "$1 is root" elif [ `id -u $1` -lt 500 ];then echo "$1 is a operator" else echo "$1 is a user" fi
9、寫一個腳本,傳遞一個用戶名參數給腳本;如果用戶的id號大于等于500,且其默認shell為以sh結尾的字符串,則顯示“a user can log system"類的字符串
#!/bin/bash # check user's ID greater than 500 and the last two character of the default SHELL is sh # 20161213 if [ $# -eq 0 ];then echo 'Please insert a username!' elif `id -u $1 >>/dev/null 2>&1`;[ $? != 0 ];then echo "The user isn't fond" elif [ $(id -u $1) -ge 500 ] && [ $(grep $1 /etc/passwd|sed 's/.*://'|grep -o "sh$") == sh ];then echo "The user $1 can login system" else echo "The user'ID is $(id -u $1),the user cannt login" fi
原創文章,作者:396064847,如若轉載,請注明出處:http://www.www58058.com/63602
腳本可以先運行下~~第2個腳本,犯了一個強弱引用的錯誤;第3個腳本,輸入 “yes” 和 “quit” 的執行結果和要求不一致;第3個腳本,漏掉了任意項;第7個腳本,建議動手換個方式實現下;等等
不過整體看下來還是不錯的。加油~