1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現;
awk -F: '{if($NF!="/sbin/nologin") print $1}' /etc/passwd | wc -l
2、寫一個腳本 (1) 獲取當前主機的主機名,保存于hostname變量中; (2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com; (3) 否則,則顯示當前主機名;
#!/bin/bash
a=$(hostname)
[ “$a” == “localhost” ] && hostname www.magedu.com
hostname
3、寫一個腳本,完成如下功能 (1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在; (2) 如果存在,則顯示此設備上的所有分區信息;
#!/bin/bash [ -b $1 ] && fdisk -l [root@localhost dev]# bash devic.sh /dev/sda Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000a1d43 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 41943039 19921920 8e Linux LVM Disk /dev/mapper/cl-root: 18.2 GB, 18249416704 bytes, 35643392 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/cl-swap: 2147 MB, 2147483648 bytes, 4194304 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes [root@localhost dev]# bash devic.sh /dev/sdf [root@localhost dev]# echo $? 1
~
4、寫一個腳本,完成如下功能 腳本能夠接受一個參數; (1) 如果參數1為quit,則顯示退出腳本,并執行正常退出; (2) 如果參數1為yes,則顯示繼續執行腳本; (3) 否則,參數1為其它任意值,均執行非正常退出;
#!/bin/bash [ "$1" == "quit" ] && echo " exit the processing " && exit [ "$1" == "yes" ] && echo " continue processing the scripts " [ "$1" != "quit" -a "$1" != "yes" ] && echo " error " && exit [root@localhost tmp]# bash test3.sh quit exit the processing [root@localhost tmp]# bash test3.sh yes continue processing the scripts [root@localhost tmp]# bash test3.sh 1334 error [root@localhost tmp]#
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 [ "$1" == "gzip" ] && tar -zcf /backups/etc-$(date +%Y%m%d).tar.gz /etc [ "$1" == "bzip2" ] && tar -jcf /backups/etc-$(date +%Y%m%d).tar.bz2 /etc [ "$1" == "xz" ] && tar -Jcf /backups/etc-$(date +%Y%m%d).tar.xz /etc [ "$1" != "gzip" -a "$1" != "bzip2" -a "$1" != "xz" ] && echo "error" && exit [root@localhost backups]# bash tartool.sh xz tar: Removing leading `/' from member names [root@localhost backups]# ls etc etc-20170322.tar.gz etc-20172203 tartool.sh etc-20170322.tar.bz2 etc-20170322.tar.xz etc-.tar.xz
6、寫一個腳本,接受一個路徑參數: (1) 如果為普通文件,則說明其可被正常訪問; (2) 如果是目錄文件,則說明可對其使用cd命令; (3) 如果為符號鏈接文件,則說明是個訪問路徑; (4) 其它為無法判斷;
#!/bin/bash if [ -f $1 ]; then echo " this file can be visited " elif [ -d $1 ]; then echo " it is directory and and can be excuted by command cd " elif [ -L $1 ]; then echo " it is a path to visit " elif [ -e $1 ]; then echo " file exit but can not identify filetype" else echo " can not be identified" fi
7、寫一個腳本,取得當前主機的主機名,判斷 (1) 如果主機名為空或為localhost,或為””(none)””,則將其命名為mail.magedu.com; (2) 否則,顯示現有的主機名即可;
#!/bin/bash a=$(hostname) [ "$a" == "localhost" -o "$a" == "none" ] && hostname www.magedu.com hostname
8、寫一腳本,接受一個用戶名為參數; (1) 如果用戶的id號為0,則顯示其為管理員; (2) 如果用戶的id號大于0且小于500, 則顯示其為系統用戶; (3) 否則,則顯示其為普通用戶;
#!/bin/bash declare -i userid userid=$(id -u $1) if [ $userid -eq 0 ]; then echo "$1 is administer " elif [ $userid -gt 0 -a $userid -lt 500 ]; then echo "$1 is system user" else echo "$1 is a common user" fi
10、寫一個腳本,傳遞一個用戶名參數給腳本; (1) 如果用戶的id號大于等于500,且其默認shell為以sh結尾的字符串,則顯示“a user can log system.”類的字符串; (2) 否則,則顯示無法登錄系統;
#!/bin/bash if ! id $1 &> /dev/null;then echo " no such user,will exit " exit 2 else userid=$(id -u $1) if [ $userid -ge 500 ] && grep "^$1.*sh$" /etc/passwd &> /dev/null;then echo "$1 is a user can log system" else echo "can not log in system" fi fi
11、寫一個腳本,完成如下任務 : (1) 按順序分別復制/var/log目錄下的每個直接文件或子目錄至/tmp/test1-testn目錄中; (2) 復制目錄時,才使用cp -r命令; (3) 復制文件時使用cp命令; (4) 復制鏈接文件時使用cp -d命令; (5) 余下的所有類型,使用cp -a命令;”
#!/bin/bash declare -i di=1 for j in /var/log/* do [ -d /tmp/test$di ] || mkdir -p /tmp/test$di if [ -d $j ]; then cp -r $j /tmp/test$di elif [ -f $j ]; then cp $j /tmp/test$di elif [ -L $J ] ; then cp -d $j /tmp/test$di else cp -a $j /tmp/test$di fi let di++ done
原創文章,作者:diglinux,如若轉載,請注明出處:http://www.www58058.com/72172