shell脚本编写-2

1、条件判断if语句  

    1)、 单分支

if  判断条件;then

    条件为真的分支代码

    fi

2)、双分支

    if  判断条件; then 

    条件为真的分支代码

    else

    条件为假的分支代码

    fi

逐条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if语句

例如:if ping -c1 -W2 station1 &> /dev/null; then

echo 'Station1 is UP'

elif grep "station1" ~/maintenance.txt &> /dev/null;then

echo 'Station1 is undergoing maintenance’

else

echo 'Station1 is unexpectedly DOWN!'

exit 1

   fi

2、条件判断case语句

case 变量引用 in

PAT1)

分支1

;;

PAT2)

分支2

;;

*)

默认分支

;;

Esac

case 支持glob风格的通配符(正则表达式):

*: 任意长度任意字符

?: 任意单个字符

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

a|b: a或b

3、练习题示例

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

#!/bin/bash

if grep “^$1\>” /etc/passwd >> /dev/dull;then

echo “the username exists”

exit

else

useradd $1

getent passwd $1

fi

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

第一种方法:

#!/bin/passwd

read -p "please input the yes or no:" a

if [ $a == yes ];then

echo "yes"

elif [ $a == y ];then

echo "yes"

elif [ $a == Y ];then

echo "yes"

elif [ $a == YES ];then

echo "yes"

elif [ $a == no ];then

echo "no"

elif [ $a == n ];then

echo "no"

elif [ $a == N ];then

echo "no"

elif [ $a == NO ];then

echo "no"

else

echo "other"

fi    

第二种方法:

#!/bin/bash

read –p “please inout yes or no:” a

case $a in

[yY]|[yY][Ee][sS])

echo “yes”

;;

[Nn]|[Nn][Oo])

echo “no”

;;

*)

echo “other”

;;

esac     

注意case语句:

case支持glob风格的通配符(正则表达式):

*: 任意长度任意字符

?: 任意单个字符

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

a|b: a或b     

第三种方法:

#!/bin/bash

read -p "please inout yes or no:" a

ans=`echo $a|tr 'A-Z' 'a-z'`

if [ $ans == yes -o $ans == y ]

then

echo "yes"

elif [ $ans == no -o $ans == n ]

then

echo "no"

else

echo "other"

fi                                      

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

第一种方法:

#!/bin/bash

read -p "please input the file path:" a

[ ! -e $a ] && echo "the file not exist" && exit

if [ -f $a ];then

echo "this file is common"

elif [ -d $a ];then

echo "this file is directory"

elif [ -h $a ];then

echo "this file is link"

else

echo "this file is other"

fi

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

首先如何用已有知识表示正整数,注意01也是正整数,可以用正则表达式0*[1-9][0-9]*

#!/bin/bash

read -p "please input the argument:" a

if [[ $a =~ ^0*[1-9][0-9]*$ ]];then

echo "this arg is zzshu"

else

echo "this arg isn't zzshu"

fi

 



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

(0)
上一篇 2016-08-15 09:22
下一篇 2016-08-15 09:24

相关推荐

  • Shell——Linux基本命令(2)

    1.Shell Shell是Linux系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行. shell也被称为LINUX的命令解释器(commandinterpreter). vshell是一种高级程序设计语言. 2.bash shell GNUBourne-AgainShell(bash)是GNU计划中重要的工…

    2017-07-13
  • 如何将迁移home到独立分区中

        背景(centos7中):         由于在刚开始学习linux时磁盘分配追求简单,只是把一个磁盘简单的分了3个分区。现在,因用户数据增多、磁盘空间变少、当数据撑满磁盘时,会导致系统崩溃。为防止此种情况的发生我必须把把根下的/home 目录迁移出 来独立分…

    2017-08-10
  • Linux进程及作业管理总结

    一、简介     在使用Windows操作系统中很多时候需要查看某些程序进程的运行情况,一般来说我们可以打开Windows提供的"任务管理器",然后点击"进程"栏即可查看到当前系统运行的进程列表。例如偶尔出现系统内存、CPU占用过高的时候,我们往往都会查看进程列表,并找到当前占用内存或CPU过高的进…

    Linux干货 2015-10-05
  • 运维工程师技能需求排行

    这是我今天在拉勾网搜索运维,翻完了4四页也招聘信息之后得到的,我的目的是想要看看之后的学习,哪个更应该成为重点,有些在我意料之中,有些还真的没想到,算是努力了一个小时的收获吧,分享给大家。
    注意:其中的看法仅代表个人观点,很多都是依靠我自己的学习经验和工作经验累积的

    Linux干货 2017-12-12
  • CentOS 7破解密码教程

    启动时任意键暂停启动  按e键进入编辑模式  将光标移动linux16开始的行,添加内核参数rd.break  按ctrl-x启动  mount –o remount,rw /sysroot  chroot /sysroot  passwd root  touch /.autorelabe…

    Linux干货 2016-12-01
  • 实验:系统启动和内核管理

    实验:bootloader 破坏stage1: 备份:dd if=/dev/sda of=/app/mbr bs=1 count=512 破坏:dd if=/dev/zero of=/dev/sda bs=1 count=446;此时/boot/grub/下的文件没有任何改动; 表现:stage1破坏后系统会直接进行光盘引导 修复1:救援模式–&…

    Linux干货 2017-05-14