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

相关推荐

  • 从Linux小白到大牛——与狼共舞的日子12(上)

    马哥教育网络班21期+第12周课程练习 1、请描述一次完整的http请求处理过程; 1)建立或处理连接:客户端发送http请求报文,服务器端接收或拒绝请求; 2)接收请求:服务器端接收来自客户端对某些资源的请求; 3)处理请求:服务器端解析客户端请求报文,获取客户端请求的资源及请求方法等信息; 4)访问资源:服务器端获取客户端请求的资源; 5)构建响应报文;…

    Linux干货 2016-12-26
  • ssh+rsync批量管理,批量分发

    现在我简单架设了一个7台服务器的集群集体如下,架设集群的过程我就省略了… [nfs存储一台]192.168.42.10[负载均衡2台]192.168.42.40192.168.42.41[web服务器2台]192.168.42.30192.168.42.31[备份1台]192.168.42.20[mysql 1台]192.168.42.50 我现…

    Linux干货 2017-04-22
  • Linux中的用户、组和权限的管理

    一、Linux的安全模型     在Linux中用户登陆时必须提供用户名和密码(用户是由root用户创建的,最初的密码也是root用户设定的)。系统使用用户和群组来控制使用者访问文件和其他资源的权限。每一个文件都一定属于一个用户(一般该用户就是文件的创造者)并与一个群组相关。每一个进程(处理程序)都会与一个用户和群组关…

    Linux干货 2016-08-07
  • 网络中多网卡和多ip中的高可用

    一、虚拟网卡实现一个网卡多个地址 1、单个网卡实现多个ipv4地址,只需要在该网卡的配置文件的目录新增网卡配置文件即可。进入网卡"eth0"的目录下 2、新增网卡配置文件"ifcfg-eth0:0"和"ifcfg-eth0:1" 3、关掉NetworkManager服务 4、重启网卡,让系统重读配置…

    Linux干货 2016-09-10
  • Nginx及Nginx模块——更加轻量级的HTTP server

    Nginx engine X = Nginx      NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known f…

    Linux干货 2016-10-25
  • GNU awk

    awk -> gawk – 模式扫描和输出语言文件, pattern scanning and processing language 基本用法: gawk [options] 'program' FILE … program: PATTERN{ACTION ATATEMENTS} ACTION ATATEM…

    Linux干货 2016-09-22