linux中if case语句用法示例

shell中的if 和case两个条件语句

1. if的语法1:
    if
    条件
    then
    commands
    else
    commands
    fi

if的语法2:
  语法:if 条件
        then
          commands
        elif 条件
        then
           commands
        elif 条件
        then
            commands
        ....
        else
           commands
        fi

        说明:read  是读取用户输入的一行

if语句的一些说明

   首先:if 条件1
        then
          commands
        elif 条件2
        then
           commands
        elif 条件3
        then
            commands
        ....
        else
           commands
        fi

中的条件1,2,3等,都是同等地位的,也就是相当于条件1不满足,则看条件2,条件1、2都不满足,则看条件3 以此往下推(-a 为and的意思,但是不能用&&替代,否则会报错,-o 为or意思)。

case条件语句:
   语法: case 条件 in
          xxx)
          commands;;
          xxx)
          commands;;
          xxx)
          commands;;
          esac

   说明:这个esac 就是case的结束,想if...fi 一样的,
         注意commands;; 中的“;;”不能少掉。

例子
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 ,提示用户输入 yes 或 no, 并判断用户输入的是 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

时间: 2024-11-03 23:51:39

linux中if case语句用法示例的相关文章

html 中 #include file 的用法示例介绍

 html 中 #include file 的用法示例介绍 有两个文件a.htm和b.htm,在同一目录下a.htm内容如下 代码如下: <!-- #include file="b.htm" --> b.htm内容如下 今天:雨 31 ℃-26 ℃ <br />明天:雷阵雨 33 ℃-27 ℃ 直接在浏览器中打开a,没有任何显示,后来知道,include是SSI(Server Side Include),在html中不支持include,之后把a.htm改成a.

Javascript中For In语句用法实例

  本文实例讲述了Javascript中For In语句用法.分享给大家供大家参考.具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html;

python中while循环语句用法

  这篇文章主要介绍了python中while循环语句用法,以一个简单实例形式分析了Python使用while循环语句使用方法,需要的朋友可以参考下 ? 1 2 3 4 number = 1 while number < 20: print(number) number += 1 运行结果如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 希望本

linux中cat命令的用法

今天需要用cat查看文件,无奈忘记如何使用,于是整理了一下 linux中cat命令的用法:  1.用cat命令查看文件:$ cat file1;  $ cat -n file1:在查看的时候加上行号: cat命令还可以使用通配符:$ cat -n file*,把所有的file文件都显示出来(顺序显示)  也可以这样:$ cat -n file1 file2 file3 ;结果用通配符输出结果是完全一样的  2.合并文件 可以用cat命令和重定向操作符(>>)来合并文件: $ cat file*

JavaScript中的标签语句用法分析_javascript技巧

本文实例分析了JavaScript中的标签语句用法.分享给大家供大家参考.具体分析如下: 最近在看w3school,然后看到js部分, <!DOCTYPE html> <html> <body> <script> cars=["BMW","Volvo","Saab","Ford"]; list: { document.write(cars[0] + "<br>

图片-switch语句中各个case语句与default的次序不影响执行结果吗?

问题描述 switch语句中各个case语句与default的次序不影响执行结果吗? 解决方案 解决方案二: switch...case... 语句switch case 语句基础知识之switch case语句 解决方案三: http://zhidao.baidu.com/link?url=wmBFivbZ3tyO_HPJvx3coYKWoVtAhIXR8cfkaEU-Zea5u8nkB2Um5pwvPExjMCrihmtMY0N8dQ1si17IkSHW2K 解决方案四: 你的外层switc

Linux中yum和apt-get用法及区别

Linux中yum和apt-get用法及区别 一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包格式 rpm包,安装rpm包的命令是"rpm -参数" 2 包管理工具 yum 3 支持tar包 Debian系列 1 常见的安装包格式 deb包,安装deb包的命令是"dpkg -参数" 2 包管理工具 apt-get 3

python中list循环语句用法实例_python

本文实例讲述了python中list循环语句用法.分享给大家供大家参考.具体用法分析如下: Python 的强大特性之一就是其对 list 的解析,它提供一种紧凑的方法,可以通过对 list 中的每个元素应用一个函数,从而将一个 list 映射为另一个 list. 实例 复制代码 代码如下: a = ['cat', 'window', 'defenestrate'] for x in a:      print x, len(x) for x in [1, 2, 3]: print x,    

thinkPHP模板中for循环与switch语句用法示例_php实例

本文实例讲述了thinkPHP模板中for循环与switch语句用法.分享给大家供大家参考,具体如下: 1.for用法 <for start="开始值" end="结束值" comparison="" step="步进值" name="循环变量名" > </for> 案例 <for start="1" end="100"> {$i}