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 00:38
下一篇 2016-11-22 12:31

相关推荐

  • ps查看进程

    ps命令 ps [OPTION]支持三种选项UNIX选项 如-A -eBSD选项 如aGNU选项 如–help选项:默认显示当前终端中的进程a 选项包括所有终端中的进程x 选项包括不连接终端的进程u 选项显示进程所有者的信息f 选项显示进程树,相当于 –forestk|–sort 属性 对属性排序,属性前加- 表示倒序o …

    2017-12-19
  • 用户及权限管理

     今天是学习马哥教育第四天,也是第一个博客作业,写一篇关于用户及权限管理的简介型的博客文章,作文水品有限,所以写出来有可能有病句或者意境有问题,请大家多多包涵。  首先,用户及权限管理,需要从2方面入手来说,首先来说用户管理。  何谓用户,这是马哥一上来就提到的问题,我简单的理解,用户其实就是一个人机交互的接口,人机交互的接口是…

    Linux干货 2016-09-15
  • 文件系统的挂载使用总结

    文件系统使用 除根文件系统以外的文件系统创建后要使用需要先挂载至挂载点后才可以被访问,挂载点即分区设备文件关联的某个目录文件,挂载命令mount和 卸载命令umount; 挂载点: mount_point,作为被挂载的文件系统的访问入口; 作为挂载点需要满足三个条件:  (1)这个目录事先存在  (2)使用未被或不会被其他进程使用到的目录…

    系统运维 2016-11-19
  • 超文本传输协议-HTTP

        超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式、协作式和超媒体信息系统的应用层协议。HTTP是万维网的数据通信的基础。     设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。通过HTTP或…

    Linux干货 2017-07-31
  • Linux基础知识(二)

    一、Linux文件管理类命令的使用方法及其相关示例演示? 1、文件管理 (1)mkdir (2)rmdir (3)cp (4)mv (5)rm (6)touch 2、文件查看 (1)ls (2)tree (3)cat&tac (4)head (5)tail (6)more&less (7)stat 二、Bash特性详解 1、命令行展开 (1)…

    Linux干货 2016-09-27
  • Linux下各类颜色文件的意思

    Linux下各类颜色文件的意思   Linux系统对在终端下的各文件进行了颜色区分,了解各个颜色文件对应的意思对是学习Linux必须要掌握的。Linux文件颜色如下图: 1、白色:表示普通文件(-) 2、蓝色:表示目录(d) 3、绿色:表示可执行文件(-) 4、红色:表示压缩文件(-) 5、浅蓝色:链接文件(l) 6、黄色:表示设备文件(c) 7、…

    Linux干货 2016-10-18

评论列表(1条)

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

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