zoukankan      html  css  js  c++  java
  • shell编程2

    -------条件测试
    test命令用于测试字符串,文件状态和数字。

    1、测试文件状态

    test命令一般有2种格式:test condition或[ condition ]使用方括号时注意在条件两边加上空格。

    常用的测试文件的条件表达式有:

    -d:目录;-s:文件长度大于0、非空;-f:正规文件;-w:可写;-L:符号连接;-u:文件有suid位设置;-r:可读;-x可执行。

    如:测试first文件是否可写:

    test -w first

    echo $?

    返回0说明可写,否则不可写;或着:

    [ -w first ]

    echo $?

    可以实现同样的功能。

    2、字符串测试

    字符串测试有5种格式:

    test 'string'

    test string_operator 'string'

    test 'string1" string_operator "string2'

    [ string_operatoer 'string' ]

    [ 'string1' string_operator 'string2' ]

    其中string_operator可以是以下符号:

    =:  两个字符串相等;

    !=: 两个字符串不等

    -z: 空串

    -n: 非空串

    如:a="dfsd"  b="sdgfg"测试a和b是否相等为:

    [ '$a' = '$b']

    echo $?

    返回0说明相等,否则不相等。

    3、测试数值

    测试数值可以使用很多操作符,一般格式如下:"number1" numeric_operator "number2"或者[ "number1" numeric_operator "number2" ]其中numeric_operator可以是以下内容:

    -eq:数值相等;-ne:数值不相等;-gt:第一个数大于第二个数;-lt:第一个数小于第二个数;-le:第一个数小于等于第二个数;-ge:第一个大于等于第二个数。

    如测试两个数是否相等:

    [ '100' -eq '130']

    echo $?

    返回0说明相等,否则不相等;测试变量var1=100 和 var2=130是否相等为: [ $var1 -eq $var2 ]  echo $?

    还可以使用逻辑操作符将两个测试表达式结合起来,如果都为真则结果为真:如

    [ '100' -gt '200' -a '300' -le '400' ]

    echo $?

    -----------控制流结构

    1、for循环

    先看一个例子:

    #!/bin/sh

    for i in 1 3 5 7 9 2 4 6 8 10

    do

         echo  $i

    done

    执行结果为:

    1

    3

    5

    7

    9

    2

    4

    6

    8

    10

    in列表可以是shell命令的输出,如:

    #!/bin/sh

    for name in 'ls'

    do

    if [ -f $name ] ; then

           echo '$name is a regular file'

    elif [ -d $name ] ; then

           echo '$name is a directory'

    fi

    done

    需要指出,可以没有in语句,这是将等价于in '$@',如:

    #!/bin/sh

    i=1

    for param

    do

    echo 'parameter #$i is $param'

    i-$i+1

    done

    2、until循环

    until循环就是通常所说的直到型循环,如:

    #!/bin/sh

    until [ i -f a.lock ]

    do

      sleep 1

    done

    echo "you string start another application safely"

    b

    上例的意义为:a.lock是一个锁文件,当a运行时创建该文件,因而b就可以知道a在运行;如果a停止运行,就删除该文件,因而b就知道a已经停止运行,可以安全启动了。

    3、while循环

    while循环的意义就是当条件成立时,继续往下执行,如:

    #!/bin/sh

    while read string

    do

     echo 'you string is $string'

    done

    echo "thank you , bye"

    该脚本接受用户输入并打印到屏幕上,如果想结束,使用Ctrl+d组合键。

    4、使用break和continue控制循环

    下面是一个使用break跳出循环的例子,该脚本一直执行,除非按下Ctrl+d组合键:

    #!/bin/sh

    while read string

    do

    if [ $? != 0] ; then

    break

    fi

    done

    下面是一个使用continue的例子:

    #!/bin/sh

    for  ( i=0; i<20; i=$i+1)

    do

    echo -n $i

    if [ $i -lt 10 ] ; then

    echo ''

    continue

    fi

    echo 'this is a two digtal nenmber'

    done

    5、if then else语句

    下面例子判断输入的分数等级:

    #!/bin/sh

    if [ $1 -lt '0' ] ; then

    echo "erro"

    elif [ '$1' -lt '60' ] ; then

    echo "no pass"

    elif [ $1 -lt '70' ] ; then

    echo "pass"

    elif [ $1 -lt '80' ] ; then          #或者elif [ $1 -lt '80' ]   then

    echo "good"                           #echo "good"

    elif [ $1 -le '100' ] ; then

    echo "excellent"

    else

    echo "erro"

    fi

    需要指出,then或else后面不必是一条命令,可以是任意条命令,if和else带有的条件可以有很多;下面看几个例子:

    grep输出检查示例:

    #!/bin/sh

    if grep 'chinese' a.txt > /dev/null 2>&1

    then

    echo 'this file include thw word 'chinese''

    else

    echo 'this file doesn`t include the word 'chinese''

    fi

    文件拷贝示例:

    #!/bin/sh

    if cp a.txt b.txt

    then

    echo "copy the file a.txt to b.txt sucess"

    else

    echo "can`t copy a.txt to b.txt"

    fi

    当前目录测试:

    #!/bin/sh

    dir='pwd'

    if [ "$dir" != "/home/wangfnagyong" ]

    echo "current directpry isn`t wangfangyong`s home"

    else

    echo "current directory is wangfangyong`s home"

    fi

    6、case 语句

    举例说明:

    #!/bin/sh

    echo -n "do you want to continue this operator?" [n]

    read yesno

    case $yesno in

    Y | y | Yes | yes)

    echo "system will continue this operator"

    ;;

    n | N | no | NO)

    echo "system will skip this operator"

    ;;

    *)

    echo "incorrect input"

    exit 1

    ;;

    esac

  • 相关阅读:
    图论基础
    降维和聚类系列(二):拉普拉斯特征映射Laplacian Eigenmaps,谱聚类,实例代码
    降维和聚类系列(一):方法综述和比较(持续更新中)
    markdown设置图片尺寸
    指示向量indicator vector
    Sherlock and his girlfriend CodeForces
    The Meeting Place Cannot Be Changed CodeForces
    The Meeting Place Cannot Be Changed CodeForces
    数组分块入门 3
    数组分块入门 3
  • 原文地址:https://www.cnblogs.com/276815076/p/1841634.html
Copyright © 2011-2022 走看看