shells编程快速入门(四)之bashshell.docx

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Shells编程快速入门(四)之Bash Shell Bash Shell 结构 Korn和Bash shells非常相似,但是还是有一些不同之处。Bash的结构如下所示 Bash Shell 语法结构 Shba ng 行 shbang是脚本起始行,告诉kernel那个shell解析.#!位于行头。例 如 #!/bi n/bash 注释 仃注释用#符号.例如:# This is a comment 通配 符 例如*, ?,和[] 用于文件名扩展。,,2, , 和|符号用于10 和重定向和管道。为了保证这些符号不被解析,这个字符要被引起来。 例 如: rm *; ls ??; cat file[1-3]; echo How are you? 输出 显示 使用echo命令。使用 或者一对“”通配符。例如: echo How are you? 局部 变量 局部变量作用于当前shell , shell结束时局部变量失效.例如 variable. name=value declare variable. name=value n ame=Joh n Doe x=5 全局 变量 全局变量也称为环境变量.例如:内建的带-x选项的声明函数也可以设置 为环境变量。可以用export使用。例如: export VARIABLE_NAME=value declare -x VARIABLE_NAME=value export PATH=/bi n: /usr/bi n:. 从变 使用$.例如: 量中 提取 echo $variable_ name 值 echo $n ame echo $PATH 读取 使用read读入一行。例如: 用户 输入 EXAMPLE echo What is your n ame? read n ame read n ame1 n ame2 ... 参数 可以从命令行传入参数。 位置参数用于从脚本中接收值。例如: At the comma nd line: $ script name arg1 arg2 arg3 ... 在脚本中: echo $1 $2 $3 位置参数 echo $* 所有位置参数 echo $# 位置参数号 数组 Bourne shell使用位置参数创建单词列表。除了位置参数外 ,Bash shell 支持数组语法,起始索引是 0。Bash shell数组使用declare -a命令创 建。例如: set apples pears peaches (positi onal parameters) echo $1 $2 $3 declare -a array_ name=(word1 word2 word3 ...) declare -a fruit=( apples pears plums ) echo ${fruit[0]} 算术 像C/TC shells 和Bourne shell, UNIX/Linux 命令的输出可以指定到一 个变量。Bash shell提供新的语法.使用前端加$,例如: variable. name=comma nd variable. name=$( comma nd ) echo $variable_ name echo Today is date echo Today is $(date) 算术 Bash shells支持整数算术。declare -i命名用于声明一个整型变量。Korn shell的typeset命令也可以用于向后兼容。 例如 declare -i variable. name used for bash typeset -i variable. name can be used to be compatible with ksh ((n=5 + 5 )) echo $n 操作 符 Bash shell使用内建命令,类似于 C语言。 : 例如 相等性: 逻辑性: |== [equal to and != not equal to II or ! not 关系型: greater tha n = greater tha n, equal to less 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: if comma nd if comma nd the n the n block of block of stateme nts stateme nts elif comma

文档评论(0)

xiaozu + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档