bash脚本编程练习:判断、循环

 

1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;

#!/bin/bash
#
declare -i sumlogin=0
declare -i sumnologin=0
for i in $(cut /etc/passwd -d: -f7);do
if [[ "$i" == "/sbin/nologin" ]];then
let sumnologin++
else
let sumlogin++
fi
done
echo "totle nologin user $sumnologin\ntotle loging user $sumlogin"

执行结果:
~]# bash loginuser.sh
totle nologin user 23
totle loging user 9

2、写一个脚本 (1) 获取当前主机的主机名,保存于hostname变量中; (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com; (3) 否则,则显示当前主机名;

#!/bin/sh
#
hostname=$(hostname)
if [[ "$hostname" == "localhost.localdomain" ]];then
hostname www.void.com
echo "hostname change to $(hostname) ok"
else
echo "hostname is $hostname"
fi

执行结果:
a:hostname=localhost.localdomain
~]# bash hostname.sh
hostname change to www.void.com ok

b:hostname=test
~]# bash hostname.sh
hostname is test

3、写一个脚本,完成如下功能 (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在; (2) 如果存在,则显示此设备上的所有分区信息;

#!/bin/bash
#
if [ $# -lt 1 ];then
echo "At least one arg!"
exit 1
else
if fdisk -l $1 &>/dev/null;then
echo -e "$(fdisk -l $1)"
else
echo "The device is not exist!"
fi
fi

执行结果:
a:参数不存在
 ~]# bash device.sh
At least one arg!

b:设备不存在
~]# bash device.sh /dev/sdb
The device is not exist!

c:设备存在
~]# bash device.sh /dev/sda

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0004bb94

   设备 Boot  Start End  Blocks   Id  System
/dev/sda1   *2048 1026047  512000   83  Linux
/dev/sda2 10260484194303920458496   8e  Linux LVM

4、写一个脚本,完成如下功能 脚本能够接受一个参数; (1) 如果参数1为quit,则显示退出脚本, 并执行正常退出; (2) 如果参数1为yes,则显示继续执行脚本; (3) 否则,参数1为其它任意值,均执行非正常退出;

#!/bin/bash
#
read -p "Do you want to executing the script continue?[quit or yes]" arg
case $arg in
quit)
echo "script is quit"
exit 0
;;
yes)
echo "continue!"
;;
*)
echo "what!!!"
exit 2
;;
esac

执行结果:
~]# bash casetest.sh
Do you want to executing the script continue?[quit or yes]quit
script is quit
~]# echo $?
0
~]# bash casetest.sh
Do you want to executing the script continue?[quit or yes]yes
continue!
~]# echo $?
0
~]# bash casetest.sh
Do you want to executing the script continue?[quit or yes]aaaaaa
what!!!
~]# echo $?
2

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
#
time=$(date +%Y%m%d)
read -p "Please choose one compress Tool[gzip/bzip2/xz]" arg
case $arg in
gzip)
tar -zcf /backups/etc-$time.tar.gz /etc &>/dev/null
echo -e  "compress etc-$time.tar.gz ok"
;;
bzip2)
tar -jcf /backups/etc-$time.tar.bz2 /etc &>/dev/null
echo -e  "compress etc-$time.tar.bz2 ok"
;;
xz)
tar -Jcf /backups/etc-$time.tar.xz /etc &>/dev/null
echo -e  "compress etc-$time.tar.xz ok"
;;
*)
echo "error compress Tool"
;;
esac

执行结果:
~]# bash tar.sh
Please choose one compress Tool[gzip/bzip2/xz]gzip
compress etc-20161121.tar.gz ok
~]# ls /backups
etc-20161121.tar.gz

~]# bash tar.sh
Please choose one compress Tool[gzip/bzip2/xz]bzip2
compress etc-20161121.tar.bz2 ok
~]# ls /backups
etc-20161121.tar.bz2  etc-20161121.tar.gz

~]# bash tar.sh
Please choose one compress Tool[gzip/bzip2/xz]xz
compress etc-20161121.tar.xz ok
~]# ls /backups
etc-20161121.tar.bz2  etc-20161121.tar.gz  etc-20161121.tar.xz

~]# bash tar.sh
Please choose one compress Tool[gzip/bzip2/xz]test
error compress Tool

6、写一个脚本,接受一个路径参数: (1) 如果为普通文件,则说明其可被正常访问; (2) 如果是目录文件,则说明可对其使用cd命令; (3) 如果为符号链接文件,则说明是个访问路径; (4) 其它为无法判断;

#!/bin/bash
#
if [ $# -lt 1 ];then
echo "At least one arg!"
exit 1
fi

if ! [ -e $1 ];then
echo "file is not exist"
exit 2
fi

if [ -f $1 ];then
echo "this file can be access normal"
elif [ -d $1 ];then
echo "this file can be cd"
elif [ -h $1 ];then
echo "this is a link file"
else
echo "unkown"
fi

执行结果:
~]# bash file.sh
At least one arg!
~]# bash file.sh aaaa
file is not exist
~]# bash file.sh /etc
this file can be cd
~]# bash file.sh /etc/passwd
this file can be access normal

