1 / 32

Chapter Seven

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

Download Presentation

Chapter Seven

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter Seven Advanced Shell Programming

  2. Lesson A Extending the Phonelist Program

  3. 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

  4. 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

  5. Script parameters • Command line invocation may list parameters • Example: myscript one two three • Available inside scripts as $1, $2, $3

  6. Parameter example

  7. Script Parameters

  8. More Script Parameters

  9. 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

  10. 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

  11. 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 $?

  12. Relational Integer Tests

  13. String Tests

  14. Testing Files

  15. Performing Boolean Tests

  16. 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

  17. 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

  18. Lesson B Completing the Case Project

  19. Objectives • Quoting • Capturing output of commands • Set up a quick screen-clearing technique • Add to the phone application • Delete records • …

  20. 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”

  21. 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

  22. 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

  23. Deleting Phone Records The menu has been updated to allow for deleting a phone record

  24. Deleting Phone Records Examine the corp_phones file before deleting a record

  25. Deleting Phone Records The sed command is behind the delete option

  26. Deleting Phone Records The record is no longer in the file

  27. Shell functions • Used to group recurring code • Syntax: functionname() { commands }

  28. Shell function example datenow() { date } it=`datenow` echo $it

  29. 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

  30. 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

More Related