1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;
#!/bin/bash declare -i sum_login=0 declare -i sum_nologin=0 while read line;do shell=$(echo $line|awk -F: '{print $NF}') if [ "$shell" == "/sbin/nologin" ];then let sum_nologin++ else let sum_login++ fi done </etc/passwd echo "Can login: $sum_login" echo "Cannot login: $sum_nologin"
执行结果: [root@centos6 script]# ./chkuser.sh Can login: 8 Cannot login: 17
2、写一个脚本
(1) 获取当前主机的主机名,保存于hostname变量中;
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;
(3) 否则,则显示当前主机名;
#!/bin/bash hostname=$(hostname) if [ "$hostname" == "localhost" ];then hostname www.magedu.com else hostname fi
[root@centos6 script]# ./chhn.sh www.magedu.com
3、写一个脚本,完成如下功能
(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;
#!/bin/bash if [ $# -lt 1 ];then echo "Usage $0 arg1" exit 1 fi if [ -b $1 ];then fdisk -l $1 else echo "Invalid file!" fi
执行结果: [root@centos6 script]# ./devinfo.sh /dev/tty0 Invalid file! [root@centos6 script]# ./devinfo.sh /dev/sda1 Disk /dev/sda1: 524 MB, 524288000 bytes 255 heads, 63 sectors/track, 63 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
4、写一个脚本,完成如下功能
脚本能够接受一个参数;
(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;
(2) 如果参数1为yes,则显示继续执行脚本;
(3) 否则,参数1为其它任意值,均执行非正常退出;
#!/bin/bash if [ $# -lt 1 ];then echo "Usage $0 arg1" exit 1 fi while true;do case $1 in quit) echo "Exit in 2 second..." sleep 2 exit 0 ;; yes) echo "Continue..." break ;; *) echo "Unknown error,exit!" exit 1 esac done echo "Execute complete!"
执行结果: [root@centos6 script]# ./argu.sh Usage ./argu.sh arg1 [root@centos6 script]# ./argu.sh quit Exit in 2 second... [root@centos6 script]# ./argu.sh yes Continue... Execute complete! [root@centos6 script]# ./argu.sh haha Unknown 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) 其它任意值,则显示错误压缩工具,并执行非正常退出;
#!/bin/bash if [ $# -ne 1 ];then echo "Usage $0 arg1." exit 1 fi [ -d /backups ] || mkdir -p /backups case $1 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 "Invalid compress mode!" exit 1 esac
执行结果: [root@centos6 script]# ll /backups total 22904 -rw-r--r-- 1 root root 8395375 Feb 13 19:34 etc-20170213.tar.bz2 -rw-r--r-- 1 root root 9527374 Feb 13 19:32 etc-20170213.tar.gz -rw-r--r-- 1 root root 5523792 Feb 13 19:34 etc-20170213.tar.xz
6、写一个脚本,接受一个路径参数:
(1) 如果为普通文件,则说明其可被正常访问;
(2) 如果是目录文件,则说明可对其使用cd命令;
(3) 如果为符号链接文件,则说明是个访问路径;
(4) 其它为无法判断;
#!/bin/bash if [ $# -lt 1 ];then echo "Usage $0 arg1." exit 1 fi if [ -L $1 ];then echo "$1 is symbolic file." elif [ -f $1 ];then echo "$1 is regular file,you can read and write it." elif [ -d $1 ];then echo "$1 is directory,you can use the command cd to enter it." else echo "Unknown file type!" fi
执行结果: [root@www script]# ./chktype.sh /etc/hosts /etc/hosts is regular file,you can read and write it. [root@www script]# ./chktype.sh /etc/redhat-release /etc/redhat-release is symbolic file. [root@www script]# ./chktype.sh /etc/ /etc/ is directory,you can use the command cd to enter it. [root@www script]# ./chktype.sh ddd Unknown file type!
7、写一个脚本,取得当前主机的主机名,判断
(1) 如果主机名为空或为localhost,或为”(none)”,则将其命名为mail.magedu.com;
(2) 否则,显示现有的主机名即可;
#!/bin/bash if [ -z "$(hostname)" ] || [ "$(hostname)" == "localhost" ];then hostname mail.magedu.com echo "set 'mail.magedu.com' as new hostname." else hostname fi
执行结果: [root@www script]# ./sethn.sh set 'mail.magedu.com' as new hostname. [root@www script]# hostname mail.magedu.com
8、写一脚本,接受一个用户名为参数;
(1) 如果用户的id号为0,则显示其为管理员;
(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
(3) 否则,则显示其为普通用户;
#!/bin/bash if [ $# -lt 1 ];then echo "Usage $0 arg1" exit 1 fi if ! id $1 &>/dev/null;then echo "$1 not exists!" exit 1 fi uid=$(id -u $1) if [ $uid -ge 500 ];then echo "$1 is regular user." elif [ $uid -gt 0 ];then echo "$1 is system user." else echo "$1 is root." fi
执行结果: [root@www script]# ./chkuser2.sh magedu magedu is regular user. [root@www script]# ./chkuser2.sh mysql mysql is system user. [root@www script]# ./chkuser2.sh root root is root. [root@www script]# ./chkuser2.sh hahaha hahaha not exists!
10、写一个脚本,传递一个用户名参数给脚本;
(1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;
(2) 否则,则显示无法登录系统;
#!/bin/bash if [ $# -lt 1 ];then echo "Usage $0 arg1." exit 1 fi if ! id $1 &>/dev/null;then echo "$1 not exists!" exit 1 fi uid=$(id -u $1) if [ $uid -ge 500 ] && grep "^$1.*sh$" /etc/passwd &>/dev/null;then echo "$1 can login." else echo "$1 cannot login." fi
执行结果: [root@www script]# ./chkuser3.sh magedu magedu can login. [root@www script]# ./chkuser3.sh nfc nfc cannot login.
11、写一个脚本,完成如下任务 :
(1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
(2) 复制目录时,才使用cp -r命令;
(3) 复制文件时使用cp命令;
(4) 复制链接文件时使用cp -d命令;
(5) 余下的所有类型,使用cp -a命令;
#!/bin/bash declare -i j=1 for i in /var/log/*;do [ ! -d /tmp/test$j ] && mkdir -p /tmp/test$j if [ -L $i ];then cp -d $i /tmp/test$j elif [ -f $i ];then cp $i /tmp/test$j elif [ -d $i ];then cp -r $i /tmp/test$j else cp -a $i /tmp/test$j fi let j++ done
执行结果: [root@www tmp]# tree . ├── test1 │ └── anaconda.ifcfg.log ├── test10 │ └── btmp ├── test11 │ └── ConsoleKit │ └── history ├── test12 │ └── cron ├── test13 │ └── dmesg ├── test14 │ └── dmesg.old ├── test15 │ └── dracut.log ├── test16 │ └── lastlog ├── test17 │ └── maillog ├── test18 │ └── messages ├── test19 │ └── mysqld.log ├── test2 │ └── anaconda.log ├── test20 │ └── secure ├── test21 │ └── spooler ├── test22 │ └── tallylog ├── test23 │ └── wtmp ├── test24 │ └── yum.log ├── test3 │ └── anaconda.program.log ├── test4 │ └── anaconda.storage.log ├── test5 │ └── anaconda.syslog ├── test6 │ └── anaconda.xlog ├── test7 │ └── anaconda.yum.log ├── test8 │ └── audit │ └── audit.log └── test9 └── boot.log
原创文章,作者:N26-西安-方老喵,如若转载,请注明出处:http://www.178linux.com/70445
评论列表(1条)
完成的很好,脚本的思路清晰,加油!