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.178linux.com/64327
评论列表(1条)
脚本写的不错,但是不要使用系统的命令来作为变量,如果可以修改一下就完美了