Processes, Shell Scripts, Variables, and Command Line Substitution - PowerPoint PPT Presentation

1 / 82
About This Presentation
Title:

Processes, Shell Scripts, Variables, and Command Line Substitution

Description:

Activity: Writing Shell Scripts. KEY CONCEPTS: ... Made shell scripts executable ... Shell scripts and executable programs. Stored on disk ... – PowerPoint PPT presentation

Number of Views:117
Avg rating:3.0/5.0
Slides: 83
Provided by: KathrynM88
Category:

less

Transcript and Presenter's Notes

Title: Processes, Shell Scripts, Variables, and Command Line Substitution


1
Chapter 9
  • Processes, Shell Scripts, Variables, and Command
    Line Substitution

2
Overview
  • Will discuss the role of the shell and its
    responsibilities
  • The purpose and function of a process will be
    explained

3
Overview
  • Will learn commands to look at and interpret, to
    control, and to terminate processes
  • Will learn how to run commands in the foreground
    and in the background
  • Will learn how to write and execute a shell
    script

4
Overview
  • Will learn how to create and delete variables
    and make variables available to child shells
  • The effective use of quoting when entering
    commands will be discussed and then will use
    quoting with variables

5
Overview
  • Will learn the purpose and function of the type,
    help, dirs, popd, and pushd commands
  • The function of command line expansion and the
    order in which the shell processes command line
    expansions will be explained

6
The Shell
  • Can issue commands from shell prompt or by
    starting GUI and using startx command

7
The ShellFigure 9.1 The Linux System p. 481
8
The Shell
  • Kernel
  • Responsible for resource allocation and low
    level hardware interfaces
  • Loads first/remains in main memory till computer
    is turned off
  • Services requested of other parts of OS or by
    application programs known as system calls

9
The Shell
  • Kernel
  • Kernel includes
  • Interrupt Handler
  • Scheduler
  • Supervisor
  • Manager
  • Utilities
  • Commands that reside on disk
  • Brought into memory when needed

10
The Role of the Shell
  • Shell Responsibilities
  • Execution of Program
  • Internal and External (forking) commands
  • Ambiguous file references and variable
    substitution
  • Redirection
  • Pipes
  • Controlling the environment
  • Interpreted Programming Language

11
Process Structure
  • Process
  • An executing program
  • OS uses PID number to track every process
  • Structure is hierarchical
  • init (root)
  • Parent to all processes
  • Created when initialize system
  • All other processes created by fork and exec
    commands
  • Referred to as system routines or system calls

12
Process StructureFigure 9.2 fork and exec p. 483
13
Process Structure
  • Process
  • Structure is hierarchical
  • init (root)
  • getty
  • login
  • Chosen shell loaded
  • Presented with command prompt
  • Processes commands
  • Log-off
  • init starts a new getty

14
Process StructureFigure 9.3 The Login Cycle p.
484
15
Processes
  • A multitasking operating system can run
    processes concurrently or in parallel
  • Issuing a command starts a new process
  • Consists of program code and private data
  • May have other associated resources
  • OS must create environment for program to run in

16
Processes
  • Processes can assume different states R, S, T,
    and Z
  • Launch shell when log in successfully
  • Shell can spawn (create) other processes

17
ProcessesFigure 9.4 Shell and Command Execution
p. 485
18
Processes
  • To see what processes are running use
  • psreports on active processes
  • pstreeSee what processes are running and what
    processes are child process
  • uptimereport current time, amt.. of time logged
    in, of users logged in and system load averages
  • topDynamic (combines output of several
    commandsused to sort processes by percentage of
    processor time they use)

19
ProcessesTable 9.1 ps Options p. 486
20
ProcessesTable 9.1 ps Options p.486
21
ProcessesTable 9.2 ps top Command Line Options
p. 487
22
ProcessesTable 9.3 Interactive top Options p.
487
23
ProcessesTable 9.3 Interactive top Options p.
487
24
Activity Looking at Processes
  • KEY CONCEPTS
  • Function of ps command
  • Kernel manages resource systemall else that
    runs on system is a process
  • Shell creates new process each time command is
    entered by forking
  • Results of using hyphens before an option
  • Purpose and function of parsing
  • How wait state (system call) works

