| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > SHELL

Shell编程基础

时间:2006-07-16 21:29:14  来源:Linux联盟收集  作者:
  breakZ3yLinux联盟
doneZ3yLinux联盟
echo "You have selected $var"Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  下面是该脚本运行的结果: Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
What is your favourite OS?Z3yLinux联盟
1) LinuxZ3yLinux联盟
2) Gnu HurdZ3yLinux联盟
3) Free BSDZ3yLinux联盟
4) OtherZ3yLinux联盟
#? 1Z3yLinux联盟
You have selected LinuxZ3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  您也可以在shell中使用如下的loop表达式:Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
while ...; doZ3yLinux联盟
....Z3yLinux联盟
doneZ3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  while-loop 将运行直到表达式测试为真。will run while the expression that we test for is true. 关键字"break" 用来跳出循环。而关键字”continue”用来不执行余下的部分而直接跳到下一个循环。Z3yLinux联盟
Z3yLinux联盟
  for-loop表达式查看一个字符串列表 (字符串用空格分隔) 然后将其赋给一个变量: Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
for var in ....; doZ3yLinux联盟
 ....Z3yLinux联盟
doneZ3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  在下面的例子中,将分别打印ABC到屏幕上: Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
#!/bin/shZ3yLinux联盟
for var in A B C ; doZ3yLinux联盟
 echo "var is $var"Z3yLinux联盟
doneZ3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  下面是一个更为有用的脚本showrpm,其功能是打印一些RPM包的统计信息:Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
#!/bin/shZ3yLinux联盟
# list a content summary of a number of RPM packagesZ3yLinux联盟
# USAGE: showrpm rpmfile1 rpmfile2 ...Z3yLinux联盟
# EXAMPLE: showrpm /cdrom/RedHat/RPMS/*.rpmZ3yLinux联盟
for rpmpackage in $*; doZ3yLinux联盟
 if [ -r "$rpmpackage" ];thenZ3yLinux联盟
  echo "=============== $rpmpackage =============="Z3yLinux联盟
  rpm -qi -p $rpmpackageZ3yLinux联盟
 elseZ3yLinux联盟
  echo "ERROR: cannot read file $rpmpackage"Z3yLinux联盟
 fiZ3yLinux联盟
doneZ3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  这里出现了第二个特殊的变量$*,该变量包含了所有输入的命令行参数值。如果您运行showrpm openssh.rpm w3m.rpm webgrep.rpm Z3yLinux联盟
Z3yLinux联盟
  此时 $* 包含了 3 个字符串,即openssh.rpm, w3m.rpm and webgrep.rpm. Z3yLinux联盟
Z3yLinux联盟
引号Z3yLinux联盟
Z3yLinux联盟
  在向程序传递任何参数之前,程序会扩展通配符和变量。这里所谓扩展的意思是程序会把通配符(比如*)替换成合适的文件名,它变量替换成变量值。为了防止程序作这种替换,您可以使用引号:让我们来看一个例子,假设在当前目录下有一些文件,两个jpg文件, mail.jpg 和tux.jpg。Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
#!/bin/shZ3yLinux联盟
echo *.jpgZ3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  这将打印出"mail.jpg tux.jpg"的结果。Z3yLinux联盟
Z3yLinux联盟
  引号 (单引号和双引号) 将防止这种通配符扩展: Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
#!/bin/shZ3yLinux联盟
echo "*.jpg"Z3yLinux联盟
echo '*.jpg'Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  这将打印"*.jpg" 两次。 Z3yLinux联盟
Z3yLinux联盟
  单引号更严格一些。它可以防止任何变量扩展。双引号可以防止通配符扩展但允许变量扩展。 Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
