shell基础

1. Shell命令行书写规则

Shell命令行基本功能的理解有助于编写更好的Shell程序,在执行Shell命令时多个命令可以在一个命令行上运行,但此时要使用分号(;)分隔命令,例如:

[root@localhost  root]# ls a* -l;free;df

Shell命令行可以使用反斜线字符()在命令行上扩充,例如:

[root@localhost  root]# echo “this is 

>long command”

This is long command



 注意: “>”符号是自动产生的,而不是输入的。

2. 编写/修改权限及执行Shell程序的步骤

  • 编写Shell程序
  • 执行Shell程序

  Shell程序有很多类似C语言和其他程序设计语言的特征,但是又没有程序语言那样复杂。Shell程序是指放在一个文件中的一系列Linux命令和实用程序。在执行的时候,通过Linux操作系统一个接一个地解释和执行每条命令。首先,来编写第一个Shell程序,从中学习Shell程序的编写、修改权限、执行过程。

2-1  编辑Shell程序

编辑一个内容如下的源程序,保存文件名为date,可将其存放在目录/bin下。

[root@localhost  bin]#vi date

#! /bin/sh

echo “Mr.$USER,Today is:”

echo &date “+%B%d%A”

echo “Wish you a lucky day !”



注意:#! /bin/sh通知采用Bash解释。如果在echo语句中执行Shell命令date,则需要在date命令前加符号“&”,其中%B%d%A为输入格式控制符。

2-2建立可执行程序

    编辑完该文件之后不能立即执行该文件,需给文件设置可执行程序权限。使用如下命令。

[root@localhost  bin]#chmod +x date

2-3  执行Shell程序

执行Shell程序有下面三种方法:

方法一:

[root@localhost  bin]#./ date

Mr.root,Today is:

二月 06 星期二

Wish you a lucky day !

方法二:

另一种执行date的方法就是把它作为一个参数传递给Shell命令:

[root@localhost  bin]# Bash date

Mr.root,Today is:

二月 06 星期二

Wish you a lucky day !

方法三:

 为了在任何目录都可以编译和执行Shell所编写的程序,即把/bin的这个目录添加到整个环境变量中。

具体操作如下:

[root@localhost  root]#export PATH=/bin:$PATH

[root@localhost  bin]# date

Mr.root,Today is:

二月 06 星期二

Wish you a lucky day !

实例 12-1:编写一个Shell程序mkf,此程序的功能是:显示root下的文件信息,然后建立一个kk的文件夹,在此文件夹下建立一个文件aa,修改此文件的权限为可执行。

分析:此Shell程序中需要依次执行下列命令为:

进入root目录:cd /root

显示root目录下的文件信息:ls –l

新建文件夹kk: mkdir kk

进入root/kk目录:cd kk

新建一个文件aa:  vi aa  #编辑完成后需手工保存

修改aa文件的权限为可执行:chmod +x aa

回到root目录:cd /root

因此该Shell程序只是以上命令的顺序集合,假定程序名为mkf

[root@localhost  root]#vi mkf

cd /root

ls –l

mkdir kk

cd kk

vi aa

chmod +x aa

cd /root

 

3.Shell程序中使用的参数

  • 位置参数
  • 内部参数

  如同ls命令可以接受目录等作为它的参数一样,在Shell编程时同样可以使用参数。Shell程序中的参数分为位置参数和内部参数等。

3-1  位置参数

  由系统提供的参数称为位置参数。位置参数的值可以用$N得到,N是一个数字,如果为1,即$1。类似C语言中的数组,Linux会把输入的命令字符串分段并给每段进行标号,标号从0开始。0号为程序名字,从1开始就表示传递给程序的参数。如$0表示程序的名字,$1表示传递给程序的第一个参数,以此类推

3-2  内部参数

  上述过程中的$0是一个内部变量,它是必须的,而$1则可有可无,最常用的内部变量有$0、$#、$?、$*,它们的含义如下。

 
变量含义
$0 当前脚本的文件名
$n 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2。
$# 传递给脚本或函数的参数个数。
$* 传递给脚本或函数的所有参数组成的字符串。
$@ 传递给脚本或函数的所有参数。被双引号(" ")包含时,与 $* 稍有不同,下面将会讲到。
$? 上个命令的退出状态,或函数的返回值。正常退出返回0,反之为非0值
$$ 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID。

