标签:bash脚本编程
1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;
[root@localhost ~]# vim usershell.sh #!/bin/bash # declare -i nologin_num=0 declare -i login_num=0 for i in $(cut -d: -f7 /etc/passwd);do if [ "$i" == "/sbin/nologin" ];then let nologin_num++ else let login_num++ fi done echo "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 :33 The total number of user shell that can login is :5 [root@localhost ~]# grep -o /sbin/nologin /etc/passwd | wc -l 33 [root@localhost ~]# grep -v /sbin/nologin /etc/passwd | wc -l 5
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 ~]# hostname localhost [root@localhost ~]# bash hostnametest.sh Hostname has changed to www.magedu.com [root@localhost ~]# hostname www.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:" devicepath if [ -z $devicepath ];then echo "Usage: Please input a device path" exit 1 fi if [ -b $devicepath ];then fdisk -l $devicepath else 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/sdb No such device [root@localhost ~]# bash devicetest.sh Please input a device path:/dev/sda Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk 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):" parameter while true;do case $parameter in quit) 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 ;; esac done
脚本测试
[root@localhost ~]# bash parametertest.sh Please input a word(quit|yes):quit exit the script [root@localhost ~]# bash parametertest.sh Please input a word(quit|yes):yes continue to excute the script Please input a word(quit|yes):quit exit the script [root@localhost ~]# bash parametertest.sh Please input a word(quit|yes): error exit [root@localhost ~]# bash parametertest.sh Please input a word(quit|yes):yes continue to excute the script Please 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 ];then mkdir /backups fi read -p "Please choose a format of compression(gzip|bzip2|xz):" zip case $zip 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 ;; *) echo "error compression format" exit 1 ;; esac
脚本测试
[root@localhost ~]# ls /backups ls: cannot access /backups: No such file or directory [root@localhost ~]# bash compresstest.sh Please choose a format of compression(gzip|bzip2|xz):gzip [root@localhost ~]# bash compresstest.sh Please choose a format of compression(gzip|bzip2|xz):bzip2 [root@localhost ~]# bash compresstest.sh Please choose a format of compression(gzip|bzip2|xz):xz [root@localhost ~]# bash compresstest.sh Please 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:" path if [ -f $path ];then echo "${path} can be visited" cat $path elif [ -d $path ];then echo "${path} can use ‘cd‘ command" elif [ -h $path ];then echo "${path} is a access path" ls -l $path else echo "unknown file" exit 1 fi
脚本测试
[root@localhost ~]# bash pathtest.sh Please 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 1 UUID=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.sh Please input a path:/dev /dev can use ‘cd‘ command [root@localhost ~]# bash pathtest.sh Please input a path:/dev/cdrom /dev/cdrom is a access path lrwxrwxrwx. 1 root root 3 Aug 3 2016 /dev/cdrom -> sr0 [root@localhost ~]# bash pathtest.sh Please input a path:/hello unknown 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" ];then hostname "mail.magedu.com" && hostname else echo "Current hostname is $hostname" fi
脚本测试
[root@localhost ~]# hostname mail.magedu.com [root@localhost ~]# bash hostnametest2.sh Current hostname is mail.magedu.com [root@localhost ~]# hostname localhost [root@localhost ~]# bash hostnametest2.sh mail.magedu.com [root@localhost ~]# hostname none [root@localhost ~]# bash hostnametest2.sh mail.magedu.com
8、写一脚本,接受一个用户名为参数;
(1) 如果用户的id号为0,则显示其为管理员;
(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
(3) 否则,则显示其为普通用户;
vim usernametest.sh #!/bin/bash # read -p "Please input a username:" username if [ -z $username ];then echo "Usage: Please input a username" exit 1 fi if ! id $username &>/dev/null;then echo "user doesn‘t exist" else userid=$(id -u $username) if [ $userid -eq 0 ];then echo "$username is a administrator" elif [ $userid -gt 0 -a $userid -lt 500 ];then echo "$username is a system user" else echo "$username is a normal user" fi fi
脚本测试
[root@localhost ~]# bash usernametest.sh Please input a username: Usage: Please input a username [root@localhost ~]# bash usernametest.sh Please input a username:jack user doesn‘t exist [root@localhost ~]# bash usernametest.sh Please input a username:root root is a administrator [root@localhost ~]# bash usernametest.sh Please input a username:postfix postfix is a system user [root@localhost ~]# useradd yuki [root@localhost ~]# bash usernametest.sh Please input a username:yuki yuki is a normal user
标签:bash脚本编程
原文地址:http://941641.blog.51cto.com/931641/1941601