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

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

为什么要进行shell编程

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

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

建立一个脚本

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

如同其他语言一样,通过我们使用任意一种文字编辑器,比如neditkeditemacsvi等来编写我们的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

 注意:

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

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

 注意:

#! /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 !

实例 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值。
  • $*:传递给程序的所有参数组成的字符串。

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

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

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

 

12-4-1变量的赋值

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

变量名=

 注意:

等号(=)前后没有空格

例如:

   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

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

 注意:

输入路径时需“/

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

1vi编辑程序

[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`中的符号“`”为键盘左上角的“`”键。

 

                          12-5  表达式的比较

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

 

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

12-5-1字符串比较

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

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

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

       字符串操作符

          含义及返回值

=

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

 !=

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

-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

 注意:

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

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

1vi编辑程序

[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的长度大于零。

12-5-2数字比较

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

                         12-2  test比较的运算符

  运算符号

                      

-eq

           相等

      -ge

         大于等于

      -le

         小于等于

      -ne

         不等于

      -gt

         大于

      -lt

         小于

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

1vi编辑程序

[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两个逻辑值有一个为“是”,返回值就是“是”

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

1vi编辑程序

[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

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

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

1vi编辑程序

[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,输入一个字符串,如果是目录,则显示目录下的信息,如为文件显示文件的内容。

1vi编辑程序

[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

 注意:

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

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

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

1gedit编辑脚本程序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列表当中的所有文件,程序脚本如下。

1gedit编辑脚本程序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:求从1100的和。

 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`的加号两边的空格不能省,否则会成为字符串的连接。

12-6-2 while循环

   语法:

   while 表达式

     do

       操作

     done

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

实例 12-15:用while循环求1100的和。

   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

12-6-3 until循环

   语法:

until 表达式

do

操作

done

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

实例 12-16:用until循环求1100的和。

   1)用gedit编辑脚本程序test15

[root@localhost  bin]#gedit test15

total =0

num=0

   until [$sum –gt 100]

   do

       total=’expor $total +$ num’

       num=’expr $num + 1’

done

echo “The result is $total”

2)设置权限

[root@localhost  bin]#chmod +x test15

      (3)执行

[root@localhost  bin]#./ test15

The result is 5050

                               12-7  条件结构语句

  • Shell的条件结构语句

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

12-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循环求1100的和。

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

12-7-2  case语句

      语法:

case 表达式 in

1|2

操作;;

3|4

操作;;

5|6

操作;;

*

操作;;

esac

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

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

 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

 注意:

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

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

                             12-10本章小结

本章讲解了LinuxShell脚本的定义和相关Shell脚本编写的基础,这些基础知识是学习Shell脚本编程的关键。接着讲解了Shell 脚本的执行方式和Shell脚本的常见流程控制,为Shell脚本的编程做了准备。

课后习题

 

 

  1. 选择题
  2. 下列说法中正确的是( )。

A.安装软件包fctix-3.4.tar.bz2,要按顺序使用./configure;make;make install;tar命令

B.挂载U盘,mount /dev/sda  /mnt/u  -o  iocharset=gb2312

C.显示变量PS1的值用命令 echo  PS1    

D.用命令./abcsh abc执行Shell脚本abc,所得的结果并不相同

  1. 一个Bash Shell脚本的第一行是什么( )。

A. #!/bin/BashB. #/bin/BashC. #/bin/cshD. /bin/Bash

  1. Shell脚本中,用来读取文件内各个域的内容并将其赋值给Shell变量的命令是( )。

A. fold          B. join          C. tr             D. read

  1. 下列变量名中有效的Shell变量名是( )。

A. -2-time       B. _2$3        C. trust_no_1      D. 2004file

  1. 下列对Shell变量FRUIT操作,正确的是( )。
    A. 
    为变量赋值:$FRUIT=apple     B. 显示变量的值:fruit=apple
    C. 
    显示变量的值:echo $FRUIT    D. 判断变量是否有值:[ -f “$FRUIT” ]
  2. Fedora 12系统中,下列关于Shell脚本程序说法不正确的是( )。

A. Shell脚本程序以文本的形式存储
B. Shell
脚本程序在运行前需要进行编译
C. Shell
脚本程序由解释程序解释执行
D. Shell 
脚本程序主要用于系统管理和文件操作,它能够方便自如地处理大量重复
性的系统工作

  1. Shell编程中关于$2的描述正确的是( )。

A. 程序后携带了两个位置参数

B. 宏替换

C. 程序后面携带的第二个位置参数

携带位置参数的个数

$2引用第二个位置参数

  1. Fedora 12系统中,“run.sh”Shell执行脚本,在执行./run.sh file1 file2 file3的命令的过程中,变量$1的值为( )。

