linux shell编程摘要

2007-6-28

       要创建脚本程序,首先用任意文本编辑器书写脚本程序。确定程序第一行以类似#!/bin/sh开头,表示用/bin/sh程序来执行脚本。当然也可以写#/bin/bash。然后要更改脚本程序文件的属性,使其成为可执行。这可以通过 chmod +x shelllFile来实现。

      

       关于变量:

In Linux (Shell), there are two types of variable:

(1) System variables - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS.

(2) User defined variables (UDV) - Created and maintained by user. This type of variable defined in lower letters.

      

定义用户自己的变量:To define UDV use following syntax
Syntax:  variable name=value
例如:var=11或者var=”Kevin”

 

变量注意事项:

Don't put spaces on either side of the equal sign when assigning value to variable.

You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition) For e.g.

$ vech=

$ vech=""

 

To print or access UDV use following syntax
Syntax:
$variablename

  例如要打印一个变量的值,可以使用 echo $var,但是要读入一个变量值,则为read var

 

       关于if…else..流程控制形式为:

       if exp ; then

       fi

    或者

    if exp ; then

    else

    fi

    或者

    if exp ; then

    elif  ; then

    fi

       注意:then前要加分号;exp中如果是一个表达式,例如判断一个变量是否为字符串123,则为:

       if [ “$var” = “ 123” ] ; then

    echo “var = 123”

    fi

       注意[]中的空格,即[与变量之间的空格,以及等号两侧的空格。判断一个变量还要使用引号将其包含起来。

 

       关于for循环流程控制形式为:

       for var in 1 2 3 4 3 2 1

    do   

echo $var

    done

       这样,var会从in后面的列表中依次得到一个值,即依次为1 2 3 4 3 2 1,循环的次数为这个列表的长度。

       另一种控制为:

       for (( var = 1; var <= 5; ++ var ))

    do

    echo $var

    done

       形式大致跟C语言相似,此处对空格的要求也不是很苛刻。但是注意for中的表达式两边一定要加两个括号。

 

       关于while循环流程控制形式为:(until)

       while [ condition ]

    do

    do something

    done

    其中条件判断和if 中的规则一样,例如:

       var=”Kevin”

    while [ “$var” = ”Kevin” ]

    do

    read var

    done

   

       关于case形式的控制:

       case $var in

    “Kevin”) echo “var=Kevin” ;;

    “lynx”) echo “var=lynx” ;;

       esac

       注意每个语句块执行完后要添加两个分号。case语句块全部结束后以 esac 作为结束关键字。

 

       使用expr来计算算术表达式:(以及let

       例如:c=1+2,就需要这样写:c=`expr 1 + 2` 注意表达式两侧的 ` 符号,是TAB键上的键盘符号。如下代码:

       a=1

    b=2

    c=`expr $a + $b`

    echo $c

       即能让c=a+b

Examples:
$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 /* 3      #乘法
$ echo `expr 6 + 3`

       注意算术符号两侧一定要加空格。expr可以计算2个以上操作数的表达式。

 

       引号的种类:有三种引号:

1.  双引号,所有符号被该引号扩起来后就变为普通字符串,除了 / $ 符号。即使在双引号内使用$var也能将var变量的值输出来。

2.  单引号,

3.  back quote,即TAB键上的那个符号,一般用于执行命令,例如:

echo “Today is `date` “ 即可调用date来获得系统时间。

 

可以多条语句放在同一行中,只需要使用分号隔开即可。

      

       test 语句和 [] 一样可以测试表达式。

      

       一些测试符号:

      

Mathematical Operator in  Shell Script 

Meaning

Normal Arithmetical/ Mathematical Statements

But in Shell

 

 

 

For test statement with if command

For [ expr ] statement with if command

-eq

is equal to

5 == 6

if test 5 -eq 6

if [ 5 -eq 6 ]

-ne

is not equal to

5 != 6

if test 5 -ne 6

if [ 5 -ne 6 ]

-lt

is less than

5 < 6

if test 5 -lt 6

if [ 5 -lt 6 ]

-le

is less than or equal to

5 <= 6

if test 5 -le 6

if [ 5 -le 6 ]

-gt

is greater than

5 > 6

if test 5 -gt 6

if [ 5 -gt 6 ]

-ge

is greater than or equal to

5 >= 6

if test 5 -ge 6

if [ 5 -ge 6 ]

 

 

 

       要注意的是,当一个表达式为真时,返回的是0值,非0值表示假。

         对于字符串,则使用其他的比较符号,如下表:

Operator

Meaning

string1 = string2

string1 is equal to string2

string1 != string2

string1 is NOT equal to string2

string1

string1 is NOT NULL or not defined 

-n string1

string1 is NOT NULL and does exist

-z string1

string1 is NULL and does exist

 

       定义函数,使用关键字 function,例如:

       function print_str()

{

    echo $1

}

其中,function关键字也可省略。调用函数时只需要 函数名 参数1 参数2 。如果不传参数就直接给函数名。函数内部要读取参数,可以通过 $1, $2, $3 之类的来读取,分别表示第一个参数,第二个参数,第三个参数。其中,函数中还有一些特殊参数,例如 $0 表示函数名(对于一个shell文件来说是该文件名),$#表示参数个数。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值