#!/bin/shZ3yLinux联盟
echo $SHELLZ3yLinux联盟
echo "$SHELL"Z3yLinux联盟
echo '$SHELL'Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  运行结果为: Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
/bin/bashZ3yLinux联盟
/bin/bashZ3yLinux联盟
$SHELLZ3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  最后,还有一种防止这种扩展的方法,那就是使用转义字符——反斜杆: Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
echo *.jpgZ3yLinux联盟
echo $SHELLZ3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  这将输出:Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
*.jpgZ3yLinux联盟
$SHELLZ3yLinux联盟
Here documentsZ3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  当要将几行文字传递给一个命令时,here documents(译者注:目前还没有见到过对该词适合的翻译)一种不错的方法。对每个脚本写一段帮助性的文字是很有用的,此时如果我们四有那个here documents就不必用echo函数一行行输出。 一个 "Here document" 以 << 开头,后面接上一个字符串,这个字符串还必须出现在here document的末尾。下面是一个例子,在该例子中,我们对多个文件进行重命名,并且使用here documents打印帮助: Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
#!/bin/shZ3yLinux联盟
# we have less than 3 arguments. Print the help text:Z3yLinux联盟
if [ $# -lt 3 ] ; thenZ3yLinux联盟
cat < ren -- renames a number of files using sed regular expressionsZ3yLinux联盟
Z3yLinux联盟
USAGE: ren 'regexp' 'replacement' files...Z3yLinux联盟
Z3yLinux联盟
EXAMPLE: rename all *.HTM files in *.html:Z3yLinux联盟
 ren 'HTM$' 'html' *.HTMZ3yLinux联盟
Z3yLinux联盟
HELPZ3yLinux联盟
 exit 0Z3yLinux联盟
fiZ3yLinux联盟
OLD="$1"Z3yLinux联盟
NEW="$2"Z3yLinux联盟
# The shift command removes one argument from the list ofZ3yLinux联盟
# command line arguments.Z3yLinux联盟
shiftZ3yLinux联盟
shiftZ3yLinux联盟
# $* contains now all the files:Z3yLinux联盟
for file in $*; doZ3yLinux联盟
  if [ -f "$file" ] ; thenZ3yLinux联盟
   newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`Z3yLinux联盟
   if [ -f "$newfile" ]; thenZ3yLinux联盟
    echo "ERROR: $newfile exists already"Z3yLinux联盟
   elseZ3yLinux联盟
    echo "renaming $file to $newfile ..."Z3yLinux联盟
    mv "$file" "$newfile"Z3yLinux联盟
   fiZ3yLinux联盟
  fiZ3yLinux联盟
doneZ3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  这是一个复杂一些的例子。让我们详细讨论一下。第一个if表达式判断输入命令行参数是否小于3个 (特殊变量$# 表示包含参数的个数) 。如果输入参数小于3个,则将帮助文字传递给cat命令,然后由cat命令将其打印在屏幕上。打印帮助文字后程序退出。 如果输入参数等于或大于3个,我们就将第一个参数赋值给变量OLD,第二个参数赋值给变量NEW。下一步,我们使用shift命令将第一个和第二个参数从参数列表中删除,这样原来的第三个参数就成为参数列表$*的第一个参数。然后我们开始循环,命令行参数列表被一个接一个地被赋值给变量$file。接着我们判断该文件是否存在,如果存在则通过sed命令搜索和替换来产生新的文件名。然后将反短斜线内命令结果赋值给newfile。这样我们就达到了我们的目的:得到了旧文件名和新文件名。然后使用mv命令进行重命名。Z3yLinux联盟
Z3yLinux联盟
函数Z3yLinux联盟
Z3yLinux联盟
  如果您写了一些稍微复杂一些的程序,您就会发现在程序中可能在几个地方使用了相同的代码,并且您也会发现,如果我们使用了函数,会方便很多。一个函数是这个样子的: Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
functionname()Z3yLinux联盟
{Z3yLinux联盟
# inside the body $1 is the first argument given to the functionZ3yLinux联盟
# $2 the second ...Z3yLinux联盟
bodyZ3yLinux联盟
}Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  您需要在每个程序的开始对函数进行声明。Z3yLinux联盟
Z3yLinux联盟
  下面是一个叫做xtitlebar的脚本,使用这个脚本您可以改变终端窗口的名称。这里使用了一个叫做help的函数。正如您可以看到的那样,这个定义的函数被使用了两次。 Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
#!/bin/shZ3yLinux联盟
# vim: set sw=4 ts=4 et:Z3yLinux联盟
Z3yLinux联盟
help()Z3yLinux联盟
{Z3yLinux联盟
  cat < xtitlebar -- change the name of an xterm, gnome-terminal or kde konsoleZ3yLinux联盟
Z3yLinux联盟
USAGE: xtitlebar [-h] "string_for_titelbar"Z3yLinux联盟
Z3yLinux联盟
OPTIONS: -h help textZ3yLinux联盟
Z3yLinux联盟
EXAMPLE: xtitlebar "cvs"Z3yLinux联盟
Z3yLinux联盟
HELPZ3yLinux联盟
  exit 0Z3yLinux联盟
}Z3yLinux联盟
Z3yLinux联盟
# in case of error or if -h is given we call the function help:Z3yLinux联盟
[ -z "$1" ] && helpZ3yLinux联盟
[ "$1" = "-h" ] && helpZ3yLinux联盟
Z3yLinux联盟
# send the escape sequence to change the xterm titelbar:Z3yLinux联盟
echo -e "33]0;$107" Z3yLinux联盟
#Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  在脚本中提供帮助是一种很好的编程习惯,这样方便其他用户(和您)使用和理解脚本。Z3yLinux联盟
Z3yLinux联盟
命令行参数Z3yLinux联盟
Z3yLinux联盟
  我们已经见过$* 和 $1, $2 ... $9 等特殊变量,这些特殊变量包含了用户从命令行输入的参数。迄今为止,我们仅仅了解了一些简单的命令行语法(比如一些强制性的参数和查看帮助的-h选项)。但是在编写更复杂的程序时,您可能会发现您需要更多的自定义的选项。通常的惯例是在所有可选的参数之前加一个减号,后面再加上参数值 (比如文件名)。 Z3yLinux联盟
Z3yLinux联盟
  有好多方法可以实现对输入参数的分析,但是下面的使用case表达式的例子无遗是一个不错的方法。 Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
#!/bin/shZ3yLinux联盟
help()Z3yLinux联盟
{Z3yLinux联盟
 cat < This is a generic command line parser demo.Z3yLinux联盟
USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2Z3yLinux联盟
HELPZ3yLinux联盟
 exit 0Z3yLinux联盟
}Z3yLinux联盟
Z3yLinux联盟
while [ -n "$1" ]; doZ3yLinux联盟
case $1 inZ3yLinux联盟
  -h) help;shift 1;; # function help is calledZ3yLinux联盟
  -f) opt_f=1;shift 1;; # variable opt_f is setZ3yLinux联盟
  -l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2Z3yLinux联盟
  --) shift;break;; # end of optionsZ3yLinux联盟
  -*) echo "error: no such option $1. -h for help";exit 1;;Z3yLinux联盟
  *) break;;Z3yLinux联盟
esacZ3yLinux联盟
doneZ3yLinux联盟
Z3yLinux联盟
echo "opt_f is $opt_f"Z3yLinux联盟
echo "opt_l is $opt_l"Z3yLinux联盟
echo "first arg is $1"Z3yLinux联盟
echo "2nd arg is $2"Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  您可以这样运行该脚本:Z3yLinux联盟
Z3yLinux联盟
cmdparser -l hello -f -- -somefile1 somefile2Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  返回的结果是:Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
opt_f is 1Z3yLinux联盟
opt_l is helloZ3yLinux联盟
first arg is -somefile1Z3yLinux联盟
2nd arg is somefile2Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  这个脚本是如何工作的呢?脚本首先在所有输入命令行参数中进行循环,将输入参数与case表达式进行比较,如果匹配则设置一个变量并且移除该参数。根据unix系统的惯例,首先输入的应该是包含减号的参数。 Z3yLinux联盟
Z3yLinux联盟
实例Z3yLinux联盟
Z3yLinux联盟
  一般编程步骤Z3yLinux联盟
Z3yLinux联盟
  现在我们来讨论编写一个脚本的一般步骤。任何优秀的脚本都应该具有帮助和输入参数。并且写一个伪脚本(framework.sh),该脚本包含了大多数脚本都需要的框架结构,是一个非常不错的主意。这时候,在写一个新的脚本时我们只需要执行一下copy命令: Z3yLinux联盟
Z3yLinux联盟
cp framework.sh myscriptZ3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
  然后再插入自己的函数。Z3yLinux联盟
Z3yLinux联盟
  让我们再看两个例子: Z3yLinux联盟
Z3yLinux联盟
  二进制到十进制的转换Z3yLinux联盟
Z3yLinux联盟
  脚本 b2d 将二进制数 (比如 1101) 转换为相应的十进制数。这也是一个用expr命令进行数学运算的例子: Z3yLinux联盟
Z3yLinux联盟
Z3yLinux联盟
#!/bin/shZ3yLinux联盟
# vim: set sw=4 ts=4 et:Z3yLinux联盟
help()Z3yLinux联盟
{Z3yLinux联盟
 cat < b2h -- convert binary to decimalZ3yLinux联盟
Z3yLinux联盟
USAGE: b2h [-h] binarynumZ3yLinux联盟
Z3yLinux联盟
OPTIONS: -h help textZ3yLinux联盟
Z3yLinux联盟
EXAMPLE: b2h 111010Z3yLinux联盟
will return 58Z3yLinux联盟
HELPZ3yLinux联盟
 exit 0Z3yLinux联盟
}Z3yLinux联盟
Z3yLinux联盟
error()Z3yLinux联盟
{Z3yLinux联盟
  # print an error and exitZ3yLinux联盟
  echo "$1"Z3yLinux联盟
  exit 1Z3yLinux联盟
}Z3yLinux联盟
Z3yLinux联盟
lastchar()Z3yLinux联盟
{Z3yLinux联盟
  # return the last character of a string in $rvalZ3yLinux联盟
  if [ -z "$1" ]; thenZ3yLinux联盟
    # empty stringZ3yLinux联盟
    rval=""Z3yLinux联盟
    returnZ3yLinux联盟
  fiZ3yLinux联盟
  # wc puts some space behind the output this is why we need sed:Z3yLinux联盟
  numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `Z3yLinux联盟
  # now cut out the last charZ3yLinux联盟
  rval=`echo -n "$1" | cut -b $numofchar`Z3yLinux联盟
}Z3yLinux联盟
Z3yLinux联盟
chop()Z3yLinux联盟
{Z3yLinux联盟
  # remove the last character in string and return it in $rvalZ3yLinux联盟
  if [ -z "$1" ]; thenZ3yLinux联盟
    # empty stringZ3yLinux联盟
    rval=""Z3yLinux联盟
    returnZ3yLinux联盟
  fiZ3yLinux联盟
  # wc puts some space behind the output this is why we need sed:Z3yLinux联盟
  numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `Z3yLinux联盟
  if [ "$numofchar" = "1" ]; thenZ3yLinux联盟
    # only one char in stringZ3yLinux联盟
    rval=""Z3yLinux联盟
    returnZ3yLinux联盟
  fiZ3yLinux联盟
  numofcharminus1=`expr $numofchar "-" 1`Z3yLinux联盟
  # now cut all but the last char:Z3yLinux联盟

来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
    无相关信息
栏目更新
栏目热门