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.178linux.com/50716
评论列表(1条)
作业写的很好