Tải bản đầy đủ (.pdf) (68 trang)

Sun Fundamentals of Solaris 7 Student Guide With Instructor Notes phần 6 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (340.66 KB, 68 trang )

9
9-8 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Positioning Commands
The following pages list the vi editor’s editing and positioning
commands, which are used to make changes.
The vi editor is case sensitive, so use the specified case when using the
editing and positioning commands.
The Table 9-3 shows the key sequences for character movement:.
Table 9-3 Key Sequences
Command Meaning
h, ←, or Back Space Move left one character
j or ↓ Move down one line
k or ↑ Move up one line
l,→, or Space bar Move right (forward) one character
w Move forward one word (including punctuation)
W Move forward one word (past punctuation)
b Move back one word (including punctuation)
B Move back one word (past punctuation)
e Move to end of current word
$ Move to end of line
0 (zero) or ^ Move to beginning of line
Return Move down to beginning of next line
H Move to top of screen
M Move to middle of screen
L Move to bottom of screen
Control-f Page forward one screen
Control-d Scroll down one-half screen
Control-b Page back one screen
Control-u Scroll up one-half screen
9


Using the
vi
Editor 9-9
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Editing Commands
Deleting Text
To delete text, use the following options:
Table 9-4 Text Deletion Commands
Command Meaning
x (lowercase) Delete character at the cursor
X (uppercase) Delete character to the left of the cursor
dw Delete word (or part of word to right of cursor)
3dw Delete three words
dd Delete line containing the cursor
3dd Delete three lines
D Delete line to right of cursor (from cursor position to
the end of the line)
dG Delete to end of file
d1G Delete from beginning of file to cursor
:5,10d Delete lines 5 through 10
9
9-10 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Editing Commands
Undoing, Repeating, and Changing Text Commands
To change text or to cancel or repeat edit functions, use the following
commands. Many of these commands change you to Insert mode until
you press Escape.
Table 9-5 Editing Commands
Command Meaning

cw Change word (or part of word) at the cursor
location to the end of the word
3cw Change three words
R Overwrite or replace characters on line
C Change from cursor to end of line
s Substitute string for character(s)
r Replace character at cursor with one other character
i (Return) Break line
J Join current line and line below
xp Transpose character at cursor and character to the
right
~ Change case of letter (upper or lower) at cursor
u Undo previous command
U Undo all changes to current line
:u Undo previous last-line command
9
Using the
vi
Editor 9-11
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Editing Commands
Copying and Pasting Text
To copy and paste text, use the following options:
Note – Both delete and yank write to a buffer. When yanking, deleting,
and pasting, the put commands insert the text differently depending
on whether you are pasting a word(s) or a line(s).
Table 9-6 Copy and Paste Commands
Command Meaning
yy Yank a copy of line
Y Yank a copy of line

p Put yanked or deleted line below current line
P Put yanked or deleted line above current line
:1,3 co 5 Copy lines 1 through 3 and put after line 5
:4,6 m 8 Move lines 4 through 6 to line 8 (line 6 becomes line
8; line 5 becomes line 7, and line 4 becomes line 6)
9
9-12 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Editing Commands
Saving and Quitting Files
To save and quit a file, use the following options:
Table 9-7 Save and Quit Commands
Command Meaning
:w Save changes (write buffer)
:w
new_filename
Write buffer to
new_filename
:wq Save changes and quit vi
ZZ Save changes and quit vi
:q! Quit without saving changes
:wq! Save changes and quit vi (The ! will override read
only permissions if you are the owner of the file.)
9
Using the
vi
Editor 9-13
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Advanced Editing Options
The vi editor includes options for customizing your edit sessions,

such as:
● Displaying line numbers
● Displaying invisible characters such as tab and end-of-line
characters
The set command is used from last-line mode to control these options.
These options can also be placed in a file the user creates in their home
directory called .exrc. The set options are placed in this file, without
the preceding colon, one command to a line. Once the .exrc file exists,
it is read by the system each time a vi session is opened.
Table 9-8 Edit Session Customization Commands
Command Meaning
:set nu Show line numbers
:set nonu Hide line numbers
:set ic Instruct searches to ignore case
:set noic Instruct searches to be case sensitive
:set list Display invisible characters such as tab and
end-of-line
:set nolist Turn off the display of invisible characters
:set showmode Display current mode of operation
:set noshowmode Turn off mode display
:set Display all vi variables set
:set all Display all possible vi variables and their current
settings
9
9-14 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Advanced Editing Options
To find a line or to search and replace and do advanced editing, use
the following options:
To clear the screen or insert files, use the following options:

To search and replace text, use the following options:
Table 9-9 Advanced Editing/Search Commands
Command Meaning
G Go to last line of file
1G Go to first line of file
:21 Go to line 21
21G Go to line 21
Table 9-10 Clearing/Insertion Commands
Command Meaning
Control-L Clear (refresh) scrambled screen
:r
filename
Insert (read) file at line after cursor
:34 r
filename
Insert file after line 34
Table 9-11 Search and Replace Commands
Command Meaning
/
string
Search forward for
string
?
string
Search backward for
string
n Find next occurrence of string
N Find previous occurrence of string
:%s/
old

