基本脚本编译.doc_第1页
基本脚本编译.doc_第2页
基本脚本编译.doc_第3页
基本脚本编译.doc_第4页
基本脚本编译.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

1、 创建脚本文件创建shell脚本文件时,必须在文件的第一行指明所使用的shell,指明shell后将命令输入到文件中的每一行。#!/bin/bash在普通的shell脚本行中,英镑符号(#)用作注释。Shell不处理脚本中的注释行。但是shell脚本文件的第一行是个特例,#后面跟着感叹号告诉shell运行下列脚本的shell。Shell不会解析以#(#!开通的第一行除外)开头的行。richlocalhost $ cat test1 #!/bin/bash#This script displays the date and whos logged on datewhorichlocalhost $ ./test1 Sun Sep 18 09:15:53 CST 2011root pts/0 2011-09-18 09:00 (192.168.0.5)脚本的执行:给脚本执行权限chmod u+x test1 ./脚本名称 来执行脚本将脚本放到PATH变量中$PATH=$PATH:/home/bin并赋予变来那个执行权限(任何目录下都可以执行)2、 显示消息echo命令后面添加字符串,echo命令就能显示一个简单的文本字符串。echo命令即可使用双引号也可以使用单引号来标记文本字符串。如果要在字符串中使用它们,需要在一个文本使用一种引号类型,然后使用另一种类型标记字符串:echo This is a test to see if youre paying attenionThis is a test to see if youre paying attenionecho Rich says scripting is easy.Rich says scripting is easy.richlocalhost bin$ cat test1 #!/bin/bash#This script displays the date and whos logged on echo The time and date are:dateecho lets see whos logged into the system:whorichlocalhost bin$ ./test1 The time and date are:Sun Sep 18 09:50:07 CST 2011lets see whos logged into the system:root pts/0 2011-09-18 09:00 (192.168.0.5)如果想要echo命令结果和echo文本字符串在同一行,只需对echo语句使用-n参数即可richlocalhost bin$ cat test1 #!/bin/bash#This script displays the date and whos logged on echo -n The time and date are:dateecho lets see whos logged into the system:whorichlocalhost bin$ ./test1 The time and date are:Sun Sep 18 09:52:02 CST 2011lets see whos logged into the system:root pts/0 2011-09-18 09:00 (192.168.0.5)3、 使用变量 通过使用以美元符号开头的变量可以从脚本中引进环境变量richlocalhost $ cat test2#!/bin/bash#display user information frm the systemecho User info for userid: $USERecho UID: $UIDecho HOME: $HOMErichlocalhost $ ./test2User info for userid: richUID: 501HOME: /home/rich注意:当运行脚本时,echo命令中的环境变量会被它们的当前值所代替。无论何时当脚本中发现英镑符号时,它就会认为您引用了一个变量,要显示一个实际的美元符号,必须在它们前面加上反斜杠符号richlocalhost $ echo The cost of the item is $15The cost of the item is 5richlocalhost $ echo The cost of the item is $15The cost of the item is $154、 用户变量 除了环境变量,shell脚本中允许在脚本中设置自己和使用自己的变量。设置变量可以暂时存储数据并在脚本中使用他们。 用户变量可以由不超过20个字符的字母、数字或下划线组成的文本文本字符串。用户变量区分大小写,在变量、等号和变量值之间不允许有空格。脚本中定义的变量在shell脚本中的生命周期内保留他们的值,但是当shell脚本完成时就被删除了。richlocalhost $ cat test3#!/bin/bash#testing variablesdays=10guest=Katieecho $guest checked in $days days agodays=5guest=Jessicaecho $guest checked in $days days agorichlocalhost $ ./test3Katie checked in 10 days agoJessica checked in 5 days ago没有美元符号,shell就会把变量理解为一个普通文本字符串。richlocalhost $ cat test4#!/bin/bash#testing a variable value to another variablevalue1=10value2=$value1echo The resulting value is $value2richlocalhost $ ./test4The resulting value is 10richlocalhost $ cat test4 #!/bin/bash#testing a variable value to another variablevalue1=10value2=$value1echo The resulting value is value2richlocalhost $ ./test4The resulting value is value25、 反引号 反引号允许将shell命令中的输出赋值给变量。必须将整个命令用反引号包围起来。richlocalhost $ cat test5#!/bin/bash#using the backtick charatertesting=dateecho The date and time are: $testingrichlocalhost $ ./test5The date and time are: Sun Sep 18 10:42:52 CST 2011常用案例,利用反引号来捕获当前日期,并用它在脚本中创建唯一的文件名richlocalhost $ cat date #!/bin/bash#copy the /usr/bin directory listing to a file logtoday=date +%y%m%d/bin/ls -la /usr/bin $HOME/log.$todayrichlocalhost $ lsbin date log.110918 test1 test2 test3 test4 test56、 重定向输入和输出richlocalhost $ cat test6richlocalhost $ date test6richlocalhost $ cat test6Sun Sep 18 10:56:38 CST 2011richlocalhost $ cat test6testingrichlocalhost $ date test6richlocalhost $ cat test6testingSun Sep 18 10:58:27 CST 2011richlocalhost $ wc test6 2 7 37richlocalhost $ wc test string test string 1 test string 2 EOF 3 8 40richlocalhost $ 7、 管道将输出命令重定向到另一条命令,而不是将命令的输出重定向到一个文件,这个过程成为管道传送。管道传送的符号是竖条操作符(|)richlocalhost $ rpm -qa | sort rpm.listrichlocalhost $ ls bin date log.110918 rpm.list test1 test2 test3 test4 test5 test68、 数字计算expr命令允许处理命令行中的等式,但是很笨拙 richlocalhost $ expr 1 + 12richlocalhost $ expr 1+11+1在传送expr命令的字符可能被错误解析前,需要使用shell转义字符(反斜杠)来识别它们。richlocalhost $ expr 5 * 2expr: syntax errorrichlocalhost $ expr 5 * 210richlocalhost $ cat test6#!/bin/bash#An example of using the expr commandvar1=10var2=20var3=expr $var2 / $var1echo The result is: $var3richlocalhost $ ./test6The result is: 29、 是用括号Bash为一个变量制定一个数学值时,可以使用美元符号和方括号把数学等式括起来richlocalhost $ var1=$1 + 5richlocalhost $ echo $var16richlocalhost $ var2=$var1 * 2richlocalhost $ echo $var212使用方括号方法计算等式时,不必担心shel错误理解乘法符号或者其他符号。Shell知道它不是一个通配符,因为它在括号里。richlocalhost $ cat test7#!/bin/bashvar1=100var2=50var3=45var4=$var1 * ($var2 - $var3)echo The final result is $var4richlocalhost $ ./test7The final result is 500Bash shell只支持整数算法richlocalhost $ cat test8#!/bin/bashvar1=100var2=45var3=$var1 / $var2echo The final result is $var3richlocalhost $ ./test8The final result is 210、 浮点解决方案scale 变量的默认值是0。在设置scale值之前,bash计算器bc提供0小数位的答案。将scale变量值设为4之后,bash计算器显示为4位小数位的答案。-q命令行参数禁止bash计算器的冗余欢迎标语。richlocalhost $ bc -q3.44 / 50scale=43.44 / 5.6880quitrichlocalhost $除了普通的数字,bash计算器也识别变量。一旦定义了变量,可以在整个bash计算器中使用该变量。Print语句允许打印变量和数字。richlocalhost $ bc -qvar1=10var1 * 440var2=var1 / 5print var22quitrichlocalhost $ 11、 在脚本中使用bcrichlocalhost $ cat test9#!/bin/bashvar1=echo scale=4; 3.44 / 5 | bcecho The answer is $var1richlocalhost $ ./test9The answer is .6880richlocalhost $ cat test10#!/bin/bashvar1=100var2=45var3=echo scale=4; $var1 / $var2 | bcecho The answer for this is $var3richlocalhost $ ./test10The answer for this is 2.2222richlocalhost $ cat test11#!/bin/bashvar1=20var2=3.14159var3=echo scale=4; $var1 * $var1 | bcvar4=echo scale=4; $var3 * $var2 | bcecho The final result is $var4richlocalhost $ ./test11The final result is 1256.63600richlocalhost $ clearrichlocalhost $ cat test12#!/bin/bashvar1=10.46var2=43.67var3=33.2var4=71var5=bc EOFscale=4a1=($var1 * $var2)b1=($var3 * $var4)a1+b1EOFecho The final answer for this mess if $var5richlocalhost $ ./test12The final answer for this mess if 2813.988212、 退出脚本 为了向shell表明,命令已经处理完毕,每条运行在shell都使用一个推出状态。这个推出状态是介于0255之间的数值。当命令执行完成时,命令就会把推出状态传递给shell。Linux提供$?特殊变量来保存最后一条命令执行结束的推出状态。一条命令成功完成的推出状态是0.如果命令执行错误,那么推出状态机会是一个正整数。 Linux推出状态代码代码描述代码描述0命令完成成功128无效的推出参数1通常的未知错误128+x使用Linux信号x的致命错误2误用shell命令130使用Ctrl+c终止命令126命令无法找到255规范外的推出状态127没有找到命令richlocalhost $ date Sun Sep 18 14:38:16 CST 2011richlocalhost $ echo $?0richlocalhost $ asdf-bash: asdf: command not foundrichlocalhost $ echo $?127richlocalhost $ ./test1-bash: ./test1: Permission deniedrichlocalhost $ echo $?126richlocalhost $ date -tdate: invalid option - tTry date -help for more information.richlocalhost $ echo $?1richlocalhost $ exit命令允

温馨提示

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

评论

0/150

提交评论