实例 12-2:编写一个Shell程序,用于描述Shell程序中的位置参数为:$0、$#、$?、$*,程序名为test1,代码如下:

[root@localhost  bin]#vi test1

#! /bin/sh

echo “Program name is $0”;

echo “There are totally $# parameters passed to this program”;

echo “The last is $?”;

echo “The parameter are $*”;

执行后的结果如下:

[root@localhost  bin]# test1 this is a test program  //传递5个参数

Program name is /bin/test1                       //给出程序的完整路径和名字

There are totally 5 parameters passed to this program  //参数的总数

The last is 0                                    //程序执行效果

The parameters are this is a test program           //返回由参数组成的字符串

注意:命令不计算在参数内。

实例 12-3:利用内部变量和位置参数编写一个名为test2的简单删除程序,如删除的文件名为a,则在终端中输入的命令为:test a

分析:除命令外至少还有一个位置参数,即$#不能为0,删除不能为$1,程序设计过程如下。

  1. vi编辑程序
[root@localhost  bin]#vi test2

#! /bin/sh

if test $# -eq 0

then

echo “Please specify a file!else

 gzip $1                         //现对文件进行压缩

mv $1.gz $HOME/dustbin           //移动到回收站

echo “File $1 is deleted !fi
  1. 设置权限
[root@localhost  bin]#chmod +x test2

(3)  运行

[root@localhost  bin]# test2 a (如果a文件在bin目录下存在)

File a is deleted!

4. Shell程序中的使用变量

  • 变量的赋值
  • 变量的访问
  • 变量的输入

 

4-1变量的赋值

   Shell编程中,所有的变量名都由字符串组成,并且不需要对变量进行声明。要赋值给一个变量,其格式如下:

变量名=值

 注意:

           等号(=)前后没有空格

例如:

 x=6

 a=”How are you ”

 #表示把6赋值给变量x,字符串“How are you ”赋值给变量a。

4-2  访问变量值

如果要访问变量值,可以在变量前面加一个美元符号“$”,例如:

 [root@localhost  bin]#a=”How are you ”

 [root@localhost  bin]#echo “He juest said:$a”

    A is:hello world

一个变量给另一个变量赋值可以写成:

  变量2=$变量1

例如:

x=$i

i++可以写成:

i=$i+1

4-3键盘读入变量值

   Shell程序设计中,变量的值可以作为字符串从键盘读入,其格式为:

       read 变量

例如:

[root@localhost  bin]#read str

read为读入命令,它表示从键盘读入字符串到str。

实例 12-4:编写一个Shell程序test3,程序执行时从键盘读入一个目录名,然后显示这个目录下所有文件的信息。

(1)分析

存放目录的变量为DIRECTORY,其读入语句为:

read DIRECTORY

显示文件的信息命令为:ls –a

 [root@localhost  bin]#vi test3

#! /bin/sh

echo “please input name of directory”

read DIRECTORY

cd $DIRECTORY

ls –l

(2)设置权限

[root@localhost  bin]#chmod +x test3

(3)执行

[root@localhost  bin]#./test3

 注意:输入路径时需“/”

实例 12-5:运行程序test4,从键盘读入x、y的值,然后做加法运算,最后输出结果。

1)用vi编辑程序

[root@localhost  bin]#vi test4

 #! /bin/sh

echo “please input x y”

read x,y

z=`expr $x+$y`

echo “The sum is $z”

2)设置权限

[root@localhost  bin]#chmod +x test4

(3)执行

[root@localhost  bin]#./ test4

45 78

The sum is 123

 注意: 表达式total=`expr $total +$num`及num=`expr $num +1`中的符号“`”为键盘左上角的“`”键。

 

5. 表达式的比较

  • 字符串操作符
  • 逻辑运算符
  • test比较的运算符
  • 数字比较符
  • 文件操作符

  在Shell程序中,通常使用表达式比较来完成逻辑任务。表达式所代表的操作符有字符操作符、数字操作符、逻辑操作符、以及文件操作符。其中文件操作符是一种Shell所独特的操作符。因为Shell里的变量都是字符串,为了达到对文件进行操作的目的,于是才提供了文件操作符。

