版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、course materials may not be reproduced in whole or in part without the prior written permission of ibm.5.04.1 copyright ibm corporation 2008 unit 15additional shell features copyright ibm corporation 2008unit objectivesafter completing this unit, you should be able to: pass positional parameters to
2、shell scriptsuse the test commanduse the if statementimplement interactive shell scriptsimplement loops within scripts copyright ibm corporation 2008$process id (pid)$0shell script name$#number of arguments passed to the shell script$*all command line arguments passed to the script$?exit value of th
3、e last command$!process id of last background processimportant shell variables copyright ibm corporation 2008$0$1$2$3positional parametersparameters can be passed to shell scripts as arguments on the command line: $1, $2, . $9 $10, $11, . $n (korn shell only)$ cat para_scriptecho first parameter ent
4、ered was $1echo second parameter entered was $2echo third parameter entered was $3$ para_script good day sydneyfirst parameter entered was goodsecond parameter entered was daythird parameter entered was sydney copyright ibm corporation 2008highlowprecedencethe expr utilityl use the expr utility to p
5、erform integer arithmetic.l expr offers the following operators: * multiplication / integer division % remainder + addition - subtraction (also unary minus sign) copyright ibm corporation 2008$ var1=6$ var2=3 $ expr $var1 / $var22$ expr $var1 - $var23= use ( ) to group expressions:$ expr ( $var1 + $
6、var2 ) * 545 = use command substitution to store the result in a variable: $ var3=$(expr $var1 / $var2)$ echo $var32expr examples copyright ibm corporation 2008conditional executionthe exit value from a command or group of commands can be used to determine whether to do the next command: command1 &a
7、mp; command2 if (command1 successful) then do (command2) $ ls s* & rm s* command1 | command2 if (command1 not successful) then do (command2) $ cd /dir1 | echo cannot change to /dir1 copyright ibm corporation 2008operator:returns true, if:$string1 = $string2strings are identical$string1 != $strin
8、g2strings are not identical$number1 -eq $number2numbers are equal$number1 -ne $number2numbers are not equal-e $filefile exists-d $filefile is a directory-r $filefile is readable-w $filefile is writabletest commandthe test command allows you to test for a given condition: test expression or expressio
9、n or expression the test command evaluates the expression and returns true or false copyright ibm corporation 2008optional$ cat activeusage=$0 userid if $# -ne 1 then echo proper usage: $usage exit 1 fi if who | grep $1 /dev/null then echo $1 is active else echo $1 is not active fi exit$ cat check_u
10、serusage=$0 username if $# -ne 1 then echo proper usage: $usage exit 2 fi grep $1 /etc/passwd /dev/null if $? -eq 0 then echo $1 is a valid user exit 0 else echo $1 is not a valid user exit 1 fiif commandif condition is truethencarry out this set of actionselsecarry out these alternative actionsfi c
11、opyright ibm corporation 2008if $0 $1 exit -ne -eq $?activity: writing shell scripts copyright ibm corporation 2008read commandthe read command reads one line from standard input and assigns the values of each field to a shell variable.$ cat delfile# usage: delfileecho please enter the file name:rea
12、d nameif -f $name then rm $nameelse echo error: $name is not an ordinary filefi copyright ibm corporation 2008for variable in listdo command(s) done$ cat countfor var in file1 file2 file3do wc -l $vardone $ count 18 file1 20 file2 12 file3$ cat rm_tmpfor file in /tmp/*do echo removing $file rm $file
13、donefor loop syntax copyright ibm corporation 2008while expressiondo command(s)done$ cat informationx=1while $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 600donewhile loop syntax copyright ib
14、m corporation 2008reserved wordalias built-incommandfunction pathvariablequalifiedpathnamecommand search order copyright ibm corporation 2008path=/bin:/usr/bin:/etc:$home/bin:.ps1=$pwd = env=$home/.kshrcif -s $mail then mailfiecho enter terminal type (default:ibm3151):cread a if -n $a then term=$ael
15、se term=ibm3151fiecho it is now $(date) echo there are $(ps -e | wc -l) processes runningecho there are $(who | wc -l) users logged in export path env term ps1sample .profile copyright ibm corporation 2008checkpoint1. what will the following piece of code do?termtype=$termif $termtype != thenif -f /
16、home/team01/customized_script then /home/team01/customized_scriptelse echo no customized script available !fielseecho you do not have a term variable set !fi2. write a script which will accept two arguments, multiply them together, and display the result. copyright ibm corporation 2008checkpoint sol
17、utions1. what will the following piece of code do?termtype=$termif $termtype != thenif -f /home/team01/customized_script then /home/team01/customized_scriptelse echo no customized script available !fielseecho you do not have a term variable set !fithe script will set a variable termtype to the value
18、 of the term variable. in the if statement the termtype variable will be tested to see if it is not empty. if it is not, then a second check will be carried out to ensure that the /home/team01/customized_script file is an ordinary file. if it is then it will be executed. (for our example we will ass
19、ume that this file contains some extra customized features.) if this file is not an ordinary file, then a message will be sent to the user stating this. if the initial test fails, that is, the termtype variable is empty, then again a message will be sent to the user.2. write a script which will accept two arguments, multiply them together
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高中地理2026教资面试全真模拟高频题考题
- 房地产外联课件
- 高中化学2026教资面试专项考题题库及答案
- 小学英语2026教资面试全真模拟答辩
- 常见颈部肿块的护理
- 房地产经纪人培训课件
- 房地产分析课件
- 防冻安全教案课件
- 分层应用课件
- 2026年汽车维修工知识题库(附答案)
- 成人术中外周静脉输液技术操作及管理指南
- 2026年艺术史考试指南:中外艺术史重难点及全题型预测试题
- 2026粉末冶金开发高速钢刀片脱模剂适用性分析报告
- 水泥毯的施工方案(3篇)
- 三角函数压轴题专项训练-2026年高考数学 含答案
- 中医执业医师考试《针灸学》巩固题及答案
- 2026年辽宁省公安厅招聘警务辅助人员笔试试题(含答案)
- 2025年国家烟草专卖局、中国烟草总公司招录笔试题库附答案解析
- 医院收款室安全生产制度
- 金波童话阅读试题及答案
- 线描画课件完整版
评论
0/150
提交评论