




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.0 4.1 Unit 15 Additional Shell Features Copyright IBM Corporation 2008 Unit Objectives After completing this unit, you should be able to: Pass positional parameters to shell scripts Use the test command Use the if statement Implement interactive shell scripts Implement 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 the last command $!Process ID of last background process Important Shell Variables Copyright IBM Corporation 2008 $0$1$2$3 Positional Parameters Parameters can be passed to shell scripts as arguments on the command line: $1, $2, . $9 $10, $11, . $n (Korn Shell only) $ cat para_script echo First Parameter entered was $1 echo Second Parameter entered was $2 echo Third Parameter entered was $3 $ para_script Good Day Sydney First Parameter entered was Good Second Parameter entered was Day Third Parameter entered was Sydney Copyright IBM Corporation 2008 high low Precedence The expr Utility l Use the expr utility to perform 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 / $var2 2 $ expr $var1 - $var2 3 = Use ( ) to group expressions: $ expr ( $var1 + $var2 ) * 5 45 = Use command substitution to store the result in a variable: $ var3=$(expr $var1 / $var2) $ echo $var3 2 expr Examples Copyright IBM Corporation 2008 Conditional Execution The exit value from a command or group of commands can be used to determine whether to do the next command: command1 & 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 2008 Operator:Returns true, if: $string1 = $string2Strings are identical $string1 != $string2Strings 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 writable test Command The test command allows you to test for a given condition: test expression or expression or expression The test command evaluates the expression and returns true or false Copyright IBM Corporation 2008 optional $ cat active USAGE=“$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_user USAGE=“$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 fi if Command if condition is true then carry out this set of actions else carry out these alternative actions fi Copyright IBM Corporation 2008 if $0 $1 exit -ne -eq $? Activity: Writing Shell Scripts Copyright IBM Corporation 2008 read Command The read command reads one line from standard input and assigns the values of each field to a shell variable. $ cat delfile # Usage: delfile echo “Please enter the file name:“ read name if -f $name then rm $name else echo “Error: $name is not an ordinary file“ fi Copyright IBM Corporation 2008 for variable in list do command(s) done $ cat count for var in file1 file2 file3 do wc -l $var done $ count 18 file1 20 file2 12 file3 $ cat rm_tmp for FILE in /tmp/* do echo “Removing $file“ rm $FILE done for Loop Syntax Copyright IBM Corporation 2008 while expression do command(s) done $ cat information x=1 while $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 600 done while Loop Syntax Copyright IBM Corporation 2008 Reserved word alias Built-in command Function PATH variable Qualified pathname Command Search Order Copyright IBM Corporation 2008 PATH=/bin:/usr/bin:/etc:$HOME/bin:. PS1=$PWD = ENV=$HOME/.kshrc if -s “$MAIL“ then mail fi echo “Enter Terminal Type (Default:ibm3151):c“ read a if -n “$a“ then TERM=$a else TERM=ibm3151 fi echo “It is now $(date) “ echo “There are $(ps -e | wc -l) processes running“ echo “There are $(who | wc -l) users logged in“ export PATH ENV TERM PS1 Sample .profile Copyright IBM Corporation 2008 Checkpoint What will the following piece of code do? TERMTYPE=$TERM if $TERMTYPE != “ then if -f /home/team01/customized_script then /home/team01/customized_script else echo No customized script available ! fi else echo You do not have a TERM variable set ! fi Write a script which will accept two arguments, multiply them together, and display the result. Copyright IBM Corporation 2008 Checkpoint Solutions What will the following piece of code do? TERMTYPE=$TERM if $TERMTYPE != “ then if -f /home/team01/customized_script then /home/team01/customized_script else echo No customized script available ! fi else echo You do not have a TERM variable set ! fi The script will set a variable TERMTYPE to the value 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 assume 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. Write a script which will accept two arguments, multiply them together, and display th
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 法院司法调解协议书范本
- 汉沽区劳务派遣合同范本
- 火锅店长期员工合同协议
- 职工交通事故死亡协议书
- 用木桩护坡施工合同范本
- 海城农村房屋继承协议书
- 物流服务运输合同协议书
- 锁具维修合同协议书模板
- 爆破工程联营合同协议书
- 私人租地建养殖合同范本
- 加油站消防安全知识培训
- 2025年中小学公开选拔校长笔试模拟试卷
- 垂钓大赛活动方案
- 5G-A通感一体测评方法白皮书 2025
- 小儿轮状病毒性肠炎护理
- 科技项目经费管理制度
- 光伏系统的成本控制与经济性分析
- 风电场运行人员配置方案及费用
- 2025-2030年中国全自动建筑机器人行业市场现状供需分析及投资评估规划分析研究报告
- 2025年内蒙古煤炭建设工程(集团)总公司招聘笔试参考题库含答案解析
- 有理数加减混合运算练习题300道-可打印
评论
0/150
提交评论