5-1字符串比较

   作用:测试字符串是否相等、长度是否为零,字符串是否为NULL。

   常用的字符串操作符如表所示.。

       字符串操作符

          含义及返回值

=

 比较两个字符串是否相同,相同则为“真”

 !=

 比较两个字符串是否不相同,不同则为“真”

-n

比较两个字符串长度是否大于零,若大于零则为“真”

-z

比较两个字符串长度是否等于零,若等于零则为“真”

实例 12-6:从键盘输入两个字符串,判断这两个字符串是否相等,如相等输出。

1)用vi编辑程序

[root@localhost  bin]#vi test5

#! /bin/Bash

read ar1

read ar2

[ “$ar1” = “$ar2” ]

echo $? #?保存前一个命令的返回码

2)设置权限

[root@localhost  bin]#chmod +x test5

(3)执行

[root@localhost  root]#./ test5

aaa

bbb

1

 注意:  ”[”后面和”]”前面及等号“=“的前后都应有一个空格;注意这里是程序的退出情况,如果ar1和ar2的字符串是不想等的非正常退出,输出结果为1。

实例 12-7: 比较字符串长度是否大于零

1)用vi编辑程序

[root@localhost  bin]#vi test6

#! /bin/Bash

read ar

 [  -n “$ar” ]

echo $?   //保存前一个命令的返回码

2)设置权限

[root@localhost  bin]#chmod +x test6

3)执行

[root@localhost  bin]#./ test6

0

 注意: 运行结果1表示ar的小于等于零,0表示ar的长度大于零。

5-2数字比较

   在Bash Shell编程中的关系运算有别于其他编程语言,用表中的运算符用test语句表示大小的比较。                

  运算符号

          含            义

-eq

           相等

      -ge

         大于等于

      -le

         小于等于

      -ne

         不等于

      -gt

         大于

      -lt

         小于

实例 12-8:比较两个数字是否相等

1)用vi编辑程序

[root@localhost  bin]#vi test7

  #! /bin/Bash

read x,y

if test $x –eq $y

   then

     echo “$x=$y”

else

     echo “$x!=$y”

fi

2)设置权限

[root@localhost  bin]#chmod +x test7

3)执行

[root@localhost  bin]#./ test7

50 100

50!=100

[root@localhost  bin]#./ test7

  150 150

  150= =150

5-3逻辑操作

    在Shell程序设计中的逻辑运算符如表所示。              

 运算符号

          含            义

   !

 反:与一个逻辑值相反的逻辑值

   -a

 与(and):两个逻辑值为“是”返回值为“是”,反之为“否”

   -o

或(or): 两个逻辑值有一个为“是”,返回值就是“是”

实例 12-9:分别给两个字符变量赋值,一个变量赋予一定的值,另一个变量为空,求两者的与、或操作。

1)用vi编辑程序

[root@localhost  bin]#vi test8

  #! /bin/Bash

  part1 =”1111”

  part2 =” ”     #part2为空

  [ “$ part1” –a “$ part2”]

  echo $?       #保存前一个命令的返回码

  [ “$ part1” –o “$ part2”]

  echo $? 

2)设置权限

[root@localhost  bin]#chmod +x test8

3)执行

[root@localhost  bin]#./ test8

1

0

5-4文件操作

  文件测试操作表达式通常是为了测试文件的信息,一般由脚本来决定文件是否应该备份、复制或删除。由于test关于文件的操作符有很多,在表中只列举一些常用的操作符。 

   运算符号

          含            义

-d

 对象存在且为目录返回值为“是”

-f

 对象存在且为文件返回值为“是”

-L

 对象存在且为符号连接返回值为“是”

-r

 对象存在且可读则返回值为“是”

-s

 对象存在且长度非零则返回值为“是”

-w

 对象存在且且可写则返回值为“是”

-x

 对象存在且且可执行则返回值为“是”

实例 12-10:判断zb目录是否存在于/root下。

1)用vi编辑程序

[root@localhost  bin]#vi test9

