A Guide to Unix Using Linux Third Edition - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

A Guide to Unix Using Linux Third Edition

Description:

Perform program design and analysis using flowcharts and pseudocode ... corp_phones (overlaying it) A Guide to Unix Using Linux, Third Edition. 23. Clearing the Screen ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 34
Provided by: steves72
Category:

less

Transcript and Presenter's Notes

Title: A Guide to Unix Using Linux Third Edition


1
A Guide to Unix Using Linux Third Edition
  • Chapter 7
  • Advanced Shell Programming

2
Objectives
  • Perform program design and analysis using
    flowcharts and pseudocode
  • Use techniques to ensure a script is employing
    the correct shell
  • Set the default shell
  • Configure Bash login and logout scripts
  • Set defaults for the vi editor
  • Use the test command for programming functions

3
Objectives (continued)
  • Format record output
  • Delete records using a script
  • Set up a quick screen-clearing technique
  • Create a program algorithm to solve a
    cursor-repositioning problem
  • Develop and test a program to eliminate duplicate
    records
  • Create shell functions and use them in a program

4
Understanding Program Design and Analysis
  • Design process
  • Second step in program development cycle
  • After creating the specifications for program
  • Develop computer program by analyzing best way to
    achieve desired results
  • Two popular and proven analysis tools
  • Flowchart
  • Pseudocode

5
Flowcharting
  • Flowchart
  • Logic diagram
  • Uses set of standard symbols
  • Visually explains sequence of events from start
    of process to its end point
  • Documents
  • Processes
  • Procedures
  • Program sequence

6
Flowcharting (continued)
7
Writing Pseudocode
  • After creating a flowchart, you write pseudocode
  • Creates a model that you can later use as a basis
    for a real program

Display "What is your favorite vegetable? " on
the screen Enter data into veg_name If veg_name
is equal to "broccoli" Then Display "Broccoli
is a healthy choice." on the screen Else
Display "Dont forget to eat your broccoli also."
on the screen End If
8
(No Transcript)
9
Ensuring the Correct Shell Runs the Script
  • UNIX/Linux users can choose their shell of
    preference
  • To ensure that the correct shell is used to
    interpret your script
  • Include command that sets the particular shell to
    use on the first line of the script
  • !/bin/bash

10
Setting the Default Shell
  • Shell set up by default is established by system
    administrator
  • /etc/passwd file
  • trbrownx500500Thomas Brown/home/trbrown/bin/
    bash
  • To edit /etc/passwd file
  • Use vi or Emacs
  • Must be very careful!
  • Make a backup copy first
  • Use a GUI
  • e.g., User Manager tool in GNOME desktop, YaST

11
Setting the Default Shell (continued)
12
Using Bash Login and Logout Scripts
  • In Bash, two scripts run when you log in
  • .bash_profile and .bashrc
  • Edit files with a text editor
  • Unlike .bash_profile, .bashrc is also run each
    time you start a subshell
  • /etc/.bashrc, /etc/bashrc, or /etc/bash.bashrc
    files set system defaults
  • Not always available
  • .bash_logout file (in your home directory)
    executes commands when user logs out

13
Using Bash Login and Logout Scripts (continued)
14
Setting Defaults for Using the vi Editor
  • .exrc used to automatically set up vi environment
  • Located in your home directory
  • Example

set number set tabstop3 set shell/bin/bash
15
Using the test Command
16
Performing Relational Integer Tests with the test
Command
  • Exit status numeric value that command returns
    to OS when it finishes
  • Interpreting exit status for test command
  • 0 (zero) ? test result is true
  • 1 ? test result is false
  • Use echo ? to view most recent exit status

17
Performing Relational Integer Tests with the test
Command (continued)
18
Performing String Tests with the test Command
  • These tests are useful in scripts to test
    contents of variables
  • Example ensure that a variable contains a
    specific value

19
Testing Files with the test Command
20
Performing Boolean Tests with the test Command
  • Boolean operator logical operator that
    symbolizes AND, OR, or NOT to evaluate a
    relationship
  • Result of evaluation is either true or false
  • Examples
  • test expression1 -a expression2
  • test expression1 -o expression2
  • test !expression

21
Formatting Record Output
  • To format record output, use translate utility,
    tr
  • Two examples
  • 1) tr a-z A-Z lt counters
  • Sends contents of counters file as input to tr
  • Then, converts lowercase characters to uppercase
  • 2) cat names tr "" " "
  • Sends output of cat to tr
  • Pipes () contents of names file to tr
  • tr replaces each occurrence of with a space

22
Deleting Phone Records
  • sed takes contents of input file and applies
    actions to files contents
  • Actions provided as options and arguments
  • Results are sent to standard output device
  • Simple way to delete a phone record
  • Use -d option

Enter phone number Use sed -d to delete the
matching phone number and output to a temporary
file, f Confirm acceptance If the output is
accepted, copy the temporary file f back
to corp_phones (overlaying it)
23
Clearing the Screen
24
Creating an Algorithm to Place
the Cursor
Read information into field2 While field2
equals "-" Move cursor to position of
previous field, field1 Clear current
information displayed in field1 Read new
information into field1 If field1 "q"
Then Exit program End If Move
cursor to position of field2 Read information
into field2 End While
tput cup 5 18 read lname while test "lname"
"-" do tput cup 4 18 echo " "
tput cup 4 18 read phonenum tput cup 5
18 read lname done
25
Protecting Against Entering Duplicate
Data
  • A program should always check its input to ensure
    the user has entered acceptable information
  • Input validation

26
(No Transcript)
27
Using Shell Functions
  • Shell function group of commands stored in
    memory and assigned a name
  • Shell scripts can use function name to execute
    the commands
  • Use shell functions to isolate reusable code
    sections
  • Reduce typing and debugging time
  • Example
  • datenow()
  • date

28
Defining a Function from the Command Line
  • You can define functions from the command line
  • However, functions are usually stored in script
    files
  • Loaded into memory when you log in
  • Arguments are passed to functions in the same
    manner as any other shell procedure

martin_at_localhost datenow() ltEntergt gt
ltEntergt gt date ltEntergt gt ltEntergt martin_at_localho
st
martin_at_localhost datenow "Todays date and
time are" Todays date and time are Mon Feb 9
214945 MST 2009
29
Creating Functions Inside Shell Scripts
  • Example a shell script called .myfuncs
  • You may start .myfuncs from your .bash_profile or
    .bashrc login script, or the command line

sort_name() sort -k 2 -t corp_phones sort_job
() sort -k 6 -t corp_phones sort_dept() sor
t -k 5 -t corp_phones
30
Troubleshooting a Shell Script
  • Some tips to help you troubleshoot a script
  • Ensure script has execute permissions
  • Be certain first line of script specifies shell
    to use
  • Use the sh -n, -v, and -x troubleshooting options
  • Look for typographic errors
  • Look for errors in the use of particular
    characters
  • , , , , , , lt, gt
  • Check for syntax errors in the use of commands
  • Look for the use of command options that are not
    supported in your distribution of UNIX/Linux
  • Check initial and exit value of looping logic

31
Summary
  • Two most popular and proven analysis tools
    flowchart and pseudocode
  • Have the first line in a script file specify the
    shell
  • Use test to validate the existence of directories
    and files as well as to compare numeric and
    string values
  • tr changes characters typed at keyboard
  • sed reads a file as its input and outputs the
    files modified contents
  • Shell functions can make programmer more efficient

32
Command Summary
33
Command Summary (continued)
Write a Comment
User Comments (0)
About PowerShow.com