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

相关推荐

  • vim的使用

    vim 编辑器  vim的模式:      编辑模式      输入模式      扩展编辑模式:在编辑模式中输入:  ====================================   编辑模式转换成输入模式 &nbs…

    Linux干货 2016-08-11
  • linux下逻辑卷管理LVM

    LVM:逻辑卷管理 LVM这个技术就是把底层的存储设备组成一个卷组,底层存储设备的存储空间会变成一个个PE(盘区,大小为2的n次方),这个卷组里就是一个个的PE,然后,这个卷组会重新分区,这些分区就为逻辑卷,这些逻辑卷都是由卷组里分配的PE组成。 关于LVM里的命令: pv的命令: pvcreate pvs pvdisplay pvscan pvremove…

    Linux干货 2015-08-04
  • N25第十周博客作业

    1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)        (1)post:加电自检                主要是bios完成,检查主机各个组件是否完好,能否达到运行要求;    &…

    Linux干货 2017-03-10
  • httpd 2.2(Apache Web服务器)

    apache (Web服务器) 简介: Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。    Apache HTTP服务器是一个模块化的服…

    Linux干货 2016-03-30
  • 机器学习排序

     从使用的数据类型,以及相关的机器学习技术的观点来看,互联网搜索经历了三代的发展历程。        第一代技术,将互联网网页看作文本,主要采用传统信息检索的方法。        第二代技术,利用互联网的超文本结构,有效…

    Linux干货 2015-12-15
  • M20 – 1- 第二周博客(1):帮助手册查询的使用

    Linux系统中存在诸多的命令,而这些命令分为内键命令和外部命令。如何查询命令属于内部还是外部呢。前一篇博客已经介绍type命令,type命令可以区分命令属于内部还是外部,下面举个例子: 为什么这一期博客还重复讲如何区分内部与外部命令呢,因为在man手册查询中,内部命令并不是使用man查询帮助手册,仅仅使用与外部命令,那内键命令如何查询呢,接下来要讲的就是h…

    Linux干货 2016-08-02