if case语句练习

 1、 写一个脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息

[root@localhost bin]# cat createuser.sh
#!/bin/bash
# Date: 2016.8.12
# Author: cyh
# Description: Determine whether a user exists
 
user=`cut -d: -f1 /etc/passwd |grep -o "$1"`
[ $# -ne 1 ] && exit 1
if [[ -n $user ]]; then
         echo "User already exists!"
         echo "The user information: `grep "^$1\>" /etc/passwd |cut -d: -f 1,3`"
else
         useradd $1 > /dev/null
         echo "The user has been created!"
         echo "The user information: `grep $1 /etc/passwd |cut -d: -f 1,3`"
fi

 

脚本测试

[root@localhost bin]# getent passwd tom
[root@localhost bin]# createuser.sh tom
The user has been created!
The user information: tom:2028
[root@localhost bin]# getent passwd tom
tom:x:2028:2033::/home/tom:/bin/bash
[root@localhost bin]#
[root@localhost bin]# getent passwd zhangsan
zhangsan:x:2022:2024::/home/zhangsan:/bin/bash
[root@localhost bin]# createuser.sh zhangsan
User already exists!
The user information: zhangsan:2022

 

2、写一个脚本/root/bin/yesorno.sh,提示用户输入yesno,并判断用户输入的是yes还是no,或是其它信息

[root@localhost bin]# cat yesorno.sh
#!/bin/bash
# Description: Determine the user input characters(yes or no).
 
read -t5 -p "Please input yes or no: "  ans
[ -z $ans ] && echo "You have no input, script has quit" && exit 10
case $ans in
[yY][eE][sS]|y|Y)
         echo "Input is yes!"
         ;;
[nN][oO]|n|N)
         echo "Input is no!"
         ;;
*)
         echo "Input error, please enter again."
         ;;
esac

脚本测试

[root@localhost bin]# yesorno.sh
Please input yes or no: y
Input is yes!
[root@localhost bin]# yesorno.sh
Please input yes or no: yEs
Input is yes!
[root@localhost bin]# yesorno.sh
Please input yes or no: nO
Input is no!
[root@localhost bin]# yesorno.sh
Please input yes or no: n
Input is no!
[root@localhost bin]# yesorno.sh
Please input yes or no: tt
Input error, please enter again.

 

3、写一个脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)

[root@localhost bin]# cat filetype.sh
#!/bin/bash
# Author: cyh
# Date: 2016.8.15
# Description: Input a file, and determine the type of the file
 
if [ $# -ge 1 ]; then
         if [ -z $1  ]; then
                   echo "Your input is empty"
         elif [ -d $1 ]; then
                   echo "this is a directory."
         elif [ -h $1 ]; then
                   echo "this is a link file."
         elif [ -f $1 ]; then
                   echo "this is a common file."
         elif [ -c $1 ]; then
                   echo "this is a character device file."
         elif [ -b $1 ]; then
                   echo "this is a block device file."
         elif [ -s $1 ]; then
                   echo "this is a socket file."
         elif [ -p $1 ]; then
                   echo "this is a pipe file."
         else
                   echo "The input character is empty or file does not exist,please specify again."
         fi
else
         read -t 10 -p "Please input file name: " file
         [[  ! -e $file || -z $file ]] && echo "The input is empty or file does not exist,please specify again."  && exit 
         type=`ls -ld $file 2> /dev/null | cut -c1`
         case $type in
         d)
                   echo "this is a directory."
                   ;;
         -)
                   echo "this is a common file."
                   ;;
         l)
                   echo "this is a link file."
                   ;;
         s)
                   echo "this is a socket file."
                   ;;
         c)
                   echo "this is a character device file."
                   ;;
         b)
                   echo "this is a block device file."
                   ;;
         p)
                   echo "this is a pipe file."
                   ;;
         *)
                   echo "this not is file."
         esac
fi

 

脚本测试

[root@localhost bin]# filetype.sh dfdfd
The input character is empty or file does not exist,please specify again.
[root@localhost bin]# filetype.sh /var
this is a directory.
[root@localhost bin]# filetype.sh /var/log/
this is a directory.
[root@localhost bin]# filetype.sh /var/log/messages
this is a common file.
[root@localhost bin]# filetype.sh /tmp/cat
this is a link file.
[root@localhost bin]# filetype.sh /tmp/hogsuspend
this is a pipe file.
[root@localhost bin]# filetype.sh /var/log/messages21
The input character is empty or file does not exist,please specify again.
 