25
Activity Looking at Processes
  • KEY CONCEPTS
  • Purpose/function of init
  • How pstree command works
  • Used uptime to see what load is on system
  • Function of load average
  • Used M command to sort display by memory usage

26
Activity Looking at Processes Table 9.4 ps
Descriptions pp. 489-490
27
Activity Looking at Processes Figure 9.5 System
Calls p. 491
28
kill and killall commands
  • To cause process to end or terminate
  • Key in ltCtrlgt C
  • All processes sharing terminal are affected
  • Use kill command
  • Sends signal to terminate a process
  • If no signal specified TERM signal is sent
  • Process can ignore signal or begin orderly shut
    down process

29
kill and killall commands
  • To cause process to end or terminate
  • Use kill command
  • Root user can kill any process
  • User can kill processes that user id owns
  • Syntax kill option pid
  • Use killall command
  • Kills a process by name rather than by process
    id number

30
kill and killall commandsTable 9.5 kill Options
p. 493
31
kill and killall commandsTable 9.6 killall
Options p. 494
32
Activity Using kill and killall Commands with
Processes
  • KEY CONCEPTS
  • When process killed SIGTERM (15) sent
  • The find command used for seeking files
  • The ps command displays only processes in
    current terminal window
  • The killall command can be used with process name

33
The fg, bg, and jobs commands
  • Command run in foreground must be completed
    before shell returns to prompt
  • Command running in background can continue
    running while other commands are run

34
The fg, bg, and jobs commands
  • Can place command in background
  • by appending (ampersand) to end of command
  • that is running in foreground
  • Suspend currently running job to regain control
    of terminal (ltCtrlgt Z)
  • then use bg command

35
The fg, bg, and jobs commands
  • Command placed in background is assigned, by
    the shell, a job number
  • Job number is different than a process number
  • A job is a command line that was executed

36
The fg, bg, and jobs commands
  • Can bring background jobs to foreground
  • If one background job running use fg
  • More than one background running
  • fg (most recent)
  • fg - (next most recent)
  • Another command
  • fg command name
  • fg job number

37
Activity Using the fg, bg, and jobs commands
  • KEY CONCEPTS
  • The tty command used to print and change
    terminal line settings
  • Z displayedpressing ltCtrlgt Z will suspend
    job
  • Learned how to bring job in background to
    foreground
  • Determined which was the most recent job and the
    last job

38
Activity Using the fg, bg, and jobs commands
  • KEY CONCEPTS
  • Cannot use interrupt key to abort a process
    running in background
  • Use kill command to end these processes
  • In X Window System environment in GNOME, desktop
    can open multiple terminal windows and run a long
    job in one window and work in another window

39
Shell Scripts
  • Shell script
  • File containing commands that can be executed by
    the shell
  • Any command run at command line can be placed in
    a shell script

40
Shell Scripts
  • Executing a shell script
  • Give name on command line
  • If shell script created in an editoruse chmod
    to make file executable

41
Activity Writing Shell Scripts
  • KEY CONCEPTS
  • Saw how shell scripts work and placed different
    scripts in background foreground
  • Shells first jobdetermine which program to
    execute, then locate program on disk to execute
    it
  • Anything that is hidden is preceded by a .
    (period)
  • Made shell scripts executable
  • When commands are place in background do not
    need to separate commands with a (semi-colon)

42
Activity Writing Shell Scripts
  • KEY CONCEPTS
  • The following job number - current job
  • The - following job number - previous job
  • Can group commands with parentheses
  • Shell creates copy of itself for each group
  • Treats each group as a job
  • Creates a new process to execute each of the
    commands
  • Can execute a program without it being in search
    path
  • Preface command with a period followed by a
    forward slash, then the command

43
Command Types
  • Order in which commands are executed
  • aliases
  • keywords
  • functions
  • built-in commands
  • executable programs

44
Command Types
  • Shell scripts and executable programs
  • Stored on disk
  • Shell locates them via search path to execute
    them

