1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;
[root@localhost ~]# vim usershell.sh#!/bin/bash#declare -i nologin_num=0declare -i login_num=0for i in $(cut -d: -f7 /etc/passwd);do if [ "$i" == "/sbin/nologin" ];then let nologin_num++ else let login_num++ fidoneecho "The total number of user shell that can't login is :$nologin_num"echo "The total number of user shell that can login is :$login_num"
脚本测试
[root@localhost ~]# bash usershell.sh The total number of user shell that can't login is :33The total number of user shell that can login is :5[root@localhost ~]# grep -o /sbin/nologin /etc/passwd | wc -l33[root@localhost ~]# grep -v /sbin/nologin /etc/passwd | wc -l5
2、写一个脚本
(1) 获取当前主机的主机名,保存于hostname变量中;
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
(3) 否则,则显示当前主机名;
vim hostnametest.sh#!/bin/bash#hostname=$(hostname)if [ "$hostname" == "localhost" ];then hostname www.magedu.com echo "Hostname has changed to www.magedu.com"else echo "Current hostname is $hostname"fi
脚本测试
[root@localhost ~]# hostnamelocalhost[root@localhost ~]# bash hostnametest.sh Hostname has changed to www.magedu.com[root@localhost ~]# hostnamewww.magedu.com[root@localhost ~]# bash hostnametest.sh Current hostname is www.magedu.com
3、写一个脚本,完成如下功能
(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
vim devicetest.sh#!/bin/bash#read -p "Please input a device path:" devicepathif [ -z $devicepath ];then echo "Usage: Please input a device path" exit 1fiif [ -b $devicepath ];then fdisk -l $devicepathelse echo "No such device"fi
脚本测试
[root@localhost ~]# bash devicetest.sh Please input a device path:Usage: Please input a device path[root@localhost ~]# bash devicetest.sh Please input a device path:/dev/sdbNo such device[root@localhost ~]# bash devicetest.sh Please input a device path:/dev/sdaDisk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000a0ae7 Device Boot Start End Blocks Id System/dev/sda1 * 2048 1026047 512000 83 Linux/dev/sda2 1026048 41943039 20458496 8e Linux LVM
4、写一个脚本,完成如下功能
脚本能够接受一个参数;
(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
(2) 如果参数1为yes,则显示继续执行脚本;
(3) 否则,参数1为其它任意值,均执行非正常退出;
vim parametertest.sh#!/bin/bash#read -p "Please input a word(quit|yes):" parameterwhile true;docase $parameter inquit)echo "exit the script"exit 0;;yes)echo "continue to excute the script"read -p "Please input a word(quit|yes):" parameter;;*)echo "error exit"exit 1;;esacdone
脚本测试
[root@localhost ~]# bash parametertest.shPlease input a word(quit|yes):quitexit the script[root@localhost ~]# bash parametertest.shPlease input a word(quit|yes):yescontinue to excute the scriptPlease input a word(quit|yes):quitexit the script[root@localhost ~]# bash parametertest.shPlease input a word(quit|yes):error exit[root@localhost ~]# bash parametertest.shPlease input a word(quit|yes):yescontinue to excute the scriptPlease input a word(quit|yes):error exit
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) 其它任意值,则显示错误压缩工具,并执行非正常退出;
vim compresstest.sh#!/bin/bash#if [ ! -e /backups ];thenmkdir /backupsfiread -p "Please choose a format of compression(gzip|bzip2|xz):" zipcase $zip ingzip)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;;*)echo "error compression format"exit 1;;esac
脚本测试
[root@localhost ~]# ls /backupsls: cannot access /backups: No such file or directory[root@localhost ~]# bash compresstest.shPlease choose a format of compression(gzip|bzip2|xz):gzip[root@localhost ~]# bash compresstest.shPlease choose a format of compression(gzip|bzip2|xz):bzip2[root@localhost ~]# bash compresstest.shPlease choose a format of compression(gzip|bzip2|xz):xz[root@localhost ~]# bash compresstest.shPlease choose a format of compression(gzip|bzip2|xz):error compression format[root@localhost ~]# ls /backups/etc-20170624.tar.bz2 etc-20170624.tar.gz etc-20170624.tar.xz
6、写一个脚本,接受一个路径参数:
(1) 如果为普通文件,则说明其可被正常访问;
(2) 如果是目录文件,则说明可对其使用cd命令;
(3) 如果为符号链接文件,则说明是个访问路径;
(4) 其它为无法判断;
vim pathtest.sh#!/bin/bash#read -p "Please input a path:" pathif [ -f $path ];then echo "${path} can be visited" cat $pathelif [ -d $path ];then echo "${path} can use 'cd' command"elif [ -h $path ];then echo "${path} is a access path" ls -l $pathelse echo "unknown file" exit 1fi
脚本测试
[root@localhost ~]# bash pathtest.shPlease input a path:/etc/fstab/etc/fstab can be visited## /etc/fstab# Created by anaconda on Tue Aug 2 21:31:19 2016## Accessible filesystems, by reference, are maintained under '/dev/disk'# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#/dev/mapper/rhel-root / xfs defaults 1 1UUID=ce40e87b-854d-42dc-ac50-e1309101c43d /boot xfs defaults 1 2/dev/mapper/rhel-swap swap swap defaults 0 0/dev/cdrom/media/cdromiso9660defaults0 0[root@localhost ~]# bash pathtest.shPlease input a path:/dev /dev can use 'cd' command[root@localhost ~]# bash pathtest.shPlease input a path:/dev/cdrom/dev/cdrom is a access pathlrwxrwxrwx. 1 root root 3 Aug 3 2016 /dev/cdrom -> sr0[root@localhost ~]# bash pathtest.shPlease input a path:/hellounknown file
7、写一个脚本,取得当前主机的主机名,判断
(1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;
(2) 否则,显示现有的主机名即可;
vim hostnametest2.sh#!/bin/bash#hostname=$(hostname)if [ -z $hostname -o $hostname == "localhost" -o $hostname == "none" ];thenhostname "mail.magedu.com" && hostnameelseecho "Current hostname is $hostname"fi
脚本测试
[root@localhost ~]# hostnamemail.magedu.com[root@localhost ~]# bash hostnametest2.shCurrent hostname is mail.magedu.com[root@localhost ~]# hostname localhost[root@localhost ~]# bash hostnametest2.shmail.magedu.com[root@localhost ~]# hostname none[root@localhost ~]# bash hostnametest2.shmail.magedu.com
8、写一脚本,接受一个用户名为参数;
(1) 如果用户的id号为0,则显示其为管理员;
(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
(3) 否则,则显示其为普通用户;
vim usernametest.sh#!/bin/bash#read -p "Please input a username:" usernameif [ -z $username ];then echo "Usage: Please input a username" exit 1fiif ! id $username &>/dev/null;thenecho "user doesn't exist"elseuserid=$(id -u $username)if [ $userid -eq 0 ];thenecho "$username is a administrator"elif [ $userid -gt 0 -a $userid -lt 500 ];thenecho "$username is a system user"elseecho "$username is a normal user"fifi
脚本测试
[root@localhost ~]# bash usernametest.shPlease input a username:Usage: Please input a username[root@localhost ~]# bash usernametest.shPlease input a username:jackuser doesn't exist[root@localhost ~]# bash usernametest.shPlease input a username:rootroot is a administrator[root@localhost ~]# bash usernametest.shPlease input a username:postfixpostfix is a system user[root@localhost ~]# useradd yuki[root@localhost ~]# bash usernametest.shPlease input a username:yukiyuki is a normal user