1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現; [root@test ~]# vim userlgin.sh #!/bin/bash declare -i i=0 declare -i j=0 while read line;do usershell=$(echo $line |cut -d: -f7) if [ "$usershell" == "/sbin/nologin" ];then let i++ else let j++ fi done </etc/passwd echo "the nologin user total:$i" echo "the not nologin user total:$j" root@test ~]# bash userlgin.sh 2、寫一個腳本 (1) 獲取當前主機的主機名,保存于hostname變量中; (2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com; (3) 否則,則顯示當前主機名; [root@test tmp]# vim hostname2.sh #!/bin/bash hostname=`hostname` if [ "$hostname" == "localhost" ]; then hostname www.magedu.com hostname=`hostname` echo $hostname else echo $hostname fi [root@test tmp]# bash hostname2.sh 3、寫一個腳本,完成如下功能 (1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在; (2) 如果存在,則顯示此設備上的所有分區信息; [root@test tmp]# vim disk.sh #!/bin/bash fdisk -s $1 &> /dev/null if [ $? -eq 0 ];then fdisk -l $1 else echo "$1 is not exist" fi [root@test tmp]# bash disk.sh /dev/sdb1 4、寫一個腳本,完成如下功能 腳本能夠接受一個參數; (1) 如果參數1為quit,則顯示退出腳本,并執行正常退出; (2) 如果參數1為yes,則顯示繼續執行腳本; (3) 否則,參數1為其它任意值,均執行非正常退出; [root@www tmp]# vim input2.sh #!/bin/bash read -p "Please input yes or quit or other chars:" input case $input in quit) echo "safety exit" exit 0 ;; yes) echo "continue" ;; *) echo "unsafety exit" ;; esac [root@www tmp]# bash input2.sh 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@www tmp]# vim input2.sh #!/bin/bash arg=$1 case $arg in gzip) tar -zcf /backups/etc-20160613.tar.gz /etc ;; bzip2) tar -jcf /backups/etc-20160613.tar.bz2 /etc ;; xz) tar -Jcf /backups/etc-20160613.tar.xz /etc ;; *) echo "compress tool error,please input gzip,bzip2 or xz" exit 1 ;; esac [root@www tmp]# vim input2.sh 6、寫一個腳本,接受一個路徑參數: (1) 如果為普通文件,則說明其可被正常訪問; (2) 如果是目錄文件,則說明可對其使用cd命令; (3) 如果為符號鏈接文件,則說明是個訪問路徑; (4) 其它為無法判斷; [root@www tmp]# vim filetype2.sh #!/bin/bash if [ $# -lt 1 ]; then echo "At least one word." exit 1 fi if [ -f $1 ];then echo "You can access it." elif [ -L $1 ];then echo "it's a file path" elif [ -d $1 ];then echo "You can use command cd into the file." else echo "it's an unknow file" fi [root@www tmp]# bash filetype2.sh /etc 7、寫一個腳本,取得當前主機的主機名,判斷 (1) 如果主機名為空或為localhost,或為"(none)",則將其命名為mail.magedu.com; (2) 否則,顯示現有的主機名即可; [root@www tmp]# vim hostname3.sh #!/bin/bash hostname=`hostname` if [ -z "$hostname" -o "$hostname" == "localhost" -o "$hostname" == "(none)" ]; then hostname mail.magedu.com hostname=`hostname` echo $hostname else echo $hostname fi [root@www tmp]# bash -x hostname3.sh 8、寫一腳本,接受一個用戶名為參數; (1) 如果用戶的id號為0,則顯示其為管理員; (2) 如果用戶的id號大于0且小于500, 則顯示其為系統用戶; (3) 否則,則顯示其為普通用戶; [root@www tmp]# vim userid.sh #!/bin/bash if [ $# -lt 1 ];then echo "Please input an username" exit 1 fi id $1 &> /dev/null if [ $? -eq 0 ] ;then uid=`id -u $1` if [ $uid -eq 0 ];then echo "This is an administrator user" elif [ $uid -gt 0 -a $uid -lt 500 ];then echo "This is a system user" else echo "This is a common user" fi else echo "$1 is not exist" fi [root@www tmp]# bash userid.sh 10、寫一個腳本,傳遞一個用戶名參數給腳本; (1) 如果用戶的id號大于等于500,且其默認shell為以sh結尾的字符串,則顯示“a user can log system.”類的字符串; (2) 否則,則顯示無法登錄系統; [root@mail tmp]# vim userlg.sh #!/bin/bash if [ $# -lt 1 ];then echo "Please input an username" exit 1 fi id $1 &> /dev/null if [ $? -eq 0 ];then uid=`grep "^$1\>" /etc/passwd |cut -d: -f3` shell=`grep "^$1\>" /etc/passwd |cut -d: -f7|grep -o "sh$"` if [ $uid -eq 0 ];then echo "the user is an administrator user" elif [ $uid -ge 500 -a "$shell" == "sh" ];then echo "a user can log system" else echo "the user can't log system" fi else echo "$1 is not exist" fi [root@mail tmp]# bash -x userlg.sh user1 11、寫一個腳本,完成如下任務 : (1) 按順序分別復制/var/log目錄下的每個直接文件或子目錄至/tmp/test1-testn目錄中; (2) 復制目錄時,才使用cp -r命令; (3) 復制文件時使用cp命令; (4) 復制鏈接文件時使用cp -d命令; (5) 余下的所有類型,使用cp -a命令; [root@mail tmp]# vim cpfile.sh #!/bin/bash file=/var/log/* for i in $file;do if [ -f $i ];then echo "$i is common file" cp $i /tmp/test1-testn/ elif [ -L $i ];then echo "$i is link file" cp -d $i /tmp/test1-testn/ elif [ -d $i ];then echo "$i is directory" cp -r $i /tmp/test1-testn else echo "$i is other file" cp -a $i /tmp/test1-testn fi done [root@mail tmp]# bash cpfile.sh
原創文章,作者:N22_上海_長清,如若轉載,請注明出處:http://www.www58058.com/53353
完成的非常好,給出了詳細操作步驟,腳本思路清晰,能把排版在完善一下就更完美了,加油!