A. run.sh      B. file1      C. file2       D. file3

  1. 填空题
  2. Shell编程时,使用方括号表示测试条件的规则是            
  3. 编写的Shell程序运行前必须赋予该脚本文件         权限。
  4. 简答题
    1. Shell编程,判断一文件是不是字符设备文件,如果是将其拷贝到 /dev 目录下。
    2. 在根目录下有四个文件m1.txtm2.txtm3.txtm4.txt,用Shell编程,实现自动创建m1,m2,m3,m4四个目录,并将m1.txt,m2.txt,m3.txt,m4.txt四个文件分别拷贝到各自相应的目录下。
    3. 某系统管理员需每天做一定的重复工作,请按照下列要求,编制一个解决方案:
  • 在下午4 :50删除/abc目录下的全部子目录和全部文件;
  • 从早8:00~下午6:00每小时读取/xyz目录下x1文件中每行第一个域的全部数据加入到/backup目录下的bak01.txt文件内;
  • 每逢星期一下午5:50/data目录下的所有目录和文件归档并压缩为文件:backup.tar.gz
  • 在下午5:55IDE接口的CD-ROM卸载(假设:CD-ROM的设备名为hdc);
  • 在早晨8:00前开机后启动。
    1. 请用Shell编程来实现:当输入不同的选择时,执行不同的操作,如:输入start 开始启动应用程序myfiles,输入stop时,关闭myfiles,输入status时,查看myfiles进程,否则执行*)显示“EXIT!”并退出程序。
    2. 编写一个Shell程序,此程序的功能是:显示root下的文件信息,然后建立一个abc的文件夹,在此文件夹下建立一个文件k.c,修改此文件的权限为可执行。
    3. 编写一个Shell程序,挂载U盘,在U盘中根目录下所有.c文件拷贝到当前目录,然后卸载U盘。
    4. 编写一个Shell程序,程序执行时从键盘读入一个文件名,然后创建这个文件。
    5. 编写一个Shell程序,键盘输入两个字符串,比较两个字符串是否相等。
    6. 编写三个Shell程序,分别用forwhile、与until求从2+4+…+100的和。
    7. 编写一个Shell程序,键盘输入两个数及+-*、与/中的任一运算符,计算这两个数的运算结果。
    8. 编写两个Shell程序kkaa,在kk中输入两个数,调用aa计算计算这两个数之间奇数的和。
    9. 编写Shell程序,可以挂载U盘,也可挂载Windows硬盘的分区,并可对文件进行操作。
    10. 编写4个函数分别进行算术运算+-*/,并编写一个菜单,实现运算命令。

 

课程实训

实训内容:编写一个Shell程序,呈现一个菜单,有0-56个命令选项,1为挂载U盘,2为卸载U盘,3为显示U盘的信息,4把硬盘中的文件拷贝到U盘,5U盘中的文件拷贝到硬盘中,选0为退出。

程序分析:把此程序分成题目中要求的6大功能模块,另外加一个菜单显示及选择的主模板。

  1. 编辑代码

 [root@localhost  bin]#vi test19

#!/bin/sh

#mountusb.sh

#退出程序函数

quit()

{

  clear

  echo “*******************************************************************”

  echo  “***           thank you to use,Good bye!            ****”

  exit 0

  }

 #加载U盘函数

 mountusb()

 {

  clear

  #/mnt下创建usb目录

  mkdir /mnt/usb

  #查看U盘设备名称

  /sbin/fdisk –l |grep /dev/sd

  echo –e “Please Enter the device name of usb as shown above:\c”

read PARAMETER

mount /dev/$PARAMETER /mnt/usb

}

#卸载U盘函数

umountusb()

{

  clear

  ls -la /mnt/usb

}

#显示U盘信息函数

display()

{

  clear

  umount /mnt/usb

}

#拷贝硬盘文件到U盘函数

cpdisktousb()

{

   clear

   echo –e “Please Enter the filename to be Copide (under Current directory):\c”

   read FILE

   echo “Copying,please wait!...”

   cp $FILE /mnt/usb

}

#拷贝U盘函数到硬盘文件

cpusbtodisk()

{

  clear

  echo -e “Please Enter the filename to be Copide in USB:\c”

  read FILE

  echo “Copying ,Please wait!...”

  cp /mnt/usb/$FILE .  #点(.)表示当前路径

}

  clear

  while true

  do

echo “=====================================================================”

echo “***            LINUX USB MANAGE PROGRAM                     ***”

echo  “              1-MOUNT USB                                    ”

echo  “              2-UNMOUNT USB                                  ”

echo  “              3-DISPLAY USB INFORMATION                      ”

echo  “              4-COPY FILE IN DISK TO USB                     ”

echo  “              5-COPY FILE IN USB TO DISK                     ”

echo  “              0-EXIT                                         ”

echo “=====================================================================”

echo –e “Please Enter a Choice(0-5):\c”

read CHOICE

case $CHOICE in

  1. mountusb
  2. unmountusb
  3. display
  4. cpdisktousb
  5. cpusbtodisk
  6. quit

*)  echo “Invalid Choice!Corrent Choice is (0-5)”

    sleep 4

    clear;;

 esac

done

(2)修改权限

[root@localhost  bin]#chmod +x test19

(3)程序执行结果

[root@localhost  bin]#./ test19

项目实践

这段时间陈飞在学习Linux下的Shell 编程,感觉Shell 编程和C语言很相似。王工程师今天来看陈飞,顺便问一下陈飞的学习情况。陈飞就和他说了自己对Shell编程的看法。王工程师听了后,笑着说,“一样不一样,你编个程序不久明白了吗。”“那编什么程序呢”。陈飞问道。“就俄罗斯方块吧”,王工程师说。“俄罗斯方块大家都会玩,而且你可以在网上找到用C语言编写的程序,你用Shell编程实现,和C语言版的对比一下,不就明白了它们之间的不同了吗”。王工程师走了,留下了陷入沉思的陈飞。他能完成吗?

 

 

 

 

 





来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14293828/viewspace-1447570/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/14293828/viewspace-1447570/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值