基本脚本编译

上传人:s9****2 文档编号:548933764 上传时间:2022-11-16 格式:DOC 页数:8 大小:58.50KB
返回 下载 相关 举报
基本脚本编译_第1页
第1页 / 共8页
基本脚本编译_第2页
第2页 / 共8页
基本脚本编译_第3页
第3页 / 共8页
基本脚本编译_第4页
第4页 / 共8页
基本脚本编译_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《基本脚本编译》由会员分享,可在线阅读,更多相关《基本脚本编译(8页珍藏版)》请在金锄头文库上搜索。

1、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 datewhorichlocalhos

2、t $ ./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 T

3、his 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 i

4、nto 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 log

5、ged 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

6、#!/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 Th

7、e 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脚本完成时就被

8、删除了。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 t

9、est4#!/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 v

10、alue2richlocalhost $ ./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常用案例,利用反引号来

11、捕获当前日期,并用它在脚本中创建唯一的文件名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 test6richloca

12、lhost $ 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、 管道将输出命令重定向到另一条命令,而

13、不是将命令的输出重定向到一个文件,这个过程成为管道传送。管道传送的符号是竖条操作符(|)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转义字符(反斜杠)来识别它们。rich

14、localhost $ 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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号