Shells编程快速入门四之BashShell_第1页
Shells编程快速入门四之BashShell_第2页
Shells编程快速入门四之BashShell_第3页
Shells编程快速入门四之BashShell_第4页
Shells编程快速入门四之BashShell_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Shells编程快速入门(四)之Bash ShellBash Shell 结构Korn和Bash shells非常相似,但是还是有一些不同之处。Bash的结构如下所示Bash Shell 语法结构Shbang行shbang是脚本起始行,告诉kernel那个shell解析.#!位于行头。例 如#!/bi n/bash注释仃注释用#符号.例如:# This is a comment通配符例如*, ?,和用于文件名扩展。,2, ,和|符号用于10和重定向和管道。为了保证这些符号不被解析,这个字符要被引起来。例如:rm *; ls ?;cat file1-3;echo How are you?输出显示

2、使用echo命令。使用或者一对“”通配符。例如:echo How are you?局部变量局部变量作用于当前shell,shell结束时局部变量失效.例如variable. name=valuedeclare variable. name=valuen ame=Joh n Doex=5全局变量全局变量也称为环境变量.例如: 内建的带-x选项的声明函数也可以设置 为环境变量。可以用export使用。例如:export VARIABLE_NAME=valuedeclare -x VARIABLE_NAME=valueexport PATH=/bi n: /usr/bi n:.从变使用$.例如:量中

3、提取echo $variable_ name值echo $n ameecho $PATH读取使用read读入一行。例如:用户输入EXAMPLEecho What is your n ame?read n ameread n ame1 n ame2 .参数可以从命令行传入参数。 位置参数用于从脚本中接收值。例如:At the comma nd line:$ script name arg1 arg2 arg3 .在脚本中:echo $1 $2 $3位置参数echo $*所有位置参数echo $#位置参数号数组Bourne shell使用位置参数创建单词列表。除了位置参数外,Bash shell支

4、持数组语法,起始索引是0。Bash shell数组使用declare -a命令创建。例如:set apples pears peaches(positi onal parameters)echo $1 $2 $3declare -a array_ name=(word1 word2 word3 .)declare -a fruit=( apples pears plums )echo $fruit0算术像C/TC shells和Bourne shell, UNIX/Linux命令的输出可以指定到一个变量。Bash shell提供新的语法.使用前端加$,例如:variable. name=com

5、ma ndvariable. name=$( comma nd )echo $variable_ nameecho Today is dateecho Today is $(date)算术Bash shells支持整数算术。declare -i命名用于声明一个整型变量。Korn shell的typeset命令也可以用于向后兼容。例如declare -i variable. nameused for bashtypeset -i variable. namecan be used to be compatible with ksh(n=5 + 5 )echo $n操作符Bash shell使用内

6、建命令,类似于C语言。:例如相等性:逻辑性:|=equal to&and!=not equaltoIIor!not关系型:greater tha n=greater tha n, equal toless tha n=less tha n, equal to条件If类似于C语言。if用endif结束。用于模式匹配条件表达式。语句用于向后兼容Bour ne shell。例如:The if con struct is:The if/else/else if con struct is:ifcomma ndifcomma ndthe nthe nblock ofblock of stateme nt

7、sstateme ntselifcomma ndfithe nblock of stateme ntsif expressi onelseifcomma ndthe nthe nblock ofstateme ntsblock of stateme ntsfielseblock of stateme ntsif ( nu mericfiexpressi on)the nblock ofstateme ntsifexpressi on elsethe nblock ofblock of stateme ntsstateme ntsfiThe if/else con structis:if com

8、ma nd the nblock ofstateme ntselseblock ofstateme ntsfiif expressi on the nblock ofstateme ntselseblock ofstateme ntsfiif (nu mericexpressi on ) the nelifexpressi on the nblock of stateme ntselse ifexpressi on the nblock of stateme ntselseblock of stateme ntsfiif (nu meric expressi on )the nblock of

9、 stateme ntselif(nu meric expressi on )the nblock of stateme ntselse if(nu meric expressi on)the nblock of stateme ntselseblock of stateme nts elseblock ofstateme ntsfiThe case con struct is: case variable, name in pattern stateme ntsJ Jpatter n2) stateme ntsJ Jpatter n3)J Jesaccase $color inblue) e

10、cho $coloris blueJ Jgree n)echo $colorfiis gree nJ Jred|ora nge)echo $color is redor orangeJ J*) echo Not a matachJ Jesac循环四种循环while, until, for,和select.while循环后跟随,do关键子,代码段,结束于done关键子。是新 的测试操作符,老的仍旧向后兼容Bourne shell.until循环类似于while循环。for循环用于遍历一个子列表。For循环后跟随变量名,in关键子,子列 表,代码块,结束于done关键字。select循环用于提示菜

11、单选择。循环控制命令是break和continue。例如whilecomma ndun tilcomma nddodoblock ofstateme ntsblock of stateme nts donedonewhile stringexpression untilstri ng expressi on dodoblock ofstateme ntsblockof stateme ntsdonedonewhile ( nu meric expressi on )un til(nu meric expressi on )dodoblock ofstateme ntsblock of stat

12、eme ntsdonedonefor variable in wordistselect variable in wordistdodoblock ofstateme ntsblockof stateme ntsdonedonefor color in red gree n bPS3=Select an item from the menudodoitem in blue red gree necho$colorecho$itemdonedoneShows menu:1.blue2.red3.green函数有两种格式.一种格式来自于Bourne shell,Bash版本使用function关键

13、字:例如函数functioname() block of codefunctionfunctioname block of codefunctionlister echo Your prese nt work ing directory is pwdecho Your files are:lsBash Shell脚本例子:1#!/bi n/bash# GNU bash versions 2.x2# The Party Program Invitationsto friends from the guest file3guestfile=/shell/guests4if !-e $guestfi

14、lethe n5printf $guestfile#*/ non-existentexit 1fi6export PLACE=Sarotinis7( Time=$(date +%H) + 1 )8declare -a foods=(cheese crackers shrimp drinks hot dogs sandwiches)9declare -i n=010for person in $(cat $guestfile)do11if $person = root thencontinueelse# Start of here document12mail-v-s Party $person

15、 - FINISHi $person! Please join me at $PLACE for a party!Meet me at $Time oclock.Ill bring the ice cream. Would you please bring$foods$n and anything else you would like to eat?Let me know if you can make it.Hope to see you soon.Your pal,ellie$(hostname)FINIS13n=n+114 if ( $#foods* = $n )thendeclare -a foods=(cheese crackers shrimpsandwiches)16n=0fi fi17

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论