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

相关推荐

  • 详解用户,组,权限管理

    详解用户,组,权限管理 M21-陆东贵 测试环境:CentOS 7.2 内容介绍: 我们要使用操作系统进行工作,就需要人机交互操作,这时候计算机就需要创建用户,通过用户来实现操作,而且随着用户的增加需要给用户分组,这时候就需要对文件进行权限管理。 需要使用的工具:       用户相关工具:useradd; …

    Linux干货 2016-10-25
  • 运维自动化之系统安装

    自动化安装系统,cobbler的安装使用

    Linux干货 2018-01-15
  • 软件包管理–rpm、yum

    rpm软件包管理,rpm命令的使用。yum命令使用,yum源的搭建,网络yum源的搭建方法。编译安装http

    Linux干货 2017-12-03
  • Centos7上利用corosync+pacemaker+crmsh构建高可用集群

    一、高可用集群框架 资源类型:     primitive(native):表示主资源     group:表示组资源,组资源里包含多个主资源     clone:表示克隆资源     master/…

    Linux干货 2016-05-29
  • 大数据计算:如何仅用1.5KB内存为十亿对象计数

    Big Data Counting: How To Count A Billion Distinct Objects Using Only 1.5K This is a guest post by Matt Abrams (@abramsm), from Clearspring, discussing how they are able to accurat…

    Linux干货 2015-04-08
  • 文本查看类命令

    文本查看相关的命令:1.  cat – 串连文件,并以标准输出显示语法:cat [OPTION]… [FILE]… 常用选项 选项 | 含义—— | ——-n | 在显示文件内容时打印行号示例:~]# cat -n /etc/rc.d/init.d/functions显示/e…

    2017-09-07

评论列表(1条)

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

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