/
new
/g Search and replace globally
9
Using the
vi
Editor 9-15
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Creating and Saving a File
To create a file using the vi editor:
1. Type vi
filename
to create the file.
2. Type i to insert text.
3. Press the Escape key to change to command mode.
4. Type :wq to write the file and exit vi.
Note – The Escape key always puts you in command mode. Use the
Escape key if you are not sure what mode you are in. If you press the
Escape key while you are in command mode, the workstation beeps as
a reminder that you are already in command mode.
9
9-16 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Using the vi Editor
Exercise objective – In this exercise you will practice creating and
editing using vi.
Tasks
Complete the following step:
1. In your home directory there should be a file called tutor.vi.
Make sure that you are currently in your home directory, then

open this file with the command:
$ vi tutor.vi
this will open up a vi tutorial. Complete the lessons outlined in
this tutorial.
9
Using the
vi
Editor 9-17
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Using the vi Editor
Exercise Summary
Discussion – Take a few minutes to discuss what experiences, issues,
or discoveries you had during the lab exercises.

Manage the discussion here based on the time allowed for this module, which was given
in the “About This Course” module. If you find you do not have time to spend on
discussion, then just highlight the key concepts students should have learned from the
lab exercise.
● Experiences

Ask students what their overall experiences with this exercise have been. You might want
to go over any trouble spots or especially confusing areas at this time.
● Interpretations

Ask students to interpret what they observed during any aspects of this exercise.
● Conclusions

Have students articulate any conclusions they reached as a result of this exercise
experience.
● Applications


Explore with students how they might apply what they learned in this exercise to
situations at their workplace.
9
9-18 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Check Your Progress
Before continuing on to the next module, check that you are able to
accomplish or answer the following:
● Differentiate between the three modes of vi and identify the
commands that belong to each mode
● Start vi
● Position and move the cursor in vi
● Create text in vi
● Delete text in vi
● Copy or move text in vi
● Set vi options
● Exit the vi editor
● Perform search and replace functions within vi
9
Using the
vi
Editor 9-19
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Think Beyond
Under what conditions might it be necessary for you to use the vi
editor in your work environment?
10-1
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D

BourneandKorn ShellVariables
10
Objectives
Upon completion of this module, you should be able to:
● Set and unset shell and environment variables for the Bourne and
Korn shells
● Change the PATH environment variable for the Bourne and Korn
shells
● Use the which command to determine which version of a
command is being used
● Use the whereis command to search for instances of a command
on a system
10
10-2 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Relevance

Present the following question to stimulate the students and get them thinking about the
issues and topics presented in this module. While they are not expected to know the
answer to the question, the answer should be of interest to them and inspire them to learn
the content presented in this module.
Discussion – How would it be useful to customize Bourne and Korn
shells?
Additional Resources
Additional resources – The following references can provide
additional details on the topics discussed in this module:
● Solaris User’s Guide, Part Number 802-6499
● Solaris Advanced User’s Guide to System and Network Tasks,
Chapters 1 and 3, Part Number 801-4415-10.
● Man pages for sh(1), ksh(1), and csh(1)

10
Bourne and Korn Shell Variables 10-3
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Introduction
A variable is a placeholder for information to be used by the system or
user. Information such as the default printer or a pathname to a
directory can be set up as a variable.
Two categories of variables are discussed in the following section:
● Local (shell)
● Global (environment)
Two key environment variables are the PATH and ENV variables.
This module covers setting and unsetting shell and environment
variables. The focus will be on the Bourne and Korn shells. Appendix
A, ‘‘Features of the C Shell,” covers how variables are set in the C
shell.
10
10-4 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Shell Variables
Overview
When you first log in to the host, you are placed in a predefined shell.
If you type sh (Bourne shell), ksh (Korn shell), or csh (C shell) on the
command line, a subshell is created. This process can be repeated to
create additional shells. To change to the previous shell, type exit.
Shell variables (local and global) can be either user-defined or built-in,
and can be customized by the user or predefined by the system.
Initially, when a variable is created, it is only available to its shell of
origin. This is a local variable. If a new subshell is created, the
variables created in the parent shell are not available. However, the
parent shell is still running, and when the subshell is exited, the

variables will be available again. When the shell where the variables
were created is exited, the variables of that shell are terminated. Local
variables are available only to the specific shell where they are created.
10
Bourne and Korn Shell Variables 10-5
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Shell Variables
Overview
To make a local variable available in all subshells, it must be exported,
either by adding it to an initialization file, as discussed in Module 11,
‘‘Initialization Files,” or by exporting it on the command line.
Note – See the man pages on ksh, csh, and sh for variable definitions.
10
10-6 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Shell Variables
Local Shell Variables
A user-defined variable enables you to determine both the name of the
variable and its value. For example, a pathname could be assigned for
the on-line dictionary used in text editing programs.
By convention, the Bourne and Korn shells use capital letters for shell
variable names. The first command format in the following examples
sets the variable based on a name and value selected by the user, while
the unset command removes the variable from the current shell and
subshells:
Command Format
VARIABLE=value
unset
VARIABLE
10

