320 likes | 417 Views
Chapter Seven. Advanced Shell Programming. Lesson A. Extending the Phonelist Program. Objectives. Learn to write scripts that tell the system which shell to use as an interpreter Learn about script parameters Learn about numeric variables
E N D
Chapter Seven Advanced Shell Programming
Lesson A Extending the Phonelist Program
Objectives • Learn to write scripts that tell the system which shell to use as an interpreter • Learn about script parameters • Learn about numeric variables • Use the test command to compare values and validate file existence • Use the sed command in a script to delete phone records
Ensuring the Correct Shell Runs the Script • In Unix there are more than one shell ! • examples: bash, tcsh, ksh • need to ensure that correct shell runs the script • different shells use different programming constructs • Convention: first line of a script states which executable should run script • Example: #! /bin/bash
Script parameters • Command line invocation may list parameters • Example: myscript one two three • Available inside scripts as $1, $2, $3
Numeric variables • Let command defines integer variables with numeric values • Example: let i=1 let i=5*i-2 • Let command allows simple numeric calculations
Using the test Command • Bash shell uses test command to: • perform relational tests with integers • test and compare strings • check if a file exists and what type of file it is • perform Boolean tests • Test command is standard Unix command • Is used in bash for if and while statements
Using the test command • Syntax: test some-expression [ some-expression ] • Indicates result via exit status • 0 is true, 1 is false • To get exit status: echo $?
Using the test Command The content of this file was created in part due to using the test command to determine if a directory existed
Using the test Command The output shown here was created in part due to using the test command to determine a value entered by a user
Lesson B Completing the Case Project
Objectives • Quoting • Capturing output of commands • Set up a quick screen-clearing technique • Add to the phone application • Delete records • …
Quoting • Double quotes example: text=“hello world” echo “here it is: $text” • Single quotes example: text=‘one two three’ echo “here it is: $text” • Execution quote example: text=`date` echo “here it is: $text”
Clearing the Screen • clear command queries terminal for escape sequence • Faster: • store the output of clear • echo to the screen • Example: CLEAR=`clear` echo $CLEAR
Stream editor: sed • Can be used to remove lines from a file • Syntax: sed ‘/pattern/d’ file1 > file2 • Removes all lines which match the pattern from file1
Deleting Phone Records The menu has been updated to allow for deleting a phone record
Deleting Phone Records Examine the corp_phones file before deleting a record
Deleting Phone Records The sed command is behind the delete option
Deleting Phone Records The record is no longer in the file
Shell functions • Used to group recurring code • Syntax: functionname() { commands }
Shell function example datenow() { date } it=`datenow` echo $it
Shell function parameters checkfile() { echo “Checking: $1” if [ -f “$1” ] then echo “$1 is a file” else if [ -d “$1” ] then echo “$1 is a directory” fi fi } checkfile phmenu
Chapter Summary • Use the first line in a script file to tell the OS which shell to use when interpreting the script • Use the test command to validate the existence of directories and files as well as compare numeric and string values • The sed command reads a file as its input and outputs the file’s modified content • Assign the clear command sequence to the shell variable CLEAR that can be set inside the login script • Shell functions can simplify program code by isolating code that can be reused throughout one or many programs