您当前的位置: 首页 >  linux

phymat.nico

暂无认证

  • 5浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

linux Shell(脚本)编程入门实例讲解详解

phymat.nico 发布时间:2017-12-31 20:54:34 ,浏览量:5

linux Shell(脚本)编程入门实例讲解详解

为什么要进行shell编程

在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具。Shell不仅仅是命令的收集,而且是一门非常棒的编程语言。您可以通过使用shell使大量的任务自动化,shell特别擅长系统管理任务,尤其适合那些易用性、可维护性和便携性比效率更重要的任务。

下面,让我们一起来看看shell是如何工作的:

建立一个脚本

Linux中有好多中不同的shell,但是通常我们使用bash (bourne again shell) 进行shell编程,因为bash是免费的并且很容易使用。所以在本文中笔者所提供的脚本都是使用bash(但是在大多数情况下,这些脚本同样可以在bash的大姐,bourne shell中运行)。

如同其他语言一样,通过我们使用任意一种文字编辑器,比如nedit、kedit、emacs、vi等来编写我们的shell程序。程序必须以下面的行开始(必须方在文件的第一行):

#!/bin/sh

符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中我们使用/bin/sh来执行程序。当编辑好脚本时,如果要执行该脚本,还必须使其可执行。

要使脚本可执行:

chmod +x filename

然后,您可以通过输入: ./filename 来执行您的脚本。

注释

在进行shell编程时,以#开头的句子表示注释,直到这一行的结束。我们真诚地建议您在程序中使用注释。如果您使用了注释,那么即使相当长的时间内没有使用该脚本,您也能在很短的时间内明白该脚本的作用及工作原理。

变量

在其他编程语言中您必须使用变量。在shell编程中,所有的变量都由字符串组成,并且您不需要对变量进行声明。要赋值给一个变量,您可以这样写:

变量名=值

取出变量值可以加一个美元符号($)在变量前面:

#!/bin/sh

#对变量赋值:

a="hello world"

# 现在打印变量a的内容:

echo "A is:"

echo $a

在您的编辑器中输入以上内容,然后将其保存为一个文件first。之后执行chmod +x first。使其可执行,最后输入./first执行该脚本。

这个脚本将会输出:

A is:

hello world

有时候变量名很容易与其他文字混淆,比如:

num=2

echo "this is the $numnd"

这并不会打印出"this is the 2nd",而仅仅打印"this is the ",因为shell会去搜索变量numnd的值,但是这个变量时没有值的。可以使用花括号来告诉shell我们要打印的是num变量:

num=2

echo "this is the $nd"

这将打印: this is the 2nd

有许多变量是系统自动设定的,这将在后面使用这些变量时进行讨论。

如果您需要处理数学表达式,那么您需要使用诸如expr等程序(见下面)。除了一般的仅在程序内有效的shell变量以外,还有环境变量。由export关键字处理过的变量叫做环境变量。我们不对环境变量进行讨论,因为通常情况下仅仅在登录脚本中使用环境变量。

Shell命令和流程控制

在shell脚本中可以使用三类命令:

1)Unix 命令:

虽然在shell脚本中可以使用任意的unix命令,但是还是由一些相对更常用的命令。这些命令通常是用来进行文件和文字操作的。

常用命令语法及功能:

echo "some text": 将文字内容打印在屏幕上。

ls: 文件列表。

wc –l file wc -w file wc -c file: 计算文件行数 计算文件中的单词数 计算文件中的字符数。

cp sourcefile destfile: 文件拷贝。

mv oldname newname : 重命名文件或移动文件。

rm file: 删除文件。

grep 'pattern' file: 在文件内搜索字符串比如:grep 'searchstring' file.txt

cut -b colnum file: 指定欲显示的文件内容范围,并将它们输出到标准输出设备比如:输出每行第5个到第9个字符cut –b 5-9 file.txt千万不要和cat命令混淆,这是两个完全不同的命令。

cat file.txt: 输出文件内容到标准输出设备(屏幕)上。

file somefile: 得到文件类型。

read var: 提示用户输入,并将输入赋值给变量。

sort file.txt: 对file.txt文件中的行进行排序。

uniq: 删除文本文件中出现的行列比如: sort file.txt | uniq。

expr: 进行数学运算Example: add 2 and 3 expr 2 "+" 3。

find: 搜索文件比如:根据文件名搜索find . -name filename -print。

tee: 将数据输出到标准输出设备(屏幕) 和文件比如:somecommand | tee outfile。

basename file: 返回不包含路径的文件名比如: basename /bin/tux将返回 tux。

dirname file: 返回文件所在路径比如:dirname /bin/tux将返回 /bin。

head file: 打印文本文件开头几行。

tail file : 打印文本文件末尾几行。

sed: Sed是一个基本的查找替换程序。可以从标准输入(比如命令管道)读入文本,并将结果输出到标准输出(屏幕)。该命令采用正则表达式(见参考)进行搜索。不要和shell中的通配符相混淆。比如:将linuxfocus 替换为 LinuxFocus :cat text.file | sed 's/linuxfocus/LinuxFocus/' > newtext.file。