[root@localhost bin]# filetype.sh
Please input file name: dfsdfsfq2
The input character is empty or file does not exist,please specify again.
[root@localhost bin]# filetype.sh
Please input file name: /etc/fstab
this is a common file.
[root@localhost bin]# filetype.sh
Please input file name: /tmp/cat
this is a link file.
[root@localhost bin]# filetype.sh
Please input file name: /tmp/hogsuspend
this is a pipe file.

 

4、写一个脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数

[root@localhost bin]# cat checkint.sh
#!/bin/bash
#
 
read -p "Please input an integer:" integer
#[ $integer -lt 0 ] && echo "Please enter at least one value" && exit 10
if [[ $integer =~ ^0*[0-9][0-9]*$ ]]; then
         echo "this is an integer"
else
         echo "this not an integer"
fi

脚本测试

[root@localhost bin]# checkint.sh
Please input an integer:12
this is an integer
[root@localhost bin]# checkint.sh
Please input an integer:3.14
this not an integer
[root@localhost bin]# checkint.sh
Please input an integer:df
this not an integer
[root@localhost bin]# checkint.sh
Please input an integer:09   
this is an integer
[root@localhost bin]# checkint.sh
Please input an integer:9f
this not an integer

 

原创文章,作者:cyh5217,如若转载,请注明出处:http://www.178linux.com/35908

(0)
cyh5217cyh5217
上一篇 2016-08-15 16:55
下一篇 2016-08-15 21:45

相关推荐

  • shell脚本编程初步

    shell脚本编程初步    随着课程学习的深入,我们已经学习了一些常用的命令,会去解决了一部分简单的问题,但是这不能满足复杂的工作,它可以帮助我执行自动化的常用命令、执行系统管理和故障排除、创建简单的应用程序、处理文本或文件。所以我们开始了shell脚本的编程,帮助我们更好的去完成工作。简单地说,shell编程相当于帮我们之前…

    Linux干货 2016-08-15
  • Linux文件查找命令find和压缩解压缩

    概述     Linux系统上一切皆文件,那么随着时间的推移和管理的服务器增加,在众多的文件中,如何找到我们需要的文件,这就需要用到文件查找命令了。本章就分享下关于文件查找命令的一些内容和压缩解压缩的相关内容。     具体分为一下几个部分:    …

    Linux干货 2016-08-16
  • LAMP—Apache编译安装

    一、前言:   httpd-2.2与 httpd-2.4版本相比增加了许多新特性:    1、MPM支持运行时装载    2、支持event    3、支持异步读写    4、支持每模块每目录使用不同的日志级别    5、每请求配置<IF&gt…

    Linux干货 2015-06-15
  • 包管理及源码安装Apache

    一,概述 yum 仓库的安装 在/etc/yum.repos.d/目录下创建后缀名为repo的配置文件 [CentOS7] name= baseurl= gpgcheck= enabled= 配置文件基本包含的四个要求 安装及升级本地程序包: * localinstall rpmfile1 [rpmfile2] […] (用install替代) …

    Linux干货 2016-09-01
  • Linux运维学习历程-第六天-Linux重定向和管道

    Linux运维学习历程-第六天-Linux重定向和管道 2 本章内容我们将学习linux中的重定向和管道两大用法   I/O输入与输出设备   重定向   管道   tee命令与tr命令 一、I/O设备   1、什么是I/O设备   管理和控制计算机的所有输入/输出(I/O)设备是操作系统…

    Linux干货 2016-08-03
  • 浅述vim操作

    vim是一种比vi更加强大的模式化的全屏文本编辑器。vim在工作过程中有三种模式:编辑模式、插入模式、末行模式。 编辑模式(默认):又称命令模式,其工作内容包括移动光标、剪切、粘贴、删除等 输入模式:亦可称为插入模式,主要是在文件中修改文本内容 末行模式:亦称扩展命令模式,主要是执行vim内置命令的 vim三种模式之间的切换:如下图   (1)编辑…

    Linux干货 2016-08-11