#! /bin/Bash

[ -d /root/zb ]

echo $?    #保存前一个命令的返回码

2)设置权限

[root@localhost  bin]#chmod +x test9

 (3)执行

[root@localhost  bint]#./ test9

(4)在/root添加zb目录

[root@localhost  bin]#mkdir zb

(5)执行

[root@localhost  bin]#./test9

0

 注意:运行结果是返回参数“$?”,结果1表示判断的目录不存在,0表示判断的目录不存在。

实例 12-11:编写一个Shell程序test10,输入一个字符串,如果是目录,则显示目录下的信息,如为文件显示文件的内容。

1)用vi编辑程序

[root@localhost  bin]#vi test10

#! /bin/Bash

  echo “Please enter the directory name or file name”

  read DORF

  if [ -d $DORF ]

  then

ls $DORF

  elif [ -f $DORF ]

then

cat $DORF

else

  echo “input error!fi

2)设置权限

[root@localhost  bin]#chmod +x test10

3)执行

[root@localhost  bin]#./ test10

6. 循环结构语句

  • Shell的循环语句

  Shell常见的循环语句有for循环、while循环语句和until循环。

6-1  for循环

语法:

for 变量 in 列表

  do

    操作

  done

 注意:  变量要在循环内部用来指列表当中的对象。

    列表是在for循环的内部要操作的对象,可以是字符串也可以是文件,如果是文件则为文件名。

实例 12-12:在列表中的值:a,b,c,e,I,2,4,6,8用循环的方式把字符与数字分成两行输出。

1)用gedit编辑脚本程序test11

[root@localhost  bin]#gedit test11

#! /bin/Bash

for i in a,b,c,e,I  2,4,6,8

do

echo $i

done

2)设置权限

[root@localhost  bin]#chmod +x test11

 (3)执行

[root@localhost  bin]#./ test11

a,b,c,e,i

2,4,6,8

注意:在循环列表中的空格可表示换行。

实例 12-13:删除垃圾箱中的所有文件。

     分析:在本机中,垃圾箱的位置是在$HOME/.Trash中,因而是在删除$HOME/.Trash列表当中的所有文件,程序脚本如下。

1)用gedit编辑脚本程序test12

[root@localhost  bin]#gedit test12

#! /bin/Bash