45
Command Types
  • Use type command to see what kind of command you
    are using
  • Use help command to see purpose of a built-in
    command
  • Use alias command to create shortcuts to
    commonly used commands

46
Activity Using alias/type/help
  • KEY CONCEPTS
  • Can create own aliases
  • To capture entire command use quotation marks
  • Press ltCtrlgt C to return to command prompt
  • Use unalias command followed by alias to remove
    it from list
  • Used type with a command
  • To retrieve commands quickly use
  • Hashing
  • Type command with -path option
  • Used a man page for a built-in command
  • Used help with command to get quick description

47
Activity Using alias/type/help
  • KEY CONCEPTS
  • Use man with command for full tutorial of
    command
  • Conditional statements control flow of program
  • Decision making based on whether expression
    true/false
  • Exit status also called condition or return code
  • Shell stores exit status of last command in ?
    variable
  • Zero status - command successful
  • Not zero statusfalse valuecommand failed
  • Aliases kept in .bashrc file from one terminal
    session to another

48
Manipulating the Directory Stack with Built-in
Commands
  • Stack
  • A list of directories that are used often and
    stored by the user for convenience

49
Manipulating the Directory Stack with Built-in
Commands
  • To manipulate a stack use
  • pushd command
  • Syntax pushd dir N -N
  • popd command
  • Syntax popd N -N
  • dirs command
  • Syntax dirs clpv N -N

50
Manipulating the Directory Stack with Built-in
CommandsTable 9.7 pushd Options p. 512
51
Manipulating the Directory Stack with Built-in
CommandsTable 9.8 popd Options p. 512
52
Manipulating the Directory Stack with Built-in
CommandsTable 9.9 dirs Options p. 512
53
Activity Using the Built-in Commands dirs,
pushd, and popd
  • KEY CONCEPTS
  • Used pushd command with
  • directory name
  • no options
  • n and -n options
  • negative numbers
  • Used dirs with -l and with -v options
  • Used popd with no options and option

54
Variables and the set Command
  • Variables
  • Contain information used for customizing
  • Information required by other processes
  • Can contain mix of letters, numbers, or
    underscores
  • Never begins with a number

55
Variables and the set Command
  • Two types of variables
  • Local
  • Private to shell they are created in
  • Namesusually lower case letters
  • Environmental
  • Passed from parent to child process
  • Namesusually upper case letters

56
Variables and the set Command
  • Shell has number of predefined options that can
    be enabled or disabled

57
Variables and the set CommandTable 9.10 Common
Shell Modes p. 517
58
Activity Using set Command
  • KEY CONCEPTS
  • noclobber
  • Set to offcan overwrite existing file with
    redirection
  • Set to oncannot overwrite a file
  • Use cp to overwrite file even with noclobber on
  • Use man bash to see defined list of commonly
    used options
  • When shell (terminal window) exited new shell
    openedoptions return to default value

59
User-created Variables
  • Rules for creating variables
  • Can be named anything except first character
    cannot be a number
  • Assigned values must not precede or follow equal
    sign with blank space or tab mark
  • Case sensitive
  • Settings remain only for that work session

60
User-created Variables
  • echo Command
  • Can see value of set variable using echo with
    and name of variable
  • Syntax variablenamevalue

61
Activity Creating/Removing User-created Variables
  • KEY CONCEPTS
  • Echo command copies argument supplied to
    standard output
  • To see value of requested variable name key in
    preceding command
  • Use double quotation marks
  • Turn off any special meanings of other
    characters
  • To preserve any spacing considerations

62
Activity Creating/Removing User-created Variables
  • KEY CONCEPTS
  • Escape sequence used to control display devices
  • Using variable as an argument with a command -
    shell replaces name of variable with value of
    variable and passes it to executing program
  • Shell parses a command line in a specific order
  • Variables created remain until terminal window
    exited
  • May remove value of variables at any time during
    a work session

63
Activity Creating/Removing User-created
VariablesTable 9.11 Escape Sequences pp. 520-521
64
export and declare
  • export and declare are built-in commands used to
    create environmental environments

