馬哥教育網絡21期+第九周練習博客
1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現; [root@localhost bin]# cat 1.sh #!/bin/bash # while read line;do if [ `echo $line | cut -d ':' -f7` != '/sbin/nologin' ]; then let i++ elif [ `echo $line | cut -d ':' -f7` = '/sbin/nologin' ];then let j++ fi done < /etc/passwd echo "The default shell is '/sbin/nologin' $i." echo "The default shell is not '/sbin/nologin' $j." 2、寫一個腳本 (1) 獲取當前主機的主機名,保存于hostname變量中; (2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com; (3) 否則,則顯示當前主機名; [root@localhost bin]# cat 2.sh #!/bin/bash # hostname=`hostname` if [ $hostname = 'localhost.localdomain' ];then `echo "www.magedu.com" > /proc/sys/kernel/hostname` echo "The host name: `hostname`." fi 3、寫一個腳本,完成如下功能 (1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在; (2) 如果存在,則顯示此設備上的所有分區信息; root@localhost bin]# cat 3.sh #!/bin/bash # read -p "Please enter the disk device file path:" option # while true; do fdisk -l /dev/$option &> /dev/null if [ $? -eq 0 ]; then echo "Disk path right." echo "`fdisk -l /dev/$option`" break fi if [ $? -ne 0 ];then read -p "Please input the correct disk path:" option fi done 4、寫一個腳本,完成如下功能 腳本能夠接受一個參數; (1) 如果參數1為quit,則顯示退出腳本,并執行正常退出; (2) 如果參數1為yes,則顯示繼續執行腳本; (3) 否則,參數1為其它任意值,均執行非正常退出; [root@localhost bin]# cat 4.sh #!/bin/bash # read -p "Please input parameters:" option while true ;do if [ "$option" == 'yes' ];then read -p "Please continue to input parameters:" option elif [ "$option" == 'quit' ];then echo "The normal exit." break return 0 elif [ "$option" != 'quit' -a "$option" != 'yes' ];then echo "Non-normal exit." break fi done 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) 其它任意值,則顯示錯誤壓縮工具,并執行非正常退出; [root@localhost bin]# cat 10.sh #!/bin/bash # i=`date +%Y%m%d` if [ $# -lt 1 ]; then echo "Enter one option." else case $1 in gzip) tar -czf/etc-$i.tar.gz hfz -C /backups ;; bzip2) tar -cjf/etc-$i.tar.bz2 hfz -C /backups ;; xz) tar -cJf/etc -$i.tar.xzhfz -C /backups ;; *) echo "exit." ;; esac fi 6、寫一個腳本,接受一個路徑參數: (1) 如果為普通文件,則說明其可被正常訪問; (2) 如果是目錄文件,則說明可對其使用cd命令; (3) 如果為符號鏈接文件,則說明是個訪問路徑; (4) 其它為無法判斷; [root@localhost bin]# cat 8.sh #!/bin/bash # read -p "Please enter the file path:" option for file in /$option/* ; do if [ -f $file ];then echo "($file) This file can be normal access." elif [ -L $file ]; then echo "($file) is This file is the access path." elif [ -d $file ]; then echo "($file) This file can use the CD command." else echo "The other could not judge." fi done 7、寫一個腳本,取得當前主機的主機名,判斷 (1) 如果主機名為空或為localhost,或為"(none)",則將其命名為mail.magedu.com; (2) 否則,顯示現有的主機名即可; [root@localhost bin]# cat 5.sh #!/bin/bash # i=`hostname` if [ "$i" == 'localhost' ]; then `echo "mail.magedu.com" > /proc/sys/kernel/hostname` echo "The host name: `hostname`." elif [ "$i" == 'none' ];then `echo "mail.magedu.com" > /proc/sys/kernel/hostname` echo "The host name: `hostname`." else echo "hostname is not equal to 'localhost' or 'none' so the hostname is $i." fi 8、寫一腳本,接受一個用戶名為參數; (1) 如果用戶的id號為0,則顯示其為管理員; (2) 如果用戶的id號大于0且小于500, 則顯示其為系統用戶; (3) 否則,則顯示其為普通用戶; [root@localhost bin]# cat 6.sh #!/bin/bash # i=`cat /etc/passwd | egrep "^$1" | cut -d':' -f3` if [ $i -eq 0 ];then echo "This user to the administrator." elif [ $i -gt 0 -a $i -le 500 ];then echo "This user for users of the system." elif [ $i -gt 500 ];then echo "This user for ordinary users." fi 10、寫一個腳本,傳遞一個用戶名參數給腳本; (1) 如果用戶的id號大于等于500,且其默認shell為以sh結尾的字符串,則顯示“a user can log system.”類的字符串; (2) 否則,則顯示無法登錄系統; [root@localhost bin]# cat 7.sh #!/bin/bash # i=`cat /etc/passwd | egrep "^$1" | cut -d':' -f3` j=`` # if [ $i -lt 500 ];then echo "user UID less 500" echo "Unable to login system." fi if [ $i -ge 500 ];then cat /etc/passwd | egrep "^$1.*sh$" &> /dev/null if [ $? -eq 0 ];then echo "a user can log system." fi fi 11、寫一個腳本,完成如下任務 : (1) 按順序分別復制/var/log目錄下的每個直接文件或子目錄至/tmp/test1-testn目錄中; (2) 復制目錄時,才使用cp -r命令; (3) 復制文件時使用cp命令; (4) 復制鏈接文件時使用cp -d命令; (5) 余下的所有類型,使用cp -a命令; [root@localhost bin]# cat 9.sh #!/bin/bash # for file in /var/log/* ; do if [ -f $file ];then cp $file /tmp/test1-testn &> /dev/null elif [ -L $file ]; then cp -d $file /tmp/test1-testn &> /dev/null elif [ -d $file ]; then cp -r $file /tmp/test1-testn &> /dev/null else cp -a $file /tmp/test1-testn &> /dev/null fi done
原創文章,作者:wostop,如若轉載,請注明出處:http://www.www58058.com/43231
非常的棒,腳本寫得非常的漂亮,思路清晰,5題的排版需要注意一下。加油!