1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現;
#!/bin/bash # sum=0 num=0 while read line;do usershell=$(echo $line | cut -d : -f 7) if [ $usershell == "/sbin/nologin" ];then let sum++ else let num++ fi done < /etc/passwd echo "Can login user:$num" echo "Not can login user:$sum"
2、寫一個腳本
(1) 獲取當前主機的主機名,保存于hostname變量中;
(2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com
(3) 否則,則顯示當前主機名;
#!/bin/bash # while read line;do hostname=$(echo $line | cut -d . -f 1) if [ $hostname == "localhost" ];then echo "HOSTNAME=www.magedu.com" > /etc/sysconfig/network else echo "$hostname" fi done < /proc/sys/kernel/hostname
3、寫一個腳本,完成如下功能
(1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在;
(2) 如果存在,則顯示此設備上的所有分區信息;
#!/bin/bash # read -p "Please enter a disk device path:" path while [ -z "$path" ];do echo "Please enter a correct disk device path." read -p "Please enter a disk device path:" path done if [ -b "$path" ];then fdisk -l $path else echo "This is not a disk device path." fi
4、寫一個腳本,完成如下功能
腳本能夠接受一個參數;
(1) 如果參數1為quit,則顯示退出腳本,并執行正常退出;
(2) 如果參數1為yes,則顯示繼續執行腳本;
(3) 否則,參數1為其它任意值,均執行非正常退出;
#!/bin/bash # cat << EOF yes) Continue to execute the shell quit) Normal exit other) Abnormal exit ================================== EOF read -p "Enter your option:" option while [ -z "$option" ];do echo "Please enter at least one option" read -p "Enter your option:" option done if [ "$option" == "yes" ];then echo "yes,quit or other" read -p "Enter your option again:" option elif [ "$option" == "quit" ];then echo "Normal exit" exit 0 else echo "Abnormal exit" exit 1 fi
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 # cat << EOF gzip) Archive compressed /etc directory using gzip tool bzip2) Archive compressed /etc directory using tool xz) Archive compressed /etc directory using xz tool quit) quit ================================================== EOF read -p "Enter your option:" option while [ -z "$option" ];do echo "gzip,bzip2,xz or quit" read -p "Enter your option again:" option done case $option in gzip) tar -zcf /backups/etc-$(date +\%Y\%m\%d).tar.gz /etc;; bzip2) tar -jcf /backups/etc-$(date +\%Y\%m\%d).tar.bz2 /etc;; xz) tar -Jcf /backups/etc-$(date +\%Y\%m\%d).tar.xz /etc;; quit) echo "quit" exit 0;; *) echo "Abnormal exit" exit 1 esac
6、寫一個腳本,接受一個路徑參數:
(1) 如果為普通文件,則說明其可被正常訪問;
(2) 如果是目錄文件,則說明可對其使用cd命令;
(3) 如果為符號鏈接文件,則說明是個訪問路徑;
(4) 其它為無法判斷;
#!/bin/bash # read -p "Please enter a path:" path while [ -z "$path" ];do read -p "Please enter a path:" path done if [ -f "$path" ];then echo "$path Can be accessed normally." elif [ -d "$path" ];then echo "$path Is a directory,You can use the CD command to switch to this directory." elif [ -h "$path" ];then echo "$path Is an access path." else echo "Can not judge." exit 0 fi
7、寫一個腳本,取得當前主機的主機名,判斷
(1) 如果主機名為空或為localhost,或為"(none)",則將其命名為mail.magedu.com;
(2) 否則,顯示現有的主機名即可;
#!/bin/bash # while read line;do hostname=$(echo $line | cut -d . -f1) if [ $hostname == "localhost" -o $hostname == "(none)" ];then echo "HOSTNAME=mail.magedu.com" > /etc/sysconfig/network else echo "The current host name is:$line" fi done < /proc/sys/kernel/hostname
8、寫一腳本,接受一個用戶名為參數;
(1) 如果用戶的id號為0,則顯示其為管理員;
(2) 如果用戶的id號大于0且小于500, 則顯示其為系統用戶;
#!/bin/bash # read -p "Please enter a user name:" user while [ -z $(grep -o "^$user\>" /etc/passwd) ];do echo -e "\033[32m Please enter a correct username. \033[0m" read -p "Please enter a user name again:" user done if [ $(grep "^$user\>" /etc/passwd | cut -d : -f 3) -eq 0 ];then echo -e "\033[31m This is an admin user \033[0m" elif [ $(grep "^$user\>" /etc/passwd | cut -d : -f 3) -gt 0 -a $(grep "^$user\>" /etc/passwd | cut -d : -f 3) -lt 500 ];then echo -e "\033[31m This is an System user \033[0m" else echo -e "\033[31m This is an Ordinary user \033[0m" fi
原創文章,作者:641348038@qq.com,如若轉載,請注明出處:http://www.www58058.com/64327
腳本寫的不錯,但是不要使用系統的命令來作為變量,如果可以修改一下就完美了