7、写一个脚本,取得当前主机的主机名,判断 (1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com; (2) 否则,显示现有的主机名即可;

#!/bin/bash
#
hostname=$(hostname)
if [ -z "$hostname" -o "$hostName" == "localhost.localdomain" -o "$hostname" == "localhost" ];then   
hostname mail.void.com
echo "hostname has change to $(hostname)"
else
echo "$hostname"
fi

执行结果:
~]# hostname
localhost.localdomain
~]# bash changehost.sh
hostname has change to mail.void.com

~]# hostname test
~]# hostname
test
~]# bash changehost.sh
hostname is test

8、写一脚本,接受一个用户名为参数; (1) 如果用户的id号为0,则显示其为管理员; (2) 如果用户的id号大于0且小于500, 则显示其为系统用户; (3) 否则,则显示其为普通用户;

#!/bin/bash
#
if [ $# -lt 1 ];then
echo "at lease one arg"
exit 1
fi

if ! id $1 &>/dev/null;then
echo "no such user"
else
userid=$(id -u $1)
if [ $userid -eq 0 ];then
echo "this is admin"
elif [ $userid -gt 0 ] && [ $userid -lt 200 ];then
echo "this is system user"
else
echo "this is normal user"
fi
fi

执行结果:
~]# bash readuser.sh
at lease one arg

~]# bash readuser.sh root
this is admin

~]# bash readuser.sh void
this is normal user

~]# bash readuser.sh test
no such user

~]# bash readuser.sh mail
this is system user

10、写一个脚本,传递一个用户名参数给脚本; (1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串; (2) 否则,则显示无法登录系统;

#!/bin/bash
#
if [ $# -lt 1 ];then
echo "at least one arg"
exit 1
fi

if ! id $1 &>/dev/null;then
echo "no such user"
exit 2
fi

userid=$(id -u $1)
if [ $userid -ge 500 ] && grep "^$1.*sh$" /etc/passwd &>/dev/null;then
echo "a user can log system"
else   
echo "the user can't login"
fi


执行结果:
~]# useradd test1 -u 550 -s /sbin/nologin
~]# useradd test2 -u 300 -s /bin/bash
~]# useradd test3 -u 551 -s /bin/bash

~]# bash userread.sh
at least one arg

~]# bash userread.sh ddddddd
no such user

~]# bash userread.sh test1
the user can't login
~]# bash userread.sh test2
the user can't login
~]# bash userread.sh test3
a user can log system

原创文章,作者:N23-苏州-void,如若转载,请注明出处:http://www.178linux.com/60331

(0)
N23-苏州-voidN23-苏州-void
上一篇 2016-11-22
下一篇 2016-11-22

相关推荐

  • Linux 发展史

    摘要:     Linux是一款基于Unix的服务端操作系统,目前已广泛应用于国内外的服务器端操作系统领域,其开源性是其获得及其快速的发展,目前至少已有数百种发行版本。 本文主要讲述了linux的系统简介、发展历程和Linux的发行版;     1、Linux的哲学思想:    &nbsp…

    Linux干货 2016-10-19
  • Centos6.5基于SSL密码认证部署ELK(Elasticsearch+Logstash+kibana)

    1       简介 本章我们来介绍Centos6.5基于SSL密码认证部署ELK(Elasticsearch 1.4.4+Logstash 1.4.2+kibana3),同时为大家介绍如何集合如上组件来收集日志,本章的日志收集主要为大家介绍SYSTEM日志收集. 集中化日志收集主要应用场景是在同一…

    Linux干货 2015-06-18
  • Nginx+Keepalived实现站点高可用

    Nginx+Keepalived实现站点高可用 vrrp 虚拟路由冗余协议(Virtual Router Redundancy Protocol,简称VRRP)是由IETF提出的解决局域网中配置静态网关出现单点失效现象的路由协议,1998年已推出正式的RFC2338协议标准。VRRP广泛应用在边缘网络中,它的设计目标是支持特定情况下IP数据流量失败转移不会引…

    Linux干货 2016-11-07
  • 马哥教育网络班21期-第九周课程练习

    第九周作业 1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; #!/bin/bash # declare -i nologin=0 declare -i other=0   &n…

    Linux干货 2016-09-19
  • ELK-5.5.1部署

    ELK简介 ELK Stack是软件集合Elasticsearch、Logstash、Kibana的简称,由这三个软件及其相关的组件可以打造大规模日志实时处理系统。 其中,Elasticsearch 是一个基于 Lucene 的、支持全文索引的分布式存储和索引引擎,主要负责将日志索引并存储起来,方便业务方检索查询。 Logstash是一个日志收集、过滤、转发…

    2017-12-04
  • DNS服务器以及子域授权

    1、安装bind      [root@www ~]# yum -y install bind 2、查看安装生成的文件      [root@www ~]# rpm -ql bind | less 3、程序路径     /var/named 4、配置…

    Linux干货 2017-05-31

评论列表(1条)

  • 马哥教育
    马哥教育 2016-11-30 21:21

    非常不错,完整地看来下有脚本有输出,展示出了扎实的脚本基本功,期待下次作业~