Date post: | 18-Nov-2014 |
Category: |
Documents |
Upload: | wannabeef9 |
View: | 910 times |
Download: | 2 times |
Chapter 3:Using the Shell
The Complete Guide to Linux System Administration
The Complete Guide to Linux System Administration 2
Objectives
• Use common features of the shell to work at the command line
• Manipulate variables in the shell to control your working environment
• Redirect data at the command line
• Edit text using the vi editor
• Print basic files from the command line
The Complete Guide to Linux System Administration 3
Exploring the Bash Shell
• Shell – Command program – Accepts input from keyboard – Uses input to run commands or otherwise control
computer– Started when terminal window opened
The Complete Guide to Linux System Administration 4
Exploring the Bash Shell (continued)
• Flexible– Interact with any file that you have permission to
access– Start any program on system– Use special features of shell to work more
efficiently
The Complete Guide to Linux System Administration 5
The Shell Prompt
• Set of words or characters indicating that shell is ready to accept commands
• Default prompt example:– [Nwells@inverness nwells]$
• When logged in as root, prompt character changes to hash mark, #
The Complete Guide to Linux System Administration 6
The Functions of a Shell
• Shell purpose– Make it easy for users to run programs and work
with files in Linux– Run programs– Pass parameters to program being started
• Behavior– If command succeeds, no response– If command fails, error message printed
The Complete Guide to Linux System Administration 7
The Functions of a Shell (continued)
• Shell has many built-in features – Help you work with files and commands
• Script – Program– List of commands stored in text file
The Complete Guide to Linux System Administration 8
Different Types of Shells
• Shell has no special relationship to Linux kernel– Can use any shell
• Bourne shell– Original shell for UNIX– Called sh– Old program with limited functionality
• Bash– Default shell for all Linux systems
The Complete Guide to Linux System Administration 9
Different Types of Shells (continued)
• Shells fall into two groups:– Bourne shell programming style– C shell programming style
• Shell started for each user is determined by settings in user account– Each user on system can select preferred shell
• Immediately run different shell– Enter name of shell program
The Complete Guide to Linux System Administration 10
Entering Commands
• Features designed to simplify process of entering commands:– Tab completion
• Enter part of file or directory name
• Press tab key
• Shell fills in remainder of name
– Command history• Records each command
The Complete Guide to Linux System Administration 11
Entering Commands (continued)
• Command history– Quickly call up and repeat any command from list – Press the up arrow key at command prompt– Press enter to execute command
• History command – Displays entire history list
• Bang– !
The Complete Guide to Linux System Administration 12
Entering Commands (continued)
• Locate previously executed command– Use history number– Use beginning of command– Search history list
The Complete Guide to Linux System Administration 13
The Shell Start-up Process
• Scripts executed when you log in to Linux– Initialize various parts of environment
• Script /etc/profile executed on first log in
• Contains configuration information that applies to every user on system
• Profile script– Found in user’s home directory– Specific to single user
The Complete Guide to Linux System Administration 14
The Shell Start-up Process (continued)
• Some systems – Additional scripts are executed when user logs in
• When user starts shell– Additional scripts executed
• bashrc script – Executed each time user starts bash shell– User can add configuration information
The Complete Guide to Linux System Administration 15
The Shell Start-up Process (continued)
The Complete Guide to Linux System Administration 16
Using Aliases
• Alias – String substituted for another string at shell prompt
• Alias command – Define how shell will substitute one string for
another – Format:
• alias <string entered by user>=<string substituted by the shell>
– Enter without arguments to see list of current aliases
The Complete Guide to Linux System Administration 17
Command-line Expansion
• Shell can make many different substitutions, including:– Aliases– Variables– Command substitution– Special characters
• Place commands in single quotes – Executed first by shell
The Complete Guide to Linux System Administration 18
Command-line Expansion (continued)
• Special characters– Place in single quotes– Precede with $
• Example: $’\n’
– Other special characters:• ;
• *
• ?
The Complete Guide to Linux System Administration 19
Command-line Expansion (continued)
• Avoid command-line expansion– Escape individual characters
• Using \
– Single quote– Double quote
The Complete Guide to Linux System Administration 20
Shell Variables
• Shell variable– Name that can have value assigned to it– Can be number or string of text– Typically created using all uppercase letters
• Environment variable – Typically defined during initialization of operating
system or user’s shell– Can be accessed by any program
The Complete Guide to Linux System Administration 21
Shell Variables (continued)
• env command – Start program with environment variable setting
that is not part of current environment– Only used for started program
• Set command– Displays list of all environment variables
• Echo command– View value of single environment variable
The Complete Guide to Linux System Administration 22
Shell Variables (continued)
• Export command – Makes newly created environment variable
available to other programs
• PATH environment variable – Contains list of directories that shell searches
each time command is executed
• PS1 environment variable– Defines shell prompt for bash
The Complete Guide to Linux System Administration 23
Data Redirection
• Standard input channel– STDIN– Keyboard
• Standard output channel– STDOUT– Monitor
• Standard error– STDERR
The Complete Guide to Linux System Administration 24
Data Redirection (continued)
• Data can be redirected from standard channels
• Pipe– Connects output channel of one command to the
input channel of another command– Example: $ ls /etc | sort
The Complete Guide to Linux System Administration 25
Data Redirection (continued)
The Complete Guide to Linux System Administration 26
Data Redirection (continued)
• Mail command – Basic e-mail client – Use from command line to create e-mail message– Format: mail -s "subject line" e-mail_address
The Complete Guide to Linux System Administration 27
Editing Text with vi• Text editor
– Single most important utility for system administrator
• Many different text editors are available for Linux
• Text-mode editors:– vi– emacs– pico– Joe
The Complete Guide to Linux System Administration 28
Editing Text with vi (continued)
• Very powerful program
• Available on virtually every Linux system
• Not easy to use
• Must learn to use at least basic features in order to work as Linux system administrator– Memorize keystroke sequences– Work without any onscreen prompts
The Complete Guide to Linux System Administration 29
Editing Text with vi (continued)
• To start:– vi command– Can be followed by name of file to edit
• Modal editor– Keystrokes are interpreted differently depending
on mode you are working in
The Complete Guide to Linux System Administration 30
Editing Text with vi (continued)
• Modes:– Command mode– Insert mode– Replace mode
• Begin in command mode– Return to command mode by pressing Esc key
The Complete Guide to Linux System Administration 31
Editing Text with vi (continued)
The Complete Guide to Linux System Administration 32
Editing Text with vi (continued)
The Complete Guide to Linux System Administration 33
Editing Text with vi (continued)
The Complete Guide to Linux System Administration 34
Editing Text with vi (continued)
The Complete Guide to Linux System Administration 35
Printing from the Command Line
• Quickly configure printer attached to parallel port
• Print files directly from command line
The Complete Guide to Linux System Administration 36
Setting Up a Printer
• Print queue – Printer definition– Associate physical printer with named printer
definition
• Set up printer via GNOME desktop interface – Choose system settings– Print– Follow steps in dialog
The Complete Guide to Linux System Administration 37
Printing From a Command Line
• lpr command– Print from command line– Uses series of filters based on type of file to print
• lpoptions command– Save commonly used list of options
The Complete Guide to Linux System Administration 38
Summary
• Shell – Command interpreter used to start programs– Many shells are available
• Tab completion – Helps to quickly enter long file and directory
names
• Command history – Quickly reenter previously entered command
The Complete Guide to Linux System Administration 39
Summary (continued)
• Scripts executed when:– User logs in– New shell opened
• Aliases– Shell replaces text on command-line entry with
different text
• Shell expands text on command line in several ways
The Complete Guide to Linux System Administration 40
Summary (continued)
• Environment variables store values that programs can access
• Flow of data between programs can be changed using redirection operators on command line
• Linux supports numerous text editors
• After setting up printer, print files from command line using lpr command