for i in $HOME/.Trash/*

do

    rm $ i

echo “$ i has been deleted!”

done

2)设置权限

[root@localhost  bin]#chmod +x test12

3)执行

[root@localhost  bin]#./ test12

/root/.Trash/abc~ has been deleted!

/root/.Trash/abc1 has been deleted!

实例 12-14:求从1~100的和。

1)用gedit编辑脚本程序test13

[root@localhost  bin]#gedit test13

#! /bin/Bash

total =0

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

do

     total=`expr $total + $j`

done

echo “The result is $total”

2)设置权限

[root@localhost  bin]#chmod +x test13

3)执行

[root@localhost  bin]#./ test13

The result is 5050

 注意for语句中的双括号不能省,最后的分号可有可无,表达式total=`expr $total + $j`的加号两边的空格不能省,否则会成为字符串的连接。

 

 

6-2 while循环

语法:

 while 表达式

     do

       操作

     done

只要表达式为真,do和done之间的操作就一直会进行。

实例 12-15:用while循环求1~100的和。

 (1)用gedit编辑脚本程序test14

[root@localhost  bin]#gedit test13

total =0

num=0

   while((num<=100));

do

      total=’expor $total +$ num’

done

echo “The result is $total”

2)设置权限

[root@localhost  bin]#chmod +x test14

 (3)执行

[root@localhost  bin]#./ test14

The result is 5050

6-3 until循环

   语法:

until 表达式

do

操作

done

重复do和done之间的操作直到表达式成立为止。

实例 12-16:用until循环求1~100的和。

1)用gedit编辑脚本程序test15

[root@localhost  bin]#gedit test15

total =0

num=0

   until [$sum –gt 100]

   do

       total=’expor $total +$ num’

       num=’expr $num + 1done

echo “The result is $total”

2)设置权限

[root@localhost  bin]#chmod +x test15

  (3)执行

[root@localhost  bin]#./ test15

The result is 5050

7  条件结构语句

  • Shell的条件结构语句

  Shell程序中的条件语句主要有if语句与case语句。

7-1  if语句

语法:

if 表达式1  then

操作

elif表达式2  then

操作

elif表达式3  then

操作

……

else

操作

fi

Linux里的if的结束标志是将if反过来写成fi;而elif其实是else if的缩写。其中,elif理论上可以有无限多个。

实例 12-17:用for循环求1~100的和。

1)用gedit编辑脚本程序test16

[root@localhost  bin]#gedit test16

for((j=0;j<=10;j++))

  do

       if(($j%2==1))

       then

         echo “$j”

fi

done

2)设置权限

[root@localhost  bin]#chmod +x test16

3)执行

[root@localhost  bin]#./ test16

13579

 

7-2  case语句

 语法:

case 表达式 in

值1|值2)

操作;;

值3|值4)

操作;;

值5|值6)

操作;;

*)

操作;;

esac

    case的作用就是当字符串与某个值相同是就执行那个值后面的操作。如果同一个操作对于多个值,则使用“|”将各个值分开。在case的每一个操作的最后面都有两个“;;”分号是必需的。

实例 12-18:Linux是一个多用户操作系统,编写一程序根据不同的用户登录输出不同的反馈结果。

 (1)用vi编辑脚本程序test17

[root@localhost  bin]#gedit test17

  #!/bin/sh

  case $USER in

beechen)

 echo “You are beichen!”;;

liangnian)

    echo “You are liangnian”;        //注意这里只有一个分号

  echo “Welcome !”;;            //这里才是两个分号

 root)

  echo “You are root!”;echo “Welcome !”;;

    //将两命令写在一行,用一个分号作为分隔符

*echo “Who are you?$USER?”;;

easc

2)设置权限

[root@localhost  bin]#chmod +x test17

3)执行

[root@localhost  bin]#./ test17

You are root

Welcome!

12-8  Shell脚本中使用函数

   

  • Shell的函数

  Shell程序也支持函数。函数能完成一特定的功能,可以重复调用这个函数。

     函数格式如下:

函数名( )

 {

函数体

  }

函数调用方式为

函数名  参数列表

实例 12-19:编写一函数add求两个数的和,这两个数用位置参数传入,最后输出结果。

1)编辑代码

[root@localhost  bin]#gedit test18

#!/bin/sh

add()

{

a=$1

b=$2

z=’expr $a + $b’

echo “The sum is $z”

}

add $1 $2

2)设置权限

[root@localhost  bin]#chmod +x test18

  (3)执行

[root@localhost  bin]#./ test18  10  20

The sum is 30

 注意:函数定义完成后必须同时写出函数的调用,然后对此文件进行权限设定,在执行此文件。

9.  Shell脚本中调用其他脚本

   

  • Shell脚本的调用

    在Shell脚本的执行过程中,Shell脚本支持调用另一个Shell脚本,调用的格式为:

         程序名

实例 12-20:在Shell脚本test19中调用test20。

1)调用test20

#test19脚本

#!/bin/sh

echo “The main name is $0”

./test20

echo “The first string is $1”

#test20脚本

#!/bin/sh

echo “How are you $USER?”

2)设置权限

[root@localhost  bin]#chmod +x test19

[root@localhost  bin]#chmod +x test20

3)执行

[root@localhost  bin]#./ test19 abc123

The main name is ./test19

How are you root?

the first string is abc123

 注意:

1)在Linux编辑中命令区分大小写字符。

2)在Shell语句中加入必要的注释,以便以后查询和维护,注释以#开头。

3)对Shell变量进行数字运算时,使用乘法符号“*”时,要用转义字符“”进行转义。

4)由于Shell对命令中多余的空格不进行任何处理,因此程序员可以利用这一特性调整程序缩进,达到增强程序可读性效果。

5)在对函数命名时最好能使用有含义且能容易理解的名字,即使函数名能够比较准确地表达函数所完成的任务。同时建议对于较大的程序要建立函数名和变量命名对照表。

              

原文地址:https://www.cnblogs.com/zxqstrong/p/5000467.html