1 / 43

第十五章

第十五章. shell 编程初步. 第十五章 shell 编程初步. § 15.1 shell 脚本 中的参数传递 § 15.2 算术运算 expr 命令 § 15.3 条件执行 § 15.4 条件测试命令 § 15.5 使用 if 结构 § 15.6 read 命令 § 15.7 循环结构 § 15.8 shell 内部命令 § 15.9 软中断 § 15.10 综合示例. shell 编程初步 (2). 本章要点. 了解 shell 变量的类型 掌握 AIX 中编写 shell 脚本的基本方法.

langer
Download Presentation

第十五章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第十五章 shell 编程初步

  2. 第十五章 shell 编程初步 §15.1shell脚本中的参数传递 §15.2算术运算expr命令 §15.3条件执行 §15.4条件测试命令 §15.5使用if结构 §15.6read命令 § 15.7循环结构 § 15.8shell内部命令 § 15.9 软中断 § 15.10 综合示例

  3. shell 编程初步 (2) 本章要点 了解shell变量的类型 掌握AIX中编写shell脚本的基本方法

  4. 15.1 shell 脚本中的参数传递 shell变量分类: (1) 环境变量 (2)预定义变量 (3)自定义变量

  5. shell预定义变量 $$ $0 $# $* $? $! 当前进程号(PID) Shell脚本名 传递到shell脚本的位置参数个数 传递到shell脚本的所有命令行变量 上一命令的退出值 上一次后台执行的进程号

  6. 位置参数 参数可以通过命令行作为变量传递给shell脚本 $1,$2, $3, . . . , $9 ${10}, ${11}, . . . , ${n} (仅适用于Korn shell ) 在Bourne shell中,一次最多能够使用的变量个数为9个

  7. 位置参数(2) 示例 $ cat ascript echo First Parameter entered was $1 echo Second Parameter entered was $2 echo Third Parameter entered was $3 $ ascript Good Day Sydney First Parameter entered was Good Second Parameter entered was Day Third Parameter entered was Sydney 注: 上例中的$1,$2,$3分别代表第一个位置参数,第二个位置参数,第三个位置参数

  8. 15.2 算术运算expr 命令 功能: 通过expr命令可以实现整数运算 expr命令提供以下的运算操作符 \* / % + - 乘法运算操作符 除法运算操作符 求模运算符 加法运算符 减法运算符

  9. expr 命令(2) 示例1 $ var1 = 6 $ var2 = 3 $ expr $var1 / $var2 2 $ expr $var1 - $var2 3

  10. expr 命令(3) 使用\( \)可以对算数表达式重新分组,实现新的运算顺序 示例2 $ expr \( $var1 + $var2 \) \* 5 45 通过使用命令取代可以将运算结果存储在一个变量中 示例3 $ var3 = $(expr $var1 / $var2 ) $ echo $var3 2

  11. 15.3 条件执行 一个或一组命令的退出值可以作为下一命令执行以否的 依据 1.与运算 commmand1 && command2 (command1成功则执行command2) 示例: $ ls s* && rm s*

  12. 条件执行(2) 2. 或运算 command1 || command2(命令1不成功则执行命令2) 示例: $ cd /dir1 || echo Cannot change to dir1 && 上一命令正确执行,退出值为0时才执行下一个命令 || 上一命令执行不成功,退出值为非0时执行下一命令

  13. 15.4 条件测试命令 格式: test expression 或 [ expression ] 或 [ [ expression ] ] 功能:测试某一条件是否满足, 并据此返回一个true 或false。 test多用于if和while控制结构中。

  14. 条件测试命令(2) 如果满足下面的条件则返回true Strings are equal Strings are not equal Numbers are equal Numbers are not equal File exists and length > 0 File is a directory File is readable File is writable operator $string1 = $string2 $string1 != $string2 $number1 –eq $number2 $number1 –ne $number2 -s $file -d $file -r $file -w $file

  15. 15.5 使用 if 结构 语法格式: if condition is true then carry out this set of actions else carry out these alternative actions fi

  16. if命令示例 $ cat active USAGE=“$0 userid”if [[ $# -ne 1 ]] then echo “Proper Usage: $USAGE” exit fi if [ who | grep $1 > /dev/null ] then echo “ $1 is active” else echo “ $1 is not active” fi exit 0

  17. 15.6 read命令 功能: 从标准输入设备读入一行,并将其赋给一个shell变量 示例: $ cat delfile # Usage: delfile echo “please enter the file name: “ read name if [[ -f $name ]] then rm $name else echo “Error: $name is not an ordinary file” fi

  18. 15.7 循环结构 使用for结构构建循环 语法结构: for var in list do command(s) done

  19. 循环结构(2) for循环示例: $ cat count for var in file1 file2 file3 Do wc –l $var done $ count 18 file1 19 file2 20 file3

  20. 循环结构(3) while语法结构: While expression do command(s) done while循环会一直执行到表达式的值为false 可以用<ctrl –c>中断循环的执行

  21. 循环结构(4) while循环示例: $ cat information X=1 while [[ $x –lt 9 ]] do echo “It is now $(date)” echo “There are $(ps –e| wc –l) processes running echo “There are $(who | wc –l) users logged in” x=$(expr $x + 1) sleep 600 done

  22. 15.8 shell内部命令 shell内部命令是内建于shell中命令,使用这些 命令的时候不会开启子shell,因而使用内部命令 比系统里的外部命令运行速度快。 由于性能的原因,在进行shell编程时,应该尽 量使用内部命令。

  23. Shell内部命令 (2) shell内部命令列表: : . continue echo eval exec exit 空,永远返回为true 从当前shell中执行操作 退出for,while,until或case语句 返回信息到标准输出 读取参数执行结果命令 开新shell执行命令 退出当前shell

  24. Shell内部命令 (3) export pwd read readonly return set shift 导出变量,使当前shell可以利用 显示当前目录 从标准输入读取一行文本 使变量只读 退出函数并带有返回值 控制各种参数到标准输入的显示 命令行参数向左偏移一个

  25. Shell内部命令 (4) test times trap ulimit umask unset wait 评估表达式 显示shell运行的用户和系统时间 当捕获信号时运行指令 显示或设置shell资源 显示或设置创建文件的缺省模式 从shell内存中删除变量或函数 等待直到子进程运行完毕,报告终止

  26. Shell内部命令 示例 $ pg set_ex #!/bin/sh set accounts.doc accounts.bak while [ $# != 0 ] do echo $1 shift done $ set_ex accounts.doc accounts.bak

  27. 15.9 软中断 软中断是利用硬件中断的概念,用软件方式进行 模拟,实现宏观上的异步执行效果 在shell脚本中,软中断的是通过trap命令来实现的

  28. trap命令 格式: trap “name” signal(s) 功能: 在脚本中捕获信号。 name是捕捉到信号后所采取的处理动作。 singlal(s)就是待捕捉的信号

  29. 软中断-信号处理 脚本在捕捉到一个信号以后,通常会采取某些行动最常见的行动包括: 1) 清除临时文件。 2) 忽略该信号。 3) 询问用户是否终止该脚本的运行。

  30. 软中断-信号处理 (2) 常见的trap命令用法: trap " " 2 3 trap “cmd” 2 3 trap 2 3 忽略信号2和信号3,用户不能终止该脚本 如果捕捉到信号2或3,就执行相应的cmd 复位信号2和3,用户可以终止该脚本

  31. 软中断 示例 #cat trap1 #!/bin/sh #trap1 trap "my_exit" 2 LOOP=0 my_exit() { echo "you just hit <CTRL -C>, at number $LOOP" echo " I will now exit" exit 1 } while : do LOOP=`expr $LOOP + 1` echo $LOOP done

  32. 命令搜索顺序 合法路径名 保留字 别名 内置命令 函数 路径变量

  33. 综合示例 1 PATH=/bin:/usr/bin:/etc/:$HOME/bin:. PS1=‘$PWD => ‘ ENV=$HOME/.kshrc if [ -s “$MAIL” ] then mail fi echo “Enter Terminal Type [Default: ibm3151):\c” read a if [ -n “$a” ] then TERM=$a else TERM=ibm3151 fi echo “it is now $(date)” echo “There are $(ps –e| wc –l) processes running” echo “There are $(who | wc –l) users logged in” export PATH ENV TERM PS1

  34. 综合示例 2 #!/bin/sh #logroll #roll over the log files if size have reach the MARK #could also be used for mail boxes #limit size of log: 4096k BLOCK_LIMIT=8 MYDATE=`date +%d%m` #list of logs to check LOGS="var/spool/audlog /var/spool/networks/netlog /etc/dns/named_log" for LOGFILE in LOGS do if [ -f LOG_FILE ]; then # get the block size F_SIZE=`du -a $LOG_FILE | cut -f1` else echo "`basename $0` cannot find the $LOG_FILE" > &2 continue fi

  35. 综合示例 2 if [ "$F_SIZE" -gt "$BLOCK_LIMIT" ]; then #copy the log across and append a ddmm date on it cp $LOG_FILE $LOG_FILE$MYDATE #clear the log >$LOG_FILE chgrp admin $LOG_FILE$MYDATE fi done 该脚本的功能是: 检查日志文件的长度并倒换这些日志文件。如果某个日志文件超过了特定的长度,那么它的内容将被拷贝到另一个文件中,并清除原有文件中的内容。

  36. 测试题 1. 在命令行中进行整数运算应该采用以下哪个命令: A. grep B. expr C. echo D. egrep

  37. 测试题 2 2. 通过以下哪条命令可以查看到当前进程的PID: • echo $$ • echo $? • echo $# • echo $0

  38. 测试题 3 3. 简述下列脚本的功能: TERMTYPE=$TERM if [ $TERMTYPE != “” ] then if [ -f /home/team01/customized_script ] then /home/team01/customized_script else echo No customized script available ! fi else echo “you do not have a TERM variable set !” fi

  39. 测试题 4 4. 设计一个脚本显示以下图形: 11,22,33,44,55,66,77,88,99 22,33,44,55,66,77,88,99, 33,44,55,66,77,88,99,, 44,55,66,77,88,99,,, 55,66,77,88,99,,,, 66,77,88,99,,,,, 77,88,99,,,,,, 88,99,,,,,,, 99,,,,,,,,

  40. 测试题 5 5. 设计一个脚本实现以下功能:列出指定目录下的所 有文件并把这些文件拷贝到另一个目录中

  41. 测试题答案 1. B 2. A 3. 在/home/team01下查找有无customized_script 脚本,有就执行该脚本,无则回显一条信息 。

  42. 测试题答案(2) 4. set 11 22 33 44 55 66 77 88 99 echo $1,$2,$3,$4,$5,$6,$7,$8,$9;shift echo $1,$2,$3,$4,$5,$6,$7,$8,$9;shift echo $1,$2,$3,$4,$5,$6,$7,$8,$9;shift echo $1,$2,$3,$4,$5,$6,$7,$8,$9;shift echo $1,$2,$3,$4,$5,$6,$7,$8,$9;shift echo $1,$2,$3,$4,$5,$6,$7,$8,$9;shift echo $1,$2,$3,$4,$5,$6,$7,$8,$9;shift echo $1,$2,$3,$4,$5,$6,$7,$8,$9;shift echo $1,$2,$3,$4,$5,$6,$7,$8,$9

  43. 测试题答案(3) 5. #!/bin/sh #this script is used to list the content of the # source dir and copy them to the target dir if [ $# -lt 2 ]; then echo "usage: $0 sourcedir aimdir"; else cd $1 echo "the content of $1 is: " ls -l cp -rf * $2 fi

More Related