《aix系统应用基础》15 additional shell featu_第1页
《aix系统应用基础》15 additional shell featu_第2页
《aix系统应用基础》15 additional shell featu_第3页
《aix系统应用基础》15 additional shell featu_第4页
《aix系统应用基础》15 additional shell featu_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

1、Course materials may not be reproduced in whole or in part without the prior written permission of IBM.5.04.1编辑ppt Unit 15Additional Shell Features编辑pptUnit ObjectivesAfter completing this unit, you should be able to: Pass positional parameters to shell scriptsUse the test commandUse the if statemen

2、tImplement interactive shell scriptsImplement loops within scripts编辑ppt$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 processImportant Shell Variables

3、编辑ppt$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 entered was $1echo Second Parameter entered was $2echo Third Parameter entered was $3$ para_script Good Day

4、 SydneyFirst Parameter entered was GoodSecond Parameter entered was DayThird Parameter entered was Sydney编辑ppthighlowPrecedenceThe expr Utilityl Use the expr utility to perform integer arithmetic.l expr offers the following operators: * multiplication / integer division % remainder + addition - subt

5、raction (also unary minus sign)编辑ppt$ var1=6$ var2=3 $ expr $var1 / $var22$ expr $var1 - $var23= Use ( ) to group expressions:$ expr ( $var1 + $var2 ) * 545 = Use command substitution to store the result in a variable: $ var3=$(expr $var1 / $var2)$ echo $var32expr Examples编辑pptConditional ExecutionT

6、he 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 /d

7、ir1编辑pptOperator: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 writabletest Co

8、mmandThe 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编辑pptoptional$ cat activeUSAGE=$0 userid if $# -ne 1 then echo Proper Usage: $USAGE exit 1 fi if who | grep $1 /dev/null then

9、 echo $1 is active else echo $1 is not active fi exit$ cat check_userUSAGE=$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

10、out this set of actionselsecarry out these alternative actionsfi编辑pptif $0 $1 exit -ne -eq $?Activity: Writing Shell Scripts编辑pptread 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

11、 file name:read nameif -f $name then rm $nameelse echo Error: $name is not an ordinary filefi编辑pptfor 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 $FILEdonefor Loop

12、 Syntax编辑pptwhile 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编辑pptReserved wordalias Built-incommandFunction PAT

13、HvariableQualifiedpathnameCommand Search Order编辑pptPATH=/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=$aelse TERM=ibm3151fiecho It is now $(date) echo There are $(ps -e | wc -l) processes runninge

14、cho There are $(who | wc -l) users logged in export PATH ENV TERM PS1Sample .profile编辑pptCheckpointWhat 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 Y

15、ou do not have a TERM variable set !fiWrite a script which will accept two arguments, multiply them together, and display the result.编辑pptCheckpoint Solutions What will the following piece of code do?TERMTYPE=$TERMif $TERMTYPE != thenif -f /home/team01/customized_script then /home/team01/customized_

16、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 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 c

17、arried 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 thi

18、s. 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 the result. expr $1 * $2编辑pptExercise: Additional Shell Features编辑pptUnit Summary Positional parameters are used to pass to scripts the command line arguments. To test for a particular condition the test command can be used. The test feature can be coupled with the if statement

温馨提示

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

评论

0/150

提交评论