65
export and declare
  • Environmental variables available
  • In shell in which they were created
  • Any subshell/process spawned from parent shell
  • Environmental variables can be passed
  • To any child process started from shell where
    environmental variables created
  • To any other shells spawned from that child

66
export and declare
  • Syntax for export is export variablevalue
  • Syntax for declare is
  • declare -x variablenamevalue
  • Syntax to display all variable names exported in
    shell is export or export -p

67
Activity Using export and declare
  • KEY CONCEPTS
  • To export values use declare -x
  • To display current running pid use echo
  • Spawned shell by starting second Bash shell
  • To pass environmental variable to a child
    process must export or declare it
  • Can change value of environmental variable
  • Can pass value to a child shell but not to a
    parent

68
Command Line Expansion and Quoting
  • Command line expansion is what the shell does to
    command line before passing it onto program that
    is being called

69
Command Line Expansion and Quoting
  • Before executing a command shell
  • Parses command into separate parts and
    interprets each part
  • Looks for special characters and patterns within
    command line

70
Command Line Expansion and Quoting
  • Three types of quote characters that the shell
    recognizes and interprets
  • Single quote
  • Double quote
  • Back quote
  • Backslash character is the same as
  • putting quotes around character

71
The Single Quote, the Double Quote, and the
Backlash
  • Whenever shell sees a blank it interprets the
    next item in command line as an option or an
    argument

72
The Single Quote, the Double Quote, and the
Backlash
  • Single quotes and back quotes stop all types of
    expansions
  • Double quotes tells shell to ignore most special
    characters in the quoted statement
  • Does not ignore dollar sign, backslashes, back
    quotes

73
The Single Quote, the Double Quote, and the
Backlash
  • Backslash tells shell not to interpret what
    follows the backslash

74
Activity Single Quotes, Double Quotes, and
Backslash
  • KEY CONCEPTS
  • Results of pressing ltSpacebargt five times
  • Enclose name in quote marks tells shell to treat
    name as one argument
  • Created variable to see how quote marks work
  • For shell to interpret and treat it as a
    character, use double quotation marks
  • Learned two methods of keying in contractions so
    that will be read as one word
  • When backslash is last character on a line it
    allows you to continue a line

75
More on Command Line Expansion
  • Order in which shell processes a command
  • Parses command
  • Command broken into separate words
  • Examines each word for special characters that
    tells shell to take certain actions
  • Performs any command line expansions
  • Executes command

76
More on Command Line Expansion
  • Order shell processes command line expansion
  • brace expansion
  • tilde expansion
  • parameter expansion
  • variable substitution
  • command substitution
  • arithmetic expansion
  • word splitting
  • pathname expansion

77
Activity More on Command Line Expansion
  • KEY CONCEPTS
  • Curly braces
  • means of expansion
  • characters that precede curly braces called
    preambles
  • that follow curly braces are called postambles
  • cd changed directories to last working
    directory
  • Can test and modify variables when special
    modifiers are used
  • Modifier can check to see if variable is set and
    then assigns a value to variable

78
Activity More on Command Line Expansion
  • KEY CONCEPTS
  • Use command substitution to use standard output
    of command in shell script as well as on command
    line
  • All shells use back quotes to perform command
    substitutions
  • Remember to use curly braces
  • Back quote usually lowercase position on key
  • Can perform arithmetic expansions and other
    various mathematical calculation
  • Word splittinginterpretation of command line

79
Activity More on Command Line ExpansionTable
9.12 Special Modifiers pp. 531-532
80
The Initialization Files and Sourcing
  • Sourcing causes file settings to become part of
    the current shell and a subshell is not created

81
The Initialization Files and Sourcing
  • Source (dot) command
  • Built-in command
  • Takes script name as an argument
  • Used to reexecute any of the initialization
    files if they have been modified

82
Activity Altering the .bashrc File
  • KEY CONCEPTS
  • Altered .bashrc file and added aliases and
    environmental variables
  • Items in file preceded by a are comments and
    will not be executed
  • Until file is executed none of changes have
    taken effect
  • Because set noclobber oncannot use redirection
    to overwrite a file
  • Could overwrite file with cp command
Write a Comment
User Comments (0)
About PowerShow.com