1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現; #!/bin/bash declare -i a=0 declare -i b=0 n=`cat /etc/passwd |cut -d: -f 7` for i in $n;do if [ $i == /sbin/nologin ];then let a++ else let b++ fi done echo "the nologin users number is": $a echo "the login number users is": $b 2、寫一個腳本 (3) 否則,則顯示當前主機名; #!/bin/bash hostname=$(hostname) if [ $hostname == localhost ];then hostname www.magedu.com else echo $hostname fi 3、寫一個腳本,完成如下功能 (2) 如果存在,則顯示此設備上的所有分區信息; #!/bin/bash if [ $# -lt 1 ];then echo "please input in least one device" exit 2 fi if [ -e $1 ];then fdisk -l $1 else echo "no such device" fi 4、寫一個腳本,完成如下功能 (3) 否則,參數1為其它任意值,均執行非正常退出; #!/bin/bash read -p "pelase input a common quit/yes": a if [ $a == quit ];then echo "exit script" exit 0 elif [ $a == yes ];then echo "script continue" else echo "error agrument" exit 2 fi 5、寫一個腳本,完成如下功能 (4) 其它任意值,則顯示錯誤壓縮工具,并執行非正常退出; #!/bin/bash if [ $# -lt 1 ];then echo "please input in a common" fi if [ $1 == gzip ];then tar -zcf /backups/etc-20160613.tar.gz /etc elif [ $1 == bzip2 ];then tar -jcf /backups/etc-20160613.tar.bz2 /etc elif [ $1 == xz ];then tar -Jcf /backups/etc-20160613.tar.xz /etc else echo "argument error" exit 2 fi 6、寫一個腳本,接受一個路徑參數: (4) 其它為無法判斷; if [ $# -lt 1 ];then echo "please input a url" fi if [ -L $1 ];then echo "this is a access url" elif [ -d $1 ];then echo "can use cd common" elif [ -f $1 ];then echo "normal access" else echo "unknow" fi 7、寫一個腳本,取得當前主機的主機名,判斷 (2) 否則,顯示現有的主機名即可; #!/bin/bash hostname=`hostname` if [ $hostname == localhost -o $hostname == none ];then hostname mail.magedu.com else echo $hostname fi 8、寫一腳本,接受一個用戶名為參數; (3) 否則,則顯示其為普通用戶; #!/bin/bash a=`id -u $1` if ! grep "^$1\>" /etc/passwd &> /dev/null; then echo "no such user" elif [ $a -eq 0 ];then echo "this is root" elif [ $a -lt 500 ];then echo "this system user" else echo "this regular user" fi 10、寫一個腳本,傳遞一個用戶名參數給腳本; (2) 否則,則顯示無法登錄系統; #!/bin/bash if [ $# -lt 1 ];then echo "please input a agarument" exit 2 fi if ! grep "^$1\>" /etc/passwd &> /dev/null;then echo "no such user" exit 3 fi a=`id -u $1` b=`grep -o "^user1\>.*sh$" /etc/passwd |grep -o sh` if [ $a -ge 500 ] && [ $b == sh ];then echo "a user can log system" else echo "can not login" fi 11、寫一個腳本,完成如下任務 : (5) 余下的所有類型,使用cp -a命令; #!/bin/bash for i in /var/log/*;do if [ -d $i ];then cp -r $i /tmp/test1-testn elif [ -L $i ];then cp -d $i /tmp/test1-testn elif [ -f $i ];then cp $i /tmp/test1-testn else cp -a $i /tmp/test1-testn fi done |
原創文章,作者:a295053193,如若轉載,請注明出處:http://www.www58058.com/50716
作業寫的很好