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)
上一篇 2016-08-15 16:55
下一篇 2016-08-15 21:45

相关推荐

  • 循环 函数 软件包 【中】

    循环 函数 软件包  【中】  创建无限循环 while true; do     循环体  done  until false; do     循环体  Done 特殊用法 while循环的特殊用法(遍历文件的每一行):      &nbs…

    Linux干货 2016-08-21
  • LVS 工作模型和调度算法

    简介   LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。 LVS是四层负载均衡,也就是说建立在OSI模型的第四层——传输层之上,传输层上有我们熟悉的TCP/UDP,LVS支持TCP/UDP的负载均衡 &nbs…

    Linux干货 2016-12-19
  • 大数据计算:如何仅用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
  • selinux

    [root@localhost ~]# ll /etc/sysconfig/selinux lrwxrwxrwx. 1 root root 17 May 10 16:38 /etc/sysconfig/selinux -> ../selinux/config [root@localhost ~]# cat /etc/selinux/confi…

    Linux干货 2017-05-15
  • httpd的介绍以及常用配置

    继上一篇写了LAMP的编译安装之后没有介绍如何配置使用,接下来的几篇会依次介绍,编译安装的过程为http://www.178linux.com/64006 一.httpd介绍 1.httpd是http协议的一个经典实现,也是apache组织中的一个顶级项目,其官方站点为httpd.apache.org。 2.httpd的运行机制 高度模块化(Core+Mod…

    Linux干货 2016-12-22
  • rsync+inotify实现数据同步——单向传输

    实验环境:<从A主机推送数据到B主机[B主机可以有多个]> A主机:10.1.43.102 B主机:10.1.43.103 配置流程: —rsyncserver—- <B主机上配置> 1.vim /etc/rsyncd.conf(用户,目录,模块,虚拟用户及密码文件) vim /etc/rsyncd….

    Linux干货 2016-10-27