1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 login_user=0 5 nologin_user=0 6 for i in $(cat /etc/passwd | cut -d: -f7) 7 do 8 if [ $i == "/bin/bash" ];then 9 let login_user++ 10 else 11 let nologin_user++ 12 fi 13 done 14 echo "login user number:$login_user" 15 echo "no login user number:$nologin_user"
2、写一个脚本
(1) 获取当前主机的主机名,保存于hostname变量中;
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
(3) 否则,则显示当前主机名;
1 #!/bin/bash 2 #autho:BaoZhang 3 # 4 HOSTNAME=$(/bin/hostname) 5 if [ $UID -eq 0 ];then 6 if [ $HOSTNAME == "localhost" ];then 7 hostname "www.magedu.com" 8 echo 'hostname changed' 9 else 10 hostname 11 fi 12 else 13 echo "you must be root to change the host name" 14 exit 1 15 fi
3、写一个脚本,完成如下功能
(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 if [ $UID -eq 0 ];then 5 if [ $# -ne 1 ];then 6 echo "invalid argument " 7 exit 2 8 else 9 #ls $1 &>/dev/null 10 if [ -d $1 ];then 11 echo "$1 exist,something about $1:" 12 df -h $1 13 else 14 echo " device $1 not exist" 15 exit 3 16 fi 17 fi 18 else 19 echo "you must be root to open the device " 20 exit 1
4、写一个脚本,完成如下功能
脚本能够接受一个参数;
(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
(2) 如果参数1为yes,则显示继续执行脚本;
(3) 否则,参数1为其它任意值,均执行非正常退出;
1 #!/bin/bash 2 if [ $# -ne 1 ];then 3 echo "invalid argument,exit" 4 exit 2 5 else 6 case $1 in 7 "quit") 8 echo "quiting...." 9 exit 0 10 ;; 11 12 "yes") 13 echo "continuing...." 14 exit 0 15 ;; 16 *) 17 echo "interrupt...." 18 exit 3 19 esac 20 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) 其它任意值,则显示错误压缩工具,并执行非正常退出;
1 #/bin/bash 2 #author:BaoZhang 3 # 4 if [ $# -ne 1 ];then 5 echo "invalid argument, exit ..." 6 exit 1 7 else 8 case $1 in 9 "gzip") 10 tar -zcvf /backups/etc.`date +"%Y%m%d"`.tar.gz /etc &>/dev/null 11 echo "use gzip compreed" 12 exit 0 13 ;; 14 "zip2") 15 tar -jcvf /backup/etc.`date +"%Y%m%d"`.tar.bz2 /etc &>/dev/null 16 echo "use zip2 compreed" 17 exit 0 18 ;; 19 "xz") 20 tar -Jcvf /tmp/etc.`date +"%Y%m%d"`.tar.xz /etc &>/dev/null 21 echo "use xz compreed" 22 exit 0 23 ;; 24 *) 25 echo "invalid compress tools" 26 exit 3 27 esac 28 fi
6、写一个脚本,接受一个路径参数:
(1) 如果为普通文件,则说明其可被正常访问;
(2) 如果是目录文件,则说明可对其使用cd命令;
(3) 如果为符号链接文件,则说明是个访问路径;
(4) 其它为无法判断;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 if [ $# -ne 1 ];then 5 echo "invalid argument, one argument" 6 exit 1 7 else 8 if [ -f $1 ];then 9 echo "general file,you can use ls ...." 10 elif [ -d $1 ];then 11 echo "folder,you can use cd ....." 12 elif [ -L $1 ];then 13 echo "symbolic link file.........." 14 else 15 echo "sorry, i can not judge..." 16 fi 17 fi
7、写一个脚本,取得当前主机的主机名,判断
(1) 如果主机名为localhost,或为"(none)",则将其命名为mail.magedu.com;
(2) 否则,显示现有的主机名即可;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 HOSTNAME=$(hostname) 5 if [ $UID -eq 0 ];then 6 if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then 7 HOSTNAME=mail.magedu.com 8 else 9 echo "hostname:$HOSTNAME" 10 fi 11 else 12 echo "your permission only read....." 13 echo "hostname: $HOSTNAME" 14 fi 15
8、写一脚本,接受一个用户名为参数;
(1) 如果用户的id号为0,则显示其为管理员;
(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
(3) 否则,则显示其为普通用户;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 if [ $# -ne 1 ];then 5 echo "invalid arument, usage:$0 argument " 6 exit 1 7 else 8 id $1 &>/dev/null 9 if [ $? -eq 0 ];then 10 number=$(id -u $1) 11 if [ $number -eq 0 ];then 12 echo "sys admin...." 13 elif [ $number -gt 0 -a $number -lt 500 ];then 14 echo "sys user....." 15 else 16 echo "comm user...." 17 fi 18 else 19 echo "user $1 not exist..." 20 fi
9、写一个脚本,传递一个用户名参数给脚本;
(1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;
(2) 否则,则显示无法登录系统;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 if [ $# -ne 1 ];then 5 echo "argument invalid,usage:$0 arguement" 6 exit 1 7 else 8 id $1 &>/dev/null 9 if [ $? -eq 0 ];then 10 cat /etc/passwd | grep $1 | cut -d: -f7 | grep "sh$" &>/dev/null 11 if [ $? -eq 0 ];then 12 echo "$1:a user can login system..." 13 else 14 echo "$1 : can not login system..." 15 fi 16 else 17 echo "$1 not exist..." 18 fi 19 fi
原创文章,作者:zhangbao,如若转载,请注明出处:http://www.178linux.com/60114
评论列表(1条)
看得出扎实的脚本基本功。再接再励~