Bourne and Korn Shell Variables 10-7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Shell Variables
Local Shell Variables
Setting a Local Variable
The echo command simply echoes back to the screen whatever is
passed to it as an argument. The dollar sign ($) metacharacter
preceding a variable name enables the system to use the value of the
variable and not the name of the variable. In the above example, the
echo command with $DT displays the value of DT to the screen. Since
this is a local variable, if a new subshell is opened, the variable DT is
not available.
$ DT=/usr/dict
$ echo $DT
/usr/dict
$ cd $DT
$ pwd
/usr/dict
$ unset DT
$ echo $DT
$ cd
$ cd $DT
$ pwd
/home/user2
10
10-8 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Shell Variables
Displaying Shell Variables
Variables and their values can be displayed by typing the set

command.
Command Format
set
Displaying Variables
$ set
DISPLAY=:0.0
ERRNO=13
FCEDIT=/bin/ed
HELPPATH=/usr/openwin/lib/locale:/usr/openwin/lib/help
HOME=/home/guest
HZ=100
IFS=
LANG=C
LINENO=1
LOGNAME=guest
MAIL=/var/mail/guest
MAILCHECK=600
MANPATH=/usr/openwin/share/man:/usr/man
NOSUNVIEW=0
OPENWINHOME=/usr/openwin
PATH=/usr/openwin/bin:/usr/bin:
PPID=867
PS1=$
PWD=/home/guest
RANDOM=4188
SHELL=/bin/ksh
TERM=sun-cmd
TZ=PST8PDT
USER=guest
10

Bourne and Korn Shell Variables 10-9
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Environment Variables
Your computing environment is composed of special information, such
as the location of your mailbox and the type of terminal you have. The
SunOS 5.x system software provides several default environment
variables such as PS1, HOME, LOGNAME, SHELL, and PATH. These
variables have been defined by the shell to have a specific function.
The values of these customizable variables can be changed to suit the
user’s needs.
You can temporarily change your environment variables at the
command line. This affects only the current shell. When you exit the
shell where the environment variable has been assigned, that
environment variable is terminated or set back to its default value.
Permanent changes are made by modifying the initialization files,
discussed in Module 11, ‘‘Initialization Files.”
For more information on environment variables, refer to the sources
listed in the ‘‘Additional Resources” section of this module.
The Bourne and Korn shells use the same format for setting
environment variables.
10
10-10 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Environment Variables
Note – Any shell variable can be made available as an environment
variable.
Exporting Variables
Exporting variables in an initialization file enables the variables to be
used by the system, processes, scripts, users, and all shells. Exporting
variables at the command line makes the variables available to the

current shell and all of its child processes. A variable set in a subshell
and exported will be available to any shells opened afterwards but
will not be exported to the parent shell. It is good practice to use the
echo command to check if a value for a variable already exists before
setting a new one. This protects accidental overwriting of a previously
set value.
Command Format
VARIABLE
=
value
;export
VARIABLE
or
export
VARIABLE=value
Creating Environment Variables
$ LPDEST=staffp; export LPDEST
$ echo $LPDEST
staffp
$ EXINIT=’set showmode’; export EXINIT
$ echo $EXINIT
set showmode
$ unset LPDEST
$ echo $LPDEST
$
10
Bourne and Korn Shell Variables 10-11
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Environment Variables
The LPDEST variable defines the default printer. EXINIT sets a last line

option for vi.
Note – Setting the EXINIT variable will override any settings saved in
a.exrc file to customize the behavior of the vi editor.
10
10-12 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Environment Variables
Displaying Environment Variables
Environment variables and their values can be displayed by typing the
env command.
Command Format
env
Listing Environment Variables
$ env
_=/usr/bin/env
MANPATH=/usr/openwin/share/man:/usr/man
LANG=C
HELPPATH=/usr/openwin/lib/locale:/usr/openwin/lib/help
HZ=100
NOSUNVIEW=0
PATH=/usr/openwin/bin:/usr/bin:.
WINDOW_TERMIOS=
OPENWINHOME=/usr/openwin
LOGNAME=user1
MAIL=/var/mail/user1
TERMCAP=sun-cmd:te=\E[>4h:ti=\E[>4l:tc=sun:
DISPLAY=:0.0
SHELL=/bin/ksh
HOME=/home/user1
TERM=sun-cmd

PWD=/opt
TZ=US/Pacific
ENV=/export/home/user1/.kshrc
XINITRC=/usr/openwin/lib/Xinitrc

×