您的位置:首页 > 运维架构 > Shell

shell--赋值,判断,循环

2016-06-10 10:45 295 查看
判断

比较两个字符串是否相等的办法是:

if [ "$test"x = "test"x ]; then

这里的关键有几点:

1 使用单个等号

2 注意到等号两边各有一个空格:这是unix shell的要求

3 注意到"$test"x最后的x,这是特意安排的,因为当$test为空的时候,上面的表达式就变成了x = testx,显然是不相等的。而如果没有这个x,表达式,就会报错:[: =: unary operator expected

if elseif else

if [ "xx" = "xx" ]; then

echo "if"

elif [ "xx" = "xx" ]; then

echo "elseif"

else

echo "else"

fi

#shell判断文件夹是否存在
-e filename 如果 filename存在,则为真
-d filename 如果 filename为目录,则为真
-f filename 如果 filename为常规文件,则为真
-L filename 如果 filename为符号链接,则为真
-r filename 如果 filename可读,则为真
-w filename 如果 filename可写,则为真
-x filename 如果 filename可执行,则为真
-s filename 如果文件长度不为0,则为真
-h filename 如果文件是软链接,则为真


#如果文件夹不存在,创建文件夹

if [ ! -d "/myfolder" ]; then

  mkdir /myfolder

fi

#shell判断文件,目录是否存在或者具有权限

folder="/var/www/"

file="/var/www/log"

# -x 参数判断 $folder 是否存在并且是否具有可执行权限

if [ ! -x "$folder"]; then

  mkdir "$folder"

fi

# -d 参数判断 $folder 是否存在

if [ ! -d "$folder"]; then

  mkdir "$folder"
fi

-e filename 如果 filename存在,则为真

-f filename 如果 filename为常规文件,则为真


# -f 参数判断 $file 是否存在

if [ ! -f "$file" ]; then

  touch "$file"

fi

# -n 判断一个变量是否有值

if [ ! -n "$var" ]; then

  echo "$var is empty"

  exit 0

fi

# 判断两个变量是否相等

if [ "$var1" = "$var2" ]; then

  echo '$var1 eq $var2'

else

  echo '$var1 not eq $var2'

fi

比较



-eq

     等于,如:if ["$a" -eq "$b" ]

-ne      不等于,如:if ["$a" -ne "$b" ]

-gt      大于,如:if ["$a" -gt "$b" ]

-ge      大于等于,如:if ["$a" -ge "$b" ]

-lt      小于,如:if ["$a" -lt "$b" ]

-le      小于等于,如:if ["$a" -le "$b" ]

<     小于(需要双括号),如:(("$a" < "$b"))

<=     小于等于(需要双括号),如:(("$a" <= "$b"))

>     大于(需要双括号),如:(("$a" > "$b"))

>=     大于等于(需要双括号),如:(("$a" >= "$b"))

字符串比较

=      等于,如:if ["$a" = "$b" ]

==      等于,如:if ["$a" == "$b" ],与=等价

     注意:==的功能在[[]]和[]中的行为是不同的,如下:

      1 [[ $a ==z* ]]    #如果$a以"z"开头(模式匹配)那么将为true

      2 [[ $a =="z*" ]] # 如果$a等于z*(字符匹配),那么结果为true

      3

      4 [ $a == z*]     # File globbing 和word splitting将会发生

      5 [ "$a" =="z*" ] # 如果$a等于z*(字符匹配),那么结果为true

      一点解释,关于Fileglobbing是一种关于文件的速记法,比如"*.c"就是,再如~也是.

      但是fileglobbing并不是严格的正则表达式,虽然绝大多数情况下结构比较像.

!=      不等于,如:if ["$a" != "$b" ]

     这个操作符将在[[]]结构中使用模式匹配.

<     小于,在ASCII字母顺序下.如:

      if [[ "$a"< "$b" ]]

      if [ "$a"\< "$b" ]

     注意:在[]结构中"<"需要被转义.

>     大于,在ASCII字母顺序下.如:

      if [[ "$a"> "$b" ]]

      if [ "$a"\> "$b" ]

     注意:在[]结构中">"需要被转义.

      具体参考Example26-11来查看这个操作符应用的例子.

-z     字符串为"null".就是长度为0.

-n     字符串不为"null"

      注意:

     使用-n在[]结构中测试必须要用""把变量引起来.使用一个未被""的字符串来使用! -z

     或者就是未用""引用的字符串本身,放到[]结构中。虽然一般情况下可

     以工作,但这是不安全的.习惯于使用""来测试字符串是一种好习惯.

循环

for

#!/usr/bin/ksh

#数字段形式

for i in {1..10}

do

   echo $i

done

#详细列出(字符且项数不多)

for File in 1 2 3 4 5

do

    echo $File

done

#对存在的文件进行循环

for shname in `ls *.sh`

do

          name=`echo "$shname" | awk -F. '{print $1}'`          

          echo $name

done

#查找循环(ls数据量太大的时候也可以用这种方法)

for shname in `find . -type f -name "*.sh"`

do

          name=`echo "$shname" | awk -F/ '{print $2}'`         

          echo $name

done

#((语法循环--有点像C语法,但记得双括号

for((i=1;i<100;i++))

do

    if((i%3==0))

    then

        echo $i

        continue

    fi

done

#seq形式 起始从1开始

for i in `seq 100`

do

    if((i%3==0))

    then

        echo $i

        continue

    fi

done

while

#while循环注意为方括号[],且注意空格

min=1

max=100

while [ $min -le $max ]

do

    echo $min

    min=`expr $min + 1`

done  

#双括号形式,内部结构有点像C的语法,注意赋值:i=$(($i+1))

i=1

while(($i<100))

do

    if(($i%4==0))

    then

        echo $i

    fi

    i=$(($i+1))

done

where  read的用法

1、准备数据文件

$cat a.txt

200:2

300:3

400:4

500:5

2、用while循环从文件中读取数据

#!/bin/ksh

while read line

do

    echo $line

done < a.txt

运行shell,结果如下:

200:2

300:3

400:4
500:5

Shell 函数的定义格式如下:

function_name () {

   list of commands

    [return value ]

}

如果你愿意,也可以在函数名前加上关键字 function:

function function_name () {

   list of commands

    [return value ]

}

带参数函数例子如下:

#!/bin/bash

funWithParam(){

   echo "The value of the first parameter is $1 !"

   echo "The value of the second parameter is $2 !"

   echo "The value of the tenth parameter is $10 !"

   echo "The value of the tenth parameter is ${10} !"

   echo "The value of the eleventh parameter is ${11} !"

   echo "The amount of the parameters is $# !"

   echo "The string of the parameters is $* !"

}

funWithParam 10  2 3 4 5 6 7 8 9 34 73

输出:

The value of the first parameter is 1 !

The value of the second parameter is 2 !

The value of the tenth parameter is 100 !

The value of the tenth parameter is 34 !

The value of the eleventh parameter is 73 !

The amount of the parameters is 12 !

The string of the parameters is 10 2 3 4 5 67 8 9 34 73 !"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: