Download presentation
Presentation is loading. Please wait.
Published byJonathan Stanley Modified over 8 years ago
1
Chapter 16 Advanced Bourne Shell Programming
2
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing To describe how standard input of a command in a shell can be redirected from data within the script To explain the signal/interrupt processing capability of the Bourne shell To describe how file I/O can be performed by using file descriptors and how standard files can be redirected from within a shell script To explain the functions in the Bourne shell To discuss debugging of Bourne shell scripts To cover the commands and primitives
3
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Numeric Data Processing The values of Bourne shell variables are stored as character strings In order to perform arithmetic and logic operations on them, you need to convert them to integers and be sure that the result is converted back to a character string for its proper storage in a shell variable
4
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Numeric Data Processing expr args Purpose:Evaluate the expression arguments, ‘args’, and send the result to standard output Commonly used operators/features: \| return the first expression if it is not null or 0; else return the second expression \& Return the first expression if neither is null or 0;else return 0 =, \>, \>=, Integer comparison operators;equal, \<, \<=,!= greater than, greater than or equal to, less than,less than or equal to, not equal +, -, \*, /, % Integer arithmetic operators, add, subtract, multiply, integer divide (return quotient), remainder
5
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Numeric Data Processing $ var1=10 $ var1=`expr $var1 + 1` $ echo $var1 11 $ var1=`expr $var1 \* $var1` $ echo $var1 121 $ echo `expr $var1 / 10` 12 $ echo `expr $var1 % 10` 1 $
6
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Numeric Data Processing
7
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The Here Document The here document feature of the Bourne shell allows you to redirect standard input of a command in script and attach it to data in the script
8
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The Here Document
9
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The Here Document
10
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Interrupt (Signal) Processing Three possible actions that the process receiving the signal can take: –Accept the default action as determined by the UNIX kernel –Ignore the signal –Take a programmer defined action Hardware interrupt e.g keyboard interrupt ( ) Signal received due to termination of a child process Accessing a main memory location that is not part of the process’s address space Software termination
11
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Interrupt (Signal) Processing
12
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Interrupt (Signal) Processing
13
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The exec Command and File I/O The command level version of the UNIX loader Used : –To execute a command/program instead of the current process (under which exec is executed, usually a shell) –To open and close file descriptors When the exec command is used in conjunction with redirection operators, it allows commands and shell scripts to read/write any type of files, including devices
14
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Execution of a Command Without Creating a New Process exec command can be used to run a command instead of the process (usually a shell) that executes this command
15
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Execution of a Command Without Creating a New Process
16
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Execution of a Command Without Creating a New Process
17
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. File I/O via the exec Command Korn shell allows you to work with as many as 10 file descriptors at a time Three descriptors are set aside for: –Standard input(0) –Standard output(1) –Standard error(2) When executed from the command line, the exec< sample command causes each line in the sample file to be treated as a command and executed by the current shell When exec>data command is executed from command line, it causes outputs of all subsequent commands executed under the shell ( that normally go to the monitor screen) to go to the data file.
18
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. File I/O via the exec Command
19
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. File I/O via the exec Command
20
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. File I/O via the exec Command $ exec > data $ date $ echo Hello, World! $ uname -a $ exec > /dev/tty $ date Tue Jun 29 23:57:30 PDT 2004 $ cat data Tue Jun 29 23:56:03 PDT 2004 Hello, World! AIX pccaix 3 4 000001336700 $
21
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. File I/O via the exec Command
22
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Functions in Bourne Shell Functions are normally used if a piece of code is repeated at various places in a script The mechanism of transferring the control to the function code and returning it to the calling code takes time Another alternative is to create another script file for the block of code and invoke this code by calling the script as a command-expensive operation
23
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Functions in Bourne Shell Before you can use a function, you have to define it Put the definitions of general purpose functions in ~/.profile file Functions can be exported using the export command The commands in a function body are not executed until the function is invoked Can invoke a function by using its name as a command
24
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Functions in Bourne Shell $ machines () > { > date > echo “These are the machines on the network:” > ruptime | cut -f1 -d’ ‘ | more > } $ machines Thu Feb 19 17:05:00 PDT 2004 These are the machines on the network: upibm0... upsun1... upsun29 $
25
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Debugging Shell Programs The Bourne shell programs can be debugged by using the –x and –v options of the sh command This allows viewing the commands in the user’s script after the variable substitution but before execution
26
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Debugging Shell Programs
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.