shell脚本2——顺序选择语句

流程控制

     顺序执行

     选择执行

     循环执行

顺序执行:

    条件选择:if语句

if语句为选择执行

注意:if语句可嵌套

单分支

if  判断条件:then
    条件为真的分支代码
fi

双分支

if  判断条件; then
    条件为真的分支代码
else
    条件为假的分支代码
fi

多分支

if  CONDITION1 ; then
    if-true
elif CONDITION2 ; then
    if-ture
elif CONDITION3 ; then
    if-ture
...
else
    all-false
fi

    条件判断:case语句

case 变量引用 in
    PAT1)
        分支1
        ;;
    PAT2)
        分支2
        ;;
    ...
    *)
        默认分支
        ;;
esac

case 支持glob 风格的通配符:

    *:任意长度任意字符

    ?:任意单个字符

    []:指定范围内的任意单个字符

    a|b:a或b

练习:

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

#!/bin/bash
#descriptio
#version 0.1
#author gm
#date 20160812
if `id $1 &> /dev/null`;then
    echo "$1 user is exist;"
else
    useradd $1
    echo "useradd $1."
    echo "$1 `id $1`"
fi

[root@CentOS6 bin]# createuser.sh  gao
gao user is exist;
[root@CentOS6 bin]# createuser.sh  test
useradd test.
test uid=522(test) gid=522(test) groups=522(test)

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

#!/bin/bash
#description
#version 0.2
#author gm
#date 20160812
read -p "please input yes or no: " string
case $string in
    [yY]|[yY][eE][sS])
        echo "user input is yes.";;
    [nN]|[nN][oO])
        echo "user input is no";;
    *)
        echo "user input is other";;
esac

[root@CentOS6 bin]# yesorno.sh
please input yes or no: yse
user input is other
[root@CentOS6 bin]# yesorno.sh
please input yes or no: yes
user input is yes.
[root@CentOS6 bin]# yesorno.sh
please input yes or no: y
user input is yes.

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

#!/bin/bash
#description
#version 0.1
#author gm
#date 20160812
read -p "please file path: " path
if [ ! -e $path ] ;then
    echo "$path is not exist."
elif [ -h $path ] ;then
    echo "$path is link file."
elif [ -f $path ] ;then
    echo "$path is common file."
elif [ -d $path ] ;then
    echo "$path is dir file"
else
    echo "$path is other file."
fi

[root@CentOS6 bin]# filetype.sh
please file path: /etc
/etc is dir file
[root@CentOS6 bin]# filetype.sh
please file path: /dev/sda
/dev/sda is other file.
[root@CentOS6 bin]# filetype.sh
please file path: /etc/fstab
/etc/fstab is common file.

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

#!/bin/bash
#description
#version 0.2
#author gm
#date 20160812
read -p "please ont number of int: " num
testnum=$num
echo $num | grep -qE "^[0-9]+$"
if [ $? -eq 0 ] ; then
    if [ $num -ne 0 ] ; then
        echo "$num is positive integer."
    else
        echo "$num in not positive integer."
    fi
else
    echo "$num is not positive integer."
fi

[root@CentOS6 bin]# checkint.sh
please ont number of int: 123
123 is positive integer.
[root@CentOS6 bin]# checkint.sh
please ont number of int: sdfj
sdfj is not positive integer.
[root@CentOS6 bin]# checkint.sh
please ont number of int: 1.123324
1.123324 is not positive integer.
[root@CentOS6 bin]# checkint.sh
please ont number of int: -123.123
-123.123 is not positive integer.

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

(0)
megedugaomegedugao
上一篇 2016-08-18 10:09
下一篇 2016-08-18 10:09

相关推荐

  • 第一天上课

                      2016.10.11 第一天上课 上课环境安排 电脑IP安排 学生IP地址安排     上课软件安装    虚拟机安装 操作系统发展史    第一代计算机:电子管计算机&nb…

    Linux干货 2016-10-18
  • 没有自动ip解决办法

    用虚拟机下载好centos6.9后,本来想查看ip地址,结果发现没有自动获取ip地址,在网上找了很多方法都不行,问题如下: 用命令ifconfig查看ip地址,发现没有,如下 查看eth0如下 解决办法如下: 1、关闭NetworkManager服务 2、关闭NetworkManager开机启动 3、添加 /etc/sysconfig/network-scr…

    2017-07-16
  • RAID详解

    测试机centos6.7 x86_64 一、RAID是什么     简单描述:RAID(Redundant Array of indenpensive Disk)独立磁盘冗余阵列:磁盘阵列是把多个磁盘组成一个阵列,当作单一磁盘使用,它将数据以分段或条带(striping)的方式储存在不同的磁盘中,存取数据时,阵列中的相关磁盘一起…

    2016-02-14
  • HAProxy实战(一)

    实验目的 测试基于haproxy的反代和负载均衡配置 测试keepalived高可用haproxy的效果 实验要点 (1) 动静分离discuzx,动静都要基于负载均衡实现;(2) 进一步测试在haproxy和后端主机之间添加varnish缓存;(3) 给出拓扑设计;(4) haproxy的设定要求: (a) 启动stats;(b) 自定义403、502和5…

    Linux干货 2017-02-13
  • shell脚本编程之函数

      在编写脚本时经常会遇到某个任务需要重复使用的问题,需每次都要输入同样的代码是件挺烦人的事情,还好可以通过编写函数还简化这项工作。   函数其实就是给一段代码起个名字,在每次使用这段代码的时候可以直接使用函数名来调用就可以了。 一、创建函数   创建函数的格式有两种:   格式1:function 函数名{ &nbs…

    Linux干货 2016-01-05
  • 配额、RAID、软RAID以及LVM管理

    磁盘配额允许控制用户或者组织对磁盘的使用,它能防止个人或者组织使用文件系统中超过自己使用的部分,或者造成系统完全拥堵。配额必须由root用户或者具有root权限的用户启用和管理。 硬RAID以及软RAID :RAID是Redundant Array of Independent Disks的简写,即独立硬盘冗余阵列,简称磁盘阵列。通过实现的方式不同…

    Linux干货 2016-11-23