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

相关推荐

  • 马哥linux课堂笔记

    0805课堂笔记 基本权限 读,写,执行   对于文件的读权限,能否查看文件内容.(文件内容需不需要看,如果是文本就需要看,如果是二进制就不需要看) 对于文件的写权限,能否修改文件内容,不能修改文件名,不能删除文件.(如果需要修改文件内容和文件名,必须在其父目录增加写权限和执行权限) 对于文件的执行权限,能否运行他.   对于目录的读权限…

    Linux干货 2016-08-15
  • 第1周作业

    第一周作业   1、描述计算机的组成及其功能。 计算机由运算器,控制器,存储器,输入装置和输出装置五大部件组成计算机 运算器,控制器:CPU 存储器:内存和硬盘 输入装置和输出装置:键鼠和显示器   2、按系列罗列Linux的发行版本,并描述不同版本的联系和区别。   linux的发行版本大致分为两大类,一类是商业公司维护的发行…

    Linux干货 2017-08-24
  • N26-第四周博客

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost skel]# cp -r /etc/skel /home/tuser1[root@localhost skel]# chmod -R go=— /home/tuser1 2、编辑/etc/…

    系统运维 2017-02-08
  • 计算机的发展及组成

    计算机发展 计算机(Computer):俗称电脑,是一种能接收和存储信息,并按照存储在其内部的程序对海量数据进行自动、高速地处理,然后把处理结果输出的现代化智能电子设备。 1946年,世界上诞生了第一台计算机,用于军事,计算弹道。 1946年数学家冯.诺依曼提出冯诺依曼体系结构,即所有计算机由运算器、控制器、存储器、输入设备、输出设备组成,所有遵循冯.诺依曼…

    Linux干货 2017-07-13
  • N22期-第4周作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost ~]# cp -r /etc/skel /home/tuser1 [root@localhost ~]# chmod&nb…

    Linux干货 2016-09-05
  • CentOS 6 启动流程

    CentOS6启动流程 1.加载BIOS的硬件信息,获取第一个启动设备 2.读取第一个启动设备MBR的引导加载程序(grub)的启动信息 3.加载核心操作系统的核心信息,核心开始解压缩,并尝试驱动所有的硬件设备 4.核心执行init程序,并获取默认的运行信息 5.init程序执行/etc/rc.d/rc.sysinit文件 6.启动核心的外挂模块 7.ini…

    2017-09-01