第九周作業
1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現;
#!/bin/bash # declare -i nologin=0 declare -i other=0 for i in $(cut -d: -f7 /etc/passwd);do if [ $i == /sbin/nologin ]; then let nologin++ else let other++ fi done echo "nologin user numer:$nologin" echo "other user number:$other" ~ 測試: [root@qq scripts]# bash -x 9-1.sh + declare -i nologin=0 + declare -i other=0 ++ cut -d: -f7 /etc/passwd + for i in '$(cut -d: -f7 /etc/passwd)' + '[' /bin/bash == /sbin/nologin ']' + let other++ + for i in '$(cut -d: -f7 /etc/passwd)' + '[' /sbin/nologin == /sbin/nologin ']' + let nologin++ + for i in '$(cut -d: -f7 /etc/passwd)' + '[' /sbin/nologin == /sbin/nologin ']' + let nologin++ …… + echo 'nologin user numer:26' nologin user numer:26 + echo 'other user number:10' other user number:10
2、寫一個腳本
-
(1) 獲取當前主機的主機名,保存于hostname變量中;
-
(2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com;
-
(3) 否則,則顯示當前主機名;
#!/bin/bash hostname=$(hostname) if [ $hostname == 'localhost' ];then hostname www.magedu.com && echo "hostname change:'www.magedu.com'" else echo 'hostname is:' $hostname fi 測試: [root@qq scripts]# bash -x 9-2.sh ++ hostname + hostname=qq.localdomain + '[' qq.localdomain == localhost ']' + echo 'hostname is:' qq.localdomain hostname is: qq.localdomain [root@qq scripts]#
3、寫一個腳本,完成如下功能
-
(1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在;
-
(2) 如果存在,則顯示此設備上的所有分區信息;
#!/bin/bash # read -p "enter dev path:" dev if [ -e $dev ];then parted $dev print else echo "This dev is not exist" fi 測試: [root@qq scripts]# bash -x 9-3.sh + read -p 'enter dev path:' dev enter dev path:/dev/sda + '[' -e /dev/sda ']' + parted /dev/sda print Model: VMware, VMware Virtual S (scsi) Disk /dev/sda: 268GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 211MB 210MB primary ext4 boot 2 211MB 52.6GB 52.4GB primary ext4 3 52.6GB 94.6GB 41.9GB primary ext4 4 94.6GB 268GB 174GB extended 5 94.6GB 131GB 36.7GB logical ext4 6 131GB 152GB 21.0GB logical ext4 7 152GB 168GB 15.7GB logical ext4 8 168GB 184GB 15.7GB logical ext4 9 184GB 194GB 10.5GB logical ext4 10 194GB 198GB 4295MB logical linux-swap(v1) [root@qq scripts]#
4、寫一個腳本,完成如下功能
-
腳本能夠接受一個參數;
-
(1) 如果參數1為quit,則顯示退出腳本,并執行正常退出;
-
(2) 如果參數1為yes,則顯示繼續執行腳本;
-
(3) 否則,參數1為其它任意值,均執行非正常退出;
#!/bin/bash # if [ $# -le 0 ];then echo "usage:bash 9-4.sh quit|yes|other" exit 2 else case $1 in quit) echo "quiting" exit 0;; yes) echo "conutine";; *) echo "error..." exit 1 esac fi 測試: [root@qq scripts]# bash 9-4.sh usage:bash 9-4.sh quit|yes|other [root@qq scripts]# [root@qq scripts]# [root@qq scripts]# bash 9-4.sh quit quiting [root@qq scripts]# bash 9-4.sh yes conutine [root@qq scripts]# bash 9-4.sh d error...
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 # if [ $# -le 0 ]; then echo "usage:bash 9-5.sh gzip|bzip2|xz|..." exit 2 else case $1 in gzip) tar czvf /backups/etc-20160613.tar.gz /etc;; bzip2) tar cjvf /backups/etc-20160613.tar.bz2 /etc;; xz) tar cJvf /backups/etc-20160613.tar.xz /etc;; *) echo "error..." exit 1;; esac fi 測試: [root@qq scripts]# bash 9-5.sh xz [root@qq scripts]# ls /backups/ etc-20160613.tar.xz [root@qq scripts]#
6、寫一個腳本,接受一個路徑參數:
-
(1) 如果為普通文件,則說明其可被正常訪問;
-
(2) 如果是目錄文件,則說明可對其使用cd命令;
-
(3) 如果為符號鏈接文件,則說明是個訪問路徑;
-
(4) 其它為無法判斷;
#!/bin/bash # if [ $# -le 0 ];then echo "Usage:bash 9-6.sh /PATH/FILE." exit 2 fi if [ -h $1 ]; then echo "This is accessable path." elif [ -d $1 ]; then echo "This is directory command 'cd' can be use." elif [ -f $1 ]; then echo "This file can be access." else echo "Unknow File." fi 測試: [root@qq scripts]# bash 9-6.sh /tmp/2.txt This is accessable path. [root@qq scripts]# bash 9-6.sh /tmp/1.txt This file can be access. [root@qq scripts]# bash 9-6.sh /tmp/ This is directory command 'cd' can be use.
7、寫一個腳本,取得當前主機的主機名,判斷
-
(1) 如果主機名為空或為localhost,或為"(none)",則將其命名為mail.magedu.com;
-
(2) 否則,顯示現有的主機名即可;
#!/bin/bash # HostName=`hostname` if [ -z "HostNmae" -o "$HostName" == "localhost" -o "$HostName" == "(none)" ]; then hostname mail.magedu.com else echo "$HostName" fi 測試: [root@qq scripts]# bash 9-7.sh qq.localdomain
8、寫一腳本,接受一個用戶名為參數;
-
(1) 如果用戶的id號為0,則顯示其為管理員;
-
(2) 如果用戶的id號大于0且小于500, 則顯示其為系統用戶;
-
(3) 否則,則顯示其為普通用戶;
#!/bin/bash # read -p "Please enter one user:" user uid=`id -u $user` if [ $uid -eq 0 ]; then echo "$user is admin." elif [ $uid -gt 0 -a $uid -lt 500 ]; then echo "$user is system user." else echo "$user is common user." fi 測試: [root@qq scripts]# bash 9-8.sh Please enter one user:root root is admin. [root@qq scripts]# bash 9-8.sh Please enter one user:mysql mysql is system user. [root@qq scripts]# bash 9-8.sh Please enter one user:centos centos is common user.
10、寫一個腳本,傳遞一個用戶名參數給腳本;
-
(1) 如果用戶的id號大于等于500,且其默認shell為以sh結尾的字符串,則顯示“a user can log system.”類的字符串;
-
(2) 否則,則顯示無法登錄系統;
#!/bin/bash # if [ $# -le 0 ]; then echo "Usage:bash 9-10.sh username" exit 2 fi if id $1 &> /dev/null; then userid=`grep ^$1 /etc/passwd | grep sh$ | cut -d: -f3` if [ $userid -ge 500 ]; then echo "a user can login system." else echo "a user can not login system." fi fi 測試: [root@qq scripts]# bash 9-10.sh root a user can not login system. [root@qq scripts]# bash 9-10.sh gg a user can login system. [root@qq scripts]# cat /etc/passwd | grep gg gg:x:603:603::/home/gg:/bin/sh
11、寫一個腳本,完成如下任務 :
-
(1) 按順序分別復制/var/log目錄下的每個直接文件或子目錄至/tmp/test1-testn目錄中;
-
(2) 復制目錄時,才使用cp -r命令;
-
(3) 復制文件時使用cp命令;
-
(4) 復制鏈接文件時使用cp -d命令;
-
(5) 余下的所有類型,使用cp -a命令;
#!/bin/bash dir="/var/log/" des="/tmp/test1-testn" mkdir $des for i in `ls $dir`; do if [ -c $i ]; then cp -r $dir$i $des/ elif [ -f $i ]; then cp $dir$i $des/ elif [ -h $i ]; then cp -d $dir$i $des/ else cp -a $dir$i $des/ fi done
原創文章,作者:Net21_仲樂,如若轉載,請注明出處:http://www.www58058.com/45878
寫腳本的時候,不要用系統的函數名作為變量名