1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現;
#!/bin/bash # declare -i sumlogin=0 declare -i sumnologin=0 for i in $(cut /etc/passwd -d: -f7);do if [[ "$i" == "/sbin/nologin" ]];then let sumnologin++ else let sumlogin++ fi done echo "totle nologin user $sumnologin\ntotle loging user $sumlogin" 執行結果: ~]# bash loginuser.sh totle nologin user 23 totle loging user 9
2、寫一個腳本 (1) 獲取當前主機的主機名,保存于hostname變量中; (2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com; (3) 否則,則顯示當前主機名;
#!/bin/sh # hostname=$(hostname) if [[ "$hostname" == "localhost.localdomain" ]];then hostname www.void.com echo "hostname change to $(hostname) ok" else echo "hostname is $hostname" fi 執行結果: a:hostname=localhost.localdomain ~]# bash hostname.sh hostname change to www.void.com ok b:hostname=test ~]# bash hostname.sh hostname is test
3、寫一個腳本,完成如下功能 (1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在; (2) 如果存在,則顯示此設備上的所有分區信息;
#!/bin/bash # if [ $# -lt 1 ];then echo "At least one arg!" exit 1 else if fdisk -l $1 &>/dev/null;then echo -e "$(fdisk -l $1)" else echo "The device is not exist!" fi fi 執行結果: a:參數不存在 ~]# bash device.sh At least one arg! b:設備不存在 ~]# bash device.sh /dev/sdb The device is not exist! c:設備存在 ~]# bash device.sh /dev/sda 磁盤 /dev/sda:21.5 GB, 21474836480 字節,41943040 個扇區 Units = 扇區 of 1 * 512 = 512 bytes 扇區大小(邏輯/物理):512 字節 / 512 字節 I/O 大小(最小/最佳):512 字節 / 512 字節 磁盤標簽類型:dos 磁盤標識符:0x0004bb94 設備 Boot Start End Blocks Id System /dev/sda1 *2048 1026047 512000 83 Linux /dev/sda2 10260484194303920458496 8e Linux LVM
4、寫一個腳本,完成如下功能 腳本能夠接受一個參數; (1) 如果參數1為quit,則顯示退出腳本, 并執行正常退出; (2) 如果參數1為yes,則顯示繼續執行腳本; (3) 否則,參數1為其它任意值,均執行非正常退出;
#!/bin/bash # read -p "Do you want to executing the script continue?[quit or yes]" arg case $arg in quit) echo "script is quit" exit 0 ;; yes) echo "continue!" ;; *) echo "what!!!" exit 2 ;; esac 執行結果: ~]# bash casetest.sh Do you want to executing the script continue?[quit or yes]quit script is quit ~]# echo $? 0 ~]# bash casetest.sh Do you want to executing the script continue?[quit or yes]yes continue! ~]# echo $? 0 ~]# bash casetest.sh Do you want to executing the script continue?[quit or yes]aaaaaa what!!! ~]# echo $? 2
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 # time=$(date +%Y%m%d) read -p "Please choose one compress Tool[gzip/bzip2/xz]" arg case $arg in gzip) tar -zcf /backups/etc-$time.tar.gz /etc &>/dev/null echo -e "compress etc-$time.tar.gz ok" ;; bzip2) tar -jcf /backups/etc-$time.tar.bz2 /etc &>/dev/null echo -e "compress etc-$time.tar.bz2 ok" ;; xz) tar -Jcf /backups/etc-$time.tar.xz /etc &>/dev/null echo -e "compress etc-$time.tar.xz ok" ;; *) echo "error compress Tool" ;; esac 執行結果: ~]# bash tar.sh Please choose one compress Tool[gzip/bzip2/xz]gzip compress etc-20161121.tar.gz ok ~]# ls /backups etc-20161121.tar.gz ~]# bash tar.sh Please choose one compress Tool[gzip/bzip2/xz]bzip2 compress etc-20161121.tar.bz2 ok ~]# ls /backups etc-20161121.tar.bz2 etc-20161121.tar.gz ~]# bash tar.sh Please choose one compress Tool[gzip/bzip2/xz]xz compress etc-20161121.tar.xz ok ~]# ls /backups etc-20161121.tar.bz2 etc-20161121.tar.gz etc-20161121.tar.xz ~]# bash tar.sh Please choose one compress Tool[gzip/bzip2/xz]test error compress Tool
6、寫一個腳本,接受一個路徑參數: (1) 如果為普通文件,則說明其可被正常訪問; (2) 如果是目錄文件,則說明可對其使用cd命令; (3) 如果為符號鏈接文件,則說明是個訪問路徑; (4) 其它為無法判斷;
#!/bin/bash # if [ $# -lt 1 ];then echo "At least one arg!" exit 1 fi if ! [ -e $1 ];then echo "file is not exist" exit 2 fi if [ -f $1 ];then echo "this file can be access normal" elif [ -d $1 ];then echo "this file can be cd" elif [ -h $1 ];then echo "this is a link file" else echo "unkown" fi 執行結果: ~]# bash file.sh At least one arg! ~]# bash file.sh aaaa file is not exist ~]# bash file.sh /etc this file can be cd ~]# bash file.sh /etc/passwd this file can be access normal
7、寫一個腳本,取得當前主機的主機名,判斷 (1) 如果主機名為空或為localhost,或為"(none)",則將其命名為mail.magedu.com; (2) 否則,顯示現有的主機名即可;
#!/bin/bash # hostname=$(hostname) if [ -z "$hostname" -o "$hostName" == "localhost.localdomain" -o "$hostname" == "localhost" ];then hostname mail.void.com echo "hostname has change to $(hostname)" else echo "$hostname" fi 執行結果: ~]# hostname localhost.localdomain ~]# bash changehost.sh hostname has change to mail.void.com ~]# hostname test ~]# hostname test ~]# bash changehost.sh hostname is test
8、寫一腳本,接受一個用戶名為參數; (1) 如果用戶的id號為0,則顯示其為管理員; (2) 如果用戶的id號大于0且小于500, 則顯示其為系統用戶; (3) 否則,則顯示其為普通用戶;
#!/bin/bash # if [ $# -lt 1 ];then echo "at lease one arg" exit 1 fi if ! id $1 &>/dev/null;then echo "no such user" else userid=$(id -u $1) if [ $userid -eq 0 ];then echo "this is admin" elif [ $userid -gt 0 ] && [ $userid -lt 200 ];then echo "this is system user" else echo "this is normal user" fi fi 執行結果: ~]# bash readuser.sh at lease one arg ~]# bash readuser.sh root this is admin ~]# bash readuser.sh void this is normal user ~]# bash readuser.sh test no such user ~]# bash readuser.sh mail this is system user
10、寫一個腳本,傳遞一個用戶名參數給腳本; (1) 如果用戶的id號大于等于500,且其默認shell為以sh結尾的字符串,則顯示“a user can log system.”類的字符串; (2) 否則,則顯示無法登錄系統;
#!/bin/bash # if [ $# -lt 1 ];then echo "at least one arg" exit 1 fi if ! id $1 &>/dev/null;then echo "no such user" exit 2 fi userid=$(id -u $1) if [ $userid -ge 500 ] && grep "^$1.*sh$" /etc/passwd &>/dev/null;then echo "a user can log system" else echo "the user can't login" fi 執行結果: ~]# useradd test1 -u 550 -s /sbin/nologin ~]# useradd test2 -u 300 -s /bin/bash ~]# useradd test3 -u 551 -s /bin/bash ~]# bash userread.sh at least one arg ~]# bash userread.sh ddddddd no such user ~]# bash userread.sh test1 the user can't login ~]# bash userread.sh test2 the user can't login ~]# bash userread.sh test3 a user can log system
原創文章,作者:N23-蘇州-void,如若轉載,請注明出處:http://www.www58058.com/60331
非常不錯,完整地看來下有腳本有輸出,展示出了扎實的腳本基本功,期待下次作業~