awk: awk 用来从文本文件中提取字段。缺省地,字段分割符是空格,可以使用-F指定其他分割符。cat file.txt | awk -F, '{print "," }'这里我们使用,作为字段分割符,同时打印第一个和第三个字段。如果该文件内容如下:

Adam Bor, 34, IndiaKerry Miller, 22, USA

命令输出结果为:

Adam Bor, IndiaKerry Miller.

2) 概念: 管道, 重定向和 backtick

这些不是系统命令,但是他们真的很重要。

管道 (|) 将一个命令的输出作为另外一个命令的输入。

grep "hello" file.txt | wc -l

在file.txt中搜索包含有”hello”的行并计算其行数。在这里grep命令的输出作为wc命令的输入。当然您可以使用多个命令。

重定向:将命令的结果输出到文件,而不是标准输出(屏幕)。

> 写入文件并覆盖旧文件。

>> 加到文件的尾部,保留旧文件内容。

反短斜线,使用反短斜线可以将一个命令的输出作为另外一个命令的一个命令行参数。

命令:

find . -mtime -1 -type f -print

用来查找过去24小时(-mtime –2则表示过去48小时)内修改过的文件。如果您想将所有查找到的文件打一个包,则可以使用以下脚本:

#!/bin/sh

# The ticks are backticks (`) not normal quotes ('):

tar -zcvf lastmod.tar.gz `find . -mtime -1 -type f -print`

3) 流程控制

"if" 表达式 如果条件为真则执行then后面的部分:

if ....; then

 

==========================================================================

shell脚本实例

#! /bin/sh echo "Current command is $0" echo "The first parameter is $1" echo "The second parameter is $2" echo "The third parameter is $3" echo "Total of parameters if $#" echo "Current PID is $$" #!/bin/bash times=0 until [ "$times" = 3 ]; do   echo "I love linux."   sleep 2   times=`expr $times + 1` done    #!/bin/bash # menu shell script.      samli     2004.4.19 until         echo "List Directory..........1"         echo "Change Directory........2"         echo "Edit File...............3"         echo "Remove File.............4"         echo "Exit Menu...............5"         read choice         test $choice = 5 do         case $choice in                1) ls;;                2) echo "enter target directory:"                read dir                cd $dir                ;;                3) echo "enter file name:"                read file                vi $file                ;;                4) echo "enter file name:"                read file                rm $file                ;;                5) echo "Goodbye"               ;;                *) echo "illegal option, please input again."         esac  done  #! /bin/sh var1="abcd  efg" echo $var1 var2=1234 echo "The value of var2 is $var2" echo  $HOME echo  $PATH echo  $PWD #! /bin/sh num=0 while [ $num -le 10 ] do     num=`expr $num + 1`        if [ $num -eq 5 ]        then               continue          fi     square=`expr $num \* $num`     echo $square done #!/bin/bash  # Gnu bash versions 2.x # The Party Program--Invitations to friends from the  # "guest" file  guestfile=./guests  #  ~/shell/guests if [[ ! -e "$guestfile" ]]  then        printf "${guestfile##*/} non-existent"        exit 1 fi export PLACE="Sarotini's" (( Time=$(date +%H) + 1 )) set cheese crackers shrimp drinks "hot dogs" sandwiches  for person in $(cat $guestfile)  do        if  [[ $person = root ]]        then               continue        else                # Start of here document               mail -v -s "Party" $person                Hi ${person}! Please join me at $PLACE for a party!                Meet me at $Time o'clock.               I'll bring the ice cream. Would you please bring $1                and anything else you would like to eat? Let me know                if you can't make it.                      Hope to see you soon.                            Your pal,                            ellie@$(hostname)               FINIS               shift                if (( $# ==  0 ))                then                      set cheese crackers shrimp drinks "hot dogs" sandwiches               fi         fi done               printf "Bye..."  #!/bin/sh  # Standard AT&T Bourne Shell  # The Party Program--Invitations to friends from the  # "guest" file  guestfile=./guests   # /home/ellie/shell/guests if [ ! -f "$guestfile" ]  then        echo "慴asename $guestfile?non-existent"        exit 1 fi PLACE="Sarotini's" export PLACE Time=`date +%H` Time=`expr $Time + 1` set cheese crackers shrimp drinks "hot dogs" sandwiches  for person in $(cat $guestfile)  do        if  [ $person = root ]]        then               continue        else                # Start of here document               mail -v -s "Party" $person                Hi $person! Please join me at $PLACE for a party!                Meet me at $Time o'clock.               I'll bring the ice cream. Would you please bring $1                and anything else you would like to eat? Let me know                if you can't                                make it.                      Hope to see you soon.                            Your pal,                            ellie@`hostname`               FINIS               shift                if [ $# -eq  0 ]                then                      set cheese crackers shrimp drinks "hot dogs" sandwiches               fi         fi done               echo "Bye..."  #!/bin/sh # Scriptname: args # Script to test command line arguments echo The name of this script is $0. echo The arguments are $*. echo The first argument is $1. echo The second argument is $2. echo The number of arguments is $#. oldargs=$* set Jake Nicky Scott                    # reset the positional parameters echo All the positional parameters are $*. echo The number of postional parameters is $#. echo "Good~Vbye for now, $1 " set $(date)                       #  reset the positional parameters echo The date is $2 $3, $6. echo "The value of \$oldargs is $oldargs." set $oldargs echo $1 $2 $3 # Name: bigfiles # Purpose: Use the find command to find any files in the root # partition that have not been modified within the past n (any # number within 30 days) days and are larger than 20 blocks # (512 byte blocks) if (( $# != 2 ))   #  or     [ $# -ne 2 ] then    echo  "Usage:   $0 mdays size " 1>&2    exit 1 fi if (( $1   0 || $1 > 30 )) #  or  [ $1 -lt 0 -o $1 -gt 30 ] then    echo "mdays is out of range"    exit 2 fi if (( $2    #   or  [ $2 -le 20 ] then    echo "size is out of range"    exit 3 fi find / -xdev -mtime $1 -size +$2 #!/bin/bash # Scriptname: checker # Script to demonstrate the use of special variable # modifiers and arguments name=${1:?"requires an argument" } echo Hello $name #!/bin/bash # This is the first Bash shell program of the day. # Scriptname: greetings # Written by:  Barbara Bashful echo "Hello $LOGNAME, it's nice talking to you." echo "Your present working directory is `pwd`." echo "You are working on a machine called `uname -n`." echo "Here is a list of your files." ls      # list files in the present working directory echo  "Bye for now $LOGNAME. The time is `date +%T`!" #!/bin/bash # Scriptname: greetings2 echo "This script is called $0." echo  "$0  $1 and $2" echo "The number of positional parameters is $#" #!/bin/bash # Scriptname: idcheck # purpose:check user id to see if user is root. # Only root has a uid of 0. # Format for id output:uid=9496(ellie) gid=40 groups=40 # root's uid=0 id=`id | gawk -F'[=(]'  '{print $2}'`     # get user id echo your user id is:  $id if  (( id == 0 ))    # or   [ $id -eq 0 ] then    echo "you are superuser." else    echo "you are not superuser." fi

================================================================================================

第12章 Shell脚本编程

任务要求

  • Shell命令行的运行
  • 编写、修改权限和执行Shell程序的步骤
  • 在Shell程序中使用参数和变量
  • 表达式比较、循环结构语句和条件结构语句
  • 在Shell程序中使用函数和调用其他Shell程序
12-1   Shell命令行书写规则

教学目录

  • 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

11 注意:

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

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

教学目录

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

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

12-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 !”

11 注意:

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

12-2-2建立可执行程序

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

[root@localhost  bin]#chmod +x date

12-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 !

16-27实例 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

                    12-3  在Shell程序中使用的参数

教学目录

  • 位置参数
  • 内部参数

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

12-3-1  位置参数

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

12-3-2  内部参数

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

  • $0:命令含命令所在的路径。
  • $#:传递给程序的总的参数数目。
  • $?:Shell程序在Shell中退出的情况,正常退出返回0,反之为非0值。
  • $*:传递给程序的所有参数组成的字符串。

16-27实例 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           //返回由参数组成的字符串

11 

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

16-27实例 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!

                12-4  在Shell程序中的使用变量

教学目录

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

 

12-4-1变量的赋值

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

变量名=值

11 注意:

等号(=)前后没有空格

例如:

   x=6

   a=”How are you ”

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

12-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

12-4-3键盘读入变量值

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

     read 变量

例如:

[root@localhost  bin]#read str

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

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

分析:

 存放目录的变量为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

11 注意:

输入路径时需“/”

16-27实例 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

11 注意:

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

 

                          12-5  表达式的比较

教学目录

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

 

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

12-5-1字符串比较

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

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

                         表12-1   常用的字符串操作符

       字符串操作符

          含义及返回值

=

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

 !=

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

-n

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

-z

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

16-27实例 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

11 注意:

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

16-27实例 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

11 注意:

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

12-5-2数字比较

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

                         表12-2  用test比较的运算符

  运算符号

          含            义

-eq

           相等

      -ge

         大于等于

      -le

         小于等于

      -ne

         不等于

      -gt

         大于

      -lt

         小于

16-27实例 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

12-5-3逻辑操作

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

                      12-3  Shell中的逻辑运算符

 运算符号

          含            义

   !

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

   -a

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

   -o

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

16-27实例 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

12-5-4文件操作

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

                       表12-4   文件测试操作符

   运算符号

          含            义

-d

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

-f

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

-L

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

-r

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

-s

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

-w

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

-x

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

16-27实例 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

11 注意:

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

16-27实例 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

                             12-6  循环结构语句

教学目录

  • Shell的循环语句

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

12-6-1  for循环

语法:

for 变量 in 列表

  do

    操作

  done

11 注意:

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

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

16-27实例 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

11 注意:

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

16-27实例 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!

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

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

[root@localhost  bin]#gedit test13

#! /bin/Bash

total =0

for((j=1;j

关注
打赏
1659628745
查看更多评论
立即登录/注册

微信扫码登录

0.1535s