Академический Документы
Профессиональный Документы
Культура Документы
IT.ACADEMY.LV
. .
UNIX
2002
IT.ACADEMY.LV
ISMA
. .
UNIX: . . I.
: ISMA, 2002. 136 ., .
ISBN 9984-9531-7-3
, , ,
UNIX,
.
UNIX,
UNIX.
:
UNIX;
,
UNIX;
UNIX,
;
shell .
,
UNIX. UNIX, UNIX ,
UNIX , UNIX , UNIX ,
UNIX, .
. ., 2002
IT.ACADEMY.LV, 2002
ISMA, 2002
ISBN 9984-9531-7-3
UNIX: .
.......................................................................................................6
1. UNIX........................................................11
......................................................................................................12
...............................................................................21
...................................................................................................25
..................................................................................30
2. UNIX..................................................31
......................................................................................................32
...............................................................................38
...................................................................................................43
..................................................................................44
3. - UNIX....................45
......................................................................................................46
...............................................................................55
...................................................................................................56
..................................................................................57
4. UNIX..............................................................58
......................................................................................................59
...............................................................................66
...................................................................................................68
..................................................................................70
5. UNIX......................................71
......................................................................................................73
...............................................................................81
...................................................................................................85
..................................................................................90
6. UNIX................................................................91
......................................................................................................92
.............................................................................100
.................................................................................................104
................................................................................105
7. shell UNIX.....................................................106
....................................................................................................107
.............................................................................122
.................................................................................................127
................................................................................133
.....................................................................................................134
......................................................................................135
2002
UNIX: .
2002
UNIX: .
UNIX,
,
.
2002
UNIX: .
( ST?)
.
, UNIX
; (
ST?).
: BSD, XENIX, AIX, HP-UX, SunOS, IRIX, SCO
UNIX, OSF/1, GNU, SVR4, Minix, Linux, Mac OS X, Lindows
.
, , UNIX- (UNIX-like) .
UNIX.
UNIX,
.
,
POSIX ( Portable
Operating System Interface UNIX-like Open System)
IEEE-1003 ISO-9945.
. UNIX
.
, , UNIX
.
POSIX, .. GUI, API, TCP/IP
, , UNIX-like.
: UNIX-like OS likes UNIX!.
UNIX , ,
, ..
: ; ; ; ST,
. UNIX
, GUI
, . , GUI
UNIX, MS Windows, .
UNIX, .
30- UNIX,
, .
(ST) UNIX!
2002
UNIX: .
2002
UNIX: .
.
, .
, UNIX , UNIX
, UNIX , UNIX ,
UNIX ,
.
UNIX
.
,
UNIX,
.
.
.
,
.
, ,
.
, .
.
,
.
,
.
, -
UNIX/Linux ( UNIX)
( bash sh).
2002
UNIX: .
1.
.
2.
.
3. .
4. UNIX
-, .
5.
$,
$,
$.
6.
(root), #.
7.
. ,
.
2002
10
UNIX: .
1. UNIX
UNIX.
.
UNIX.
UNIX.
.
UNIX UNIX manual pages (man).
login, password, hostname, uname, ,
, , shell, logout, exit, passwd, man, apropos, help, info,
whatis, whereis, xman, type, pwd, cd, ls, mkdir, rmdir, rm, cp, mv, ln,
du, touch, cat, more, tail, head, file, find, who, id, uname, date, banner,
cal, bc, echo
2002
11
UNIX: .
1.1.
UNIX - ,
. ,
,
, ,
. UNIX
:
(login) (password). ,
,
( ,
, , root hacker).
UNIX- (root)
(account) , ..
.
, .
vasja.
UNIX (hostname),
.
,
hostname. , , dom.
UNIX- hostname,
uname (UNIX Name).
1.2.
( ) ,
, :
dom login:
(login) <Enter>:
dom login: vasja
Password:
(password).
,
. ,
Login incorrect.
2002
12
UNIX: .
.
<Ctrl>+<U>.
, ,
:
, <Enter>!.
, , ,
.
uname hostname.
1.3.
- ,
( ).
shell shell
( ): exit, logout, <Ctrl>+<D>.
/home/vasja$ exit
shell .
( )
( ): halt, shutdown, init 0,
<Ctrl>+<Alt>+<Del>.
,
() UNIX .
,
UNIX!!!
1.4.
UNIX .
,
. ,
UNIX .
UNIX, Linux,
(VC),
.
,
,
2002
13
UNIX: .
, ,
VC.
Linux, .
<Alt>+<F2>. login:,
, .
VC, <Alt>+<F1>.
.
Linux ,
12- VC.
VC <Alt>+<F1>
<Alt>+<F12>.
X Window System
GUI (Graphic User
Interface), , ,
startx (Linux) openwin
(Solaris). X Window System MS Windows,
! .
X Window System
<Ctrl>+<Alt>+<FN>,
<Alt>+<F7> ( X-).
, ,
, .
1.5. Shell
UNIX
(shell). Shell ,
. Shell
UNIX. , ,
X Windows System.
, .
/home/vasja$
/home/vasja$ shell, ,
.
2002
14
UNIX: .
shell,
.
.
.
:
< >:=<>[;<>]
<>:=<>[ -<>][ <>][&]
<>:=[<>[<>]]
<>:=[<>[ <>]]
:
/home/vasja$ ls al -u file1 dir1 &
:
ls ( listing ) ;
-a -l -u ;
file1 dir1 ;
& .
.
, , ,
.
, shell :
, shell.
, shell ,
.
,
. ,
.
, shell
. shell ,
, .
shell ,
,
.
2002
15
UNIX: .
, ,
shell.
, abracadabra,
, shell:
/home/vasja$ abracadabra
abracadabra: command not found
1.6.
, .
passwd , .
passwd .
$ passwd
Old password:
Enter the new password (minimum of 5, maximum of
127 characters)
Please use a combination of upper and lower case
letters and numbers.
New password:
Bad password: too short. Try again.
New password:
Re-enter new password:
Password changed.
$
!
.
,
. ,
,
, .
1.7.
UNIX ,
:
man on-line ;
info
;
2002
16
UNIX: .
help
bash;
FAQ Frequently Asked
Questions,
UNIX
( find);
Answer Book Solaris.
Manual Pages
UNIX , ""
man (manual pages). man
, ,
.
UNIX Reference Manual 8 :
1. Commands ;
2. System calls ;
3. Subroutines ;
4. Special files ;
5. File format and convention ;
6. Games ;
7. Macro packages and language conventions
( );
8. Administrator commands and procedures
.
UNIX Reference Manual
man.
man:
$ man [i] <name>
i ; i , i = 1;
<name> .
, ls,
/home/vasja$ man ls
ls.
man? /usr/man/,
2002
17
UNIX: .
, 1 8
.
nroff (new runoff).
,
.
/usr/man/cat?/*, ?
* ,
catman.
man,
; ,
man nroff,
.
nroff, man .
man:
<q> (quit);
<>, <f>, <> (forward);
<b> (back);
</pattern> (search) ;
</> (next)
;
<?> (previous)
.
:
NAME
;
SYNOPSIS
;
DESCRIPTION
;
OPTIONS
;
SEE ALSO
;
FILE
;
DIAGNOSTIC
;
BUGS
;
2002
18
UNIX: .
IMPLEMENTATION
, man
.
1.8.
. -
. UNIX
.
( /)
255 . ,
( ).
,
, , ...
(,
, UNIX
).
UNIX
, ,
/.
/
UNIX. /bin, /etc, /dev, /usr /home.
,
, , , ..
(
/etc/passwd),
.
/home
. /home/vasja.
, shell,
() .
.
, ,
, .
:
2002
19
UNIX: .
~ ,
.
.. .
, ,
:
/home/vasja$ more /home/vasja/letters/masha-let
/home/vasja/bin$ more ../letters/masha-let
/tmp$ more ~/letters/masha-let
, pwd, ls cd.
Permission denied ( ).
UNIX.
, (permissions).
.
. .
, UNIX .
man .
1.1.
UNIX !
2002
20
UNIX: .
,
, .
, man-
.
.
. ,
.
,
.
1.9.
:
apropos
,
whatis;
help
bash;
info
;
type ,
, ,
;
whatis ,
;
whereis
;
xman X- man, .
2002
21
UNIX: .
apropos
apropos ,
. :
$ apropos sort
ddsort (3/21/80) - sort DDBS files
look (1)
- find lines in a sorted list
. . . . . . . . . . . . . . .
tsort (1)
- topological sort
whatis
whatis,
:
$ whatis mv ln cp
cp (1) - copy
ln (1) - make links
mv (1) - move or rename files
whereis
whereis ,
( -m , b ). :
$ whereis -m rm
rm: /usr/man/man1/rm.1
$ whereis -b rm
rm: /bin/rm
1.10.
:
pwd, cd, ls, mkdir, rmdir, rm, mv, ln s, du
:
$
$
$
$
$
$
$
$
$
pwd
cd /; pwd; cd ~; pwd; cd /home/vasja; pwd;
cd ../../usr; pwd; cd /; pwd
ls; ls u; ls t; ls l; ls i; ls a;
ls d; ls r; ls R; ls -F
mkdir ~/letters
cd ~/letters; pwd;
mkdir a b c; ls
rmdir a; cd c; pwd; rmdir ../b
2002
22
UNIX: .
$
$
$
$
cd; rm r letters
mkdir a b c; ls il; ln s a e; mv c a
ls il; ls l a
cd ~ ;du; du a; du a /; cd ~; rm r *
1.11.
:
touch, cat, cp, mv, rm, more, tail, head, ls, ln, file, find
:
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
touch leter0
> letter1 <Ctrl>+<D>;
cat > letter2 <Ctrl>+<D>
cat letter1; more letter1 letter2; pr letter2
cp /etc/passwd .
tail passwd; tail +20 passwd
head passwd; head 5 passwd
cat > masha
ln masha dasha
ls il masha dasha; rm masha; ls il
ln s dasha masha; ls il; rm dasha; ls il
cp letter1 dasha; ls -il
file masha dasha; file passwd
file /usr/bin/sh .. letter0
find / -name passwd; find ~ -size 10
find / -perm 4000
find /usr -type d; find / user vasja
find / -group root user 0; find / print
find . mtime 7 -exec ls l {} \;
find /home -name *.bak -exec rm {} \;
1.12.
:
who, who am i, id, uname, date, banner, cal, clear, bc, echo
:
$
$
$
$
2002
23
UNIX: .
bc 23,860,929
( UNIX-).
quit
EOF ( <Ctrl>+<D>).
2002
24
UNIX: .
1.1.
, -,
, . , ls -l -F,
ls -lF.
UNIX
.
( , , ).
man .
cat
,
( ). .
:
cat <file1> ... <fileN>
letters/from-mdw.
cd
.
:
cd <directory>;
<directory> , (.
, .. ).
:
$ cd ../foo
../foo.
cp
() .
:
cp <file1> ... <fileN> <destination>
25
UNIX: .
:
$ cp ../masha dasha
../masha dasha.
echo
, .
:
echo <arg1> ... <argN>
Hello, world!.
file
.
:
file <file1> ...<fileN>
:
$ file *
find
,
.
:
find [path...] [expression] []
[path...] , ;
[expression] , print;
[] .
:
, :
$ find / -name [a-zA-Z]*[0-9]
,
:
$ find / -mtime 7
,
, :
$ find / -mtime 7 exec ls l {} \;
tmp- :
$ find ~ -name *.tmp exec rm f {} \;
2002
26
UNIX: .
ln
.
,
. ,
,
.
.
:
ln [-s] <source-file> <destination-file>
<source-file> <destination-file> ,
,
:
-s .
:
$ ln -s /home/vasja/report /home/masha/report
/home/masha report
/home/vasja.
ls
.
:
ls <file1> ... <fileN>
/home/vasja.
man
.
"" , ,
, .
:
man <command>
2002
27
UNIX: .
<command> ,
.
:
$ man ls man
ls man.
mkdir
.
:
mkdir <dir1> ... <dirN>
test /home/vasja.
more
.
:
more <file1> ... <fileN>
history-final ./papers.
mv
() .
.
,
RENAME.
:
mv <file1> ... <fileN> <destination>
../masha dasha.
pwd
/ .
$ pwd
2002
28
UNIX: .
rm
. , UNIX ,
( MS-DOS, "" ).
:
rm <file1> ... <fileN>
rmdir
. rmdir
.
:
rmdir <dir1> ... <dirN>
/home/vasja/papers, .
2002
29
UNIX: .
:
;
,
;
:
,
,
,
,
,
,
,
,
,
,
, ;
,
,
;
, , ,
;
cat;
;
;
UNIX
UNIX manual pages (man).
2002
30
UNIX: .
2. UNIX
.
shell.
.
.
shell.
shell, sh, bash, csh, ksh, tcsh, zsh, chsh, echo, /etc/shells, exit, logout, ps,
/etc/passwd, chfn, , , , ,
readonly, export, set, unset, env, noclobber, ,
, echo, ,
, /etc/motd, umasc, alias, unalias, /etc/skel, exec
2002
31
UNIX: .
2.1. (shell)
, , UNIX, shell, , . shell
. UNIX
.
Shell ,
. , shell
, , shell
(shell-scripts). UNIX
shell.
sh, bash, csh, ksh, tcsh, zsh
XFree86, CDE, Photon.
shell /etc/shells.
$ more /etc/shells
$ chsh -l
chsh - shell
shell. :
chsh [-s shell][-l][-h][-v][username]
shell:
/bin/sh
Bourne shell ( );
/bin/bash Bourne Again shell ( Bourne shell);
/bin/csh
C-shell ;
/bin/tcsh Turbo C-shell C-shell;
/bin/ksh
Korn-shell csh,
sh, . AT&T;
/dev/null -shell, .. , .
shell
/etc/shells, root, , ,
, shell ,
/etc/shells.
shell :
2002
32
UNIX: .
$ chsh -s /bin/csh
, , ..
;
$ exec /bin/csh
shell , /etc/passwd;
$ bash
bash-shell, shell,
shell.
shell- exit, logout
intr ( <Ctrl>+<D>).
shell, ps
process status reports. ,
$ ps u
() shell -.
shell,
, /etc/passwd
, , (login)
. , vasja:
$ more /etc/passwd
/etc/passwd
passwd chfn ( change finger name)
chfn GECOS ( ,
; GECOS General Electric Comprehensive
Operating System), finger. GECOS
,
: Full Name, Office, Office Phone, Home Phone.
:
chfn [-h|-f|-o|-p|-h] [user]
2002
33
UNIX: .
shell-script
$ bash filename
shell
$ bash c 'command'
2.2. () shell
shell-script
.
<Esc>+<Ctrl>+<E>. - <Ctrl>+<->.
, :
$ ls -l `echo ${HOME}/..`
$ ls -l /home/vasja/..
:
1. , , shell
.
2. ,
` `.
3.
$.
4. $(( )) $[ ].
5. <Space>,
<Tab>, <Enter>, .
6. . , ? *
[ ,
.
7. , \.
:
? ,
, *.??? ,
;
* ,
. , * , ;
[list] , : [abk3] [A-Za-z0-9_];
2002
34
UNIX: .
[^list] , , :
[^-abk3] [^A-Za-z0-9_].
2.3. shell-
.
shell-
= .
.
$
: $. :
$ ux=UNIX
$ echo $ux
$ syst=$ux; echo $syst
, :
= ``. :
$ cdir=`pwd`
$ echo $cdir
/export/home/vasja/documents
, ,
,
\.
:
$ VP=Vasja Pupkin; echo $VP
$ VP=Vasja\ Pupkin; echo $VP
readonly :
$ readonly VP cdir
$VP.
$ VP=HACKER
Read Only
shell.
,
shell
export. shell
login,
ini-.
2002
35
UNIX: .
$ export vp cdir
readonly export ,
shell, typeset, declare,
unset, set.
:
$ set o noclobber
.
, noclobber,
# ls > /etc/passwd
, root, /etc/passwd
.
( noclobber Cshell Korn-shell):
# ls >! /etc/passwd
2.4.
(), <Space>, <Tab>, <Enter>, |, >, <,
& , .
shell,
, ,
shell,
, shell,
. ()
shell.
\c backslash, c;
,
;
,
: \ , ` , , $.
, :
$ echo $HOME; echo $HOME
$ echo \$HOME; echo $HOME
2002
36
UNIX: .
\
. \ <Enter>
,
.
. ,
.
2.5. shell
shell shell-
, .
,
.
, shell, ,
(ANSI X)
.
set,
env, echo:
$ echo $PATH
root, user,
shell
. :
$ PS1=Hello, Vasja >
export:
$ export PS1 HOME
$ export PS2=-- >
unset:
$ unset PS1
2002
37
UNIX: .
2.6. shell
.
PATH
HOME
TERM
PS1
PS2
MANPATH
LOGNAME
MAIL
MAILCHEK
CDPATH
HISTSIZE
HISTFILE
TMOUT
IGNOREEOF
COLOR
IFS
PROMT_COMMAND
SECONDS
OSTYPE
PWD
OLDPWD
EDITOR
RANDOM
TZ
SHELL
2002
\s-\v\$
>
,
PS1. PS1, PS2, PS3
PROMT_COMMAND
escape, :
\w - , ;
\W , ;
\u username;
\h hostname;
\$ - $ user # root;
\t ;
\d ;
\s shell .
shell
: MSDOS, linux-gnu, freebsd
Time Zone
38
ls l
UNIX: .
shell
(, bash). man bash.
,
shell set, env, echo.
,
.
?
2.7.
, shell
2-
.
shell
.
2.7.1.
/etc shell
.
: /etc/profile, /etc/.login, /etc/csh.chsrc
(. 2.1).
,
shell,
/etc/passwd.
: ,
/etc/motd , ,
( ,
, history, shell .),
umasc , (alias)
..
2002
39
UNIX: .
2.7.2.
/etc/passwd
.
: ~/.login, ~/.profile, ~/.cshrc (.
2.1).
shell .
, shell
.
, shell,
.
(, useradd
adduser).
: /etc/skel/, /usr/local/lib/skel/.
,
, , ,
.
,
, , ,
, ..
.profile:
PATH=$PATH:$HOME:bin
EDITOR=vi
TERM=vt100
PS1=\t \h\w`echo \007`\$
export EDITOR TERM PATH PS1
echo Hi, $LOGNAME. `who|wc -l` users are in
system
#
# ESC- . 7.3.
PROMPT_COMMAND=`echo ne \
\337\033[2;999r\033[1;1H\033[00;44m\033[K`date`\033[00m\338`
alias ls=ls F
alias d=dir
2002
40
UNIX: .
2.7.3.
.
: ~/.kshrc, ~/.bashrc, ~/.cshrc
(. 2.1).
, .
shell .
2.7.4. ,
, shell,
. shell
exec <shell-name>.
~/.logout (. 2.1).
, , .
.logout:
rm f $HOME/*.log
rm f $HOME/*.log
echo Bye, $LOGNAME
2.7.5.
bash
1. ,
bash shell,
:
/etc/profile
/etc/bashrc
2.
:
~/.bashrc
~/.bash_profile
3. shell, bash,
:
/etc/bashrc
~/.bashrc
2002
41
UNIX: .
4. shell ,
~/.bashrc /etc/bashrc .
5. shell exec,
:
/etc/bashrc
~/.bashrc
6. shell shell
exec ~/.bash_logout .
7. bash-shell :
~/.bash_logout
2.7.6.
shell,
.
ini. ,
.
man ,
2.2.
2002
42
UNIX: .
2.1. shell
Shell
shell
(Systemwide)
(login)
shell
~/.profile
~/.profile
~/.kshrc
~/.kshrc
~/.bash-profile ~/.bashrc
~/.bash-logout
~/.bash-login
~/.profile
~/.cshrc
~/.cshrc
~/.logout
~/.login
Bourne
Korn
/bin/sh
/bin/ksh
/etc/profile
/etc/profile
Bash
/bin/bash
/etc/profile
/etc/bashrc
/bin/csh
TC
/bin/tcsh
/etc/.login
(Solaris)
/etc/csh.login
(Linux)
/etc/csh.cshrc
(Linux)
/etc/csh.cshrc ~/.cshrc
/etc/csh.login ~/.login
/bin/zsh
/etc/zshenv
/etc/zprofile
/etc/zshrc
/etc/zlogin
~/.zshenv
~/.zprofile
~/.zlogin
~/.tcshrc
,
~/.cshrc
~/.zshrc
~/.logout
2.2.
vi
emacs
~/.exrc
~/.emacs_pro
~/.mailrc
rn
~/.newsrc
mc
~/.mc/
startx
~/.xinitrc
2002
vi
emacs,
emacs
.
, .
,
Midnight Commander.
X11.
43
UNIX: .
:
shell;
shell;
, ;
shell: , ,
, , ;
shell;
;
, ;
shell;
( 2.7);
;
shell
;
, , Midnight
Commander:
,
,
.
2002
44
UNIX: .
3. -
UNIX
,
.
.
UNIX.
.
,
, .
, .
stty, control characters, tty, who, , stdin, stdout,
stderr, >, >>, noclobber, <, <<, at, mail, |, , , who, wc,
sort, tee, 1>, 2>, 1>&2, 2>&1, >&
2002
45
UNIX: .
3.1.
, -
.
.
UNIX-
Shell
User
UNIX
/dev/
shell
shell
,
.
/
, , .
(control characters),
, , ..
Shell ,
control characters, , shell .
stty. UNIX
, stty set tele type.
:
$ stty a
$TERM:
$ echo $TERM
vt100
$
2002
46
UNIX: .
(, vi)
,
, .
stty sane, tset reset.
3.2.
stty
, echo
read ( 7), :
stty -echo
;
stty echo
.
,
.
:
$ ls -R
, ,
,
,
stop, start, intr (.
3.2).
tty ,
:
$ tty
/dev/tty1
,
who:
2002
47
UNIX: .
$ who
root
vasja
vasja
vasja
vasja
tty1
tty2
pts/0
pts/1
pts/2
Oct
Oct
Oct
Oct
Oct
10
16
16
16
16
10:45
18:01
18:02
18:02
18:02
X System Terminal.
root, user.
, root, ,
vasja - :
# banner shadow for Vasja > /dev/tty2
# cat /etc/shadow > /dev/tty2
3.2.
UNIX
.
.
UNIX
.
, .
UNIX
stdin, stdout, stderr .
, (),
,
, (stdin). stdin
().
, ,
(stdout). stdout
().
(stderr)
,
, stdout.
2002
48
UNIX: .
3.3.
>, >! >>.
stdout:
$ command > file_name
$ command >! file_name
$ command >> file_name
shell
command , file_name
0
.
,
$ > new-file
new-file,
$ ls > spisok
ls spisok, spisok
, , ,
,
.
,
shell
noclobber.
$ set o noclobber
noclobber
$ set +o noclobber
>! noclobber
,
C-shell Korn-shell
>,
, >.
2002
49
UNIX: .
, , , ,
ls spisok, spisok
:
$ mkdir temp; cd temp; touch file1
$ ls a > spisok
cat spisok
. .. file1 spisok
,
, , ,
, .
,
myletter :
$ cat myletter > myletter
>> >,
, ,
, .
,
$ uname a >> HackerVasja
$ cat /etc/passwd >> HackerVasja
3.4.
shell
<.
stdin:
$ command < file_name
UNIX
(cat, more, ls, pwd). stdin .
, cat
:
$ cat
This is a new line
This is a new line
2002
50
UNIX: .
^D ,
( <Ctrl>+<D>).
, , , ..
. cat >
$ cat > myfile
This is a new line
for the cat command
^D
$ more myfile
This is a new line
for the cat command
$
, myfile .
stdin cat :
$ cat < myfile
This is a new line
for the cat command
$
,
:
$ cat > myfile < myletter
3.5.
. ,
<< -. :
$ mail root << w
Hello, root!
w
$
2002
51
UNIX: .
:
$ command_name arguments <<[-]symbol_flag
...
symbol_flag
symbol_flag , ;
<< -,
, shell.
,
happy.new.year:
$ at Dec 31 << !
cat happy.new.year | mail sHappy NY!
vasja@pupkin.lv
!
$
, :
-
;
-
<\>;
-, -.
3.6.
,
, ..
.
.
|:
$ command1 | command2
, .
who, wc, rm. :
$ who > temp.tmp
$ wc -l temp.tmp
$ rm temp.tmp
:
2002
52
UNIX: .
$ who | wc l
,
.
, ls, pwd, stdin
.
-,
grep, egrep, sort, head, tail, wc, spell, sed, diff.
,
. ,
,
:
$ sort /etc/passwd | cat -n | more
3.7.
tee
, -, , -,
;
, , , .
-a ( append
): tee -a file.
:
$ cat my-letter | tee new-letter
new-letter ;
$ cat my-letter | tee new-letter | wc -w > size
new-letter wc ,
size.
> >>,
tee ,
.
. ,
.
2002
53
UNIX: .
3.8.
, (
)
,
0, 1, 2.
.
,
stdout. UNIX stdout stderr,
.
, find, ,
,
.
$ find / -name *
:
$ find / -name * 2>/dev/null
stdout stderr
> >> , ,
:
$ command 1> file_out 2> file-err
$ command 1> file_out 2> file-out
$ command > file_out 2>> file-err
3.9.
stdin, stdout, stderr,
&.
>& stderr,
stdout.
,
, stdout.
, stdout myfile
stderr.
$ command 1> file_out_err 2>&1
$ command > file_out_err 2>&1
$ command >& file_out_err
2002
54
UNIX: .
.
UNIX. N3
-
___________________________________ ______
1.
,
: stdin stdout; stdin;
stdout; stdin, stdout? ,
$ command -help > file-out
stdout.
: pwd, cd, ls, mkdir, rmdir, rm, mv, ln, du, touch, cat, cp, find,
more, tail, head, file, banner, who, id, uname, date, cal, wc, chsh, exec,
echo, export, readonly, set, unset, passwd.
:
stdin
stdout
more
stdin,
stdin
stdout
stdout
ls
pwd
2.
command1|command2 command2|command1 .
: who, wc, pwd, uname, id, more.
:
who
wc
pwd
uname
id
more
who
-
2002
wc
pwd
uname
id
more
55
UNIX: .
3.1. shell
>
>!
>>
<
<<
|
2>
2>>
2>&1
>&
( stderr)
, , , ,
.
, ,
, . noclobber
( ). C-shell Korn-shell.
( stderr)
, .
.
.
( stderr)
.
.
.
.
.
3.2.
,
. ^X
<Ctrl> <X>.
erase =^H (<Backspace>) ;
werase = ^W ;
kill = ^U ( );
stop = ^S ;
start = ^Q ;
discard = ^O ;
intr = ^C ;
suspend = ^Z ;
eof = ^D ;
eol = <Enter>
.
2002
56
UNIX: .
:
;
;
;
;
, ;
, ;
.
2002
57
UNIX: .
4. UNIX
vi.
ex.
UNIX
, vi vim.
vi, , ex, , , , ,
vim, joe, pico, emacs
2002
58
UNIX: .
vi,
ex.
, vim, joe, pico, emacs.
UNIX , ,
vi (visual
editor). : ex
( ed); vi
( ex). vi
,
, ,
, UNIX.
, (man vi,
info vi, man ex, info ex, man vim, info vim).
4.1. vi
vi
(work mode).
. vi .
vi.
vi, .
. ,
, x ,
.
. ,
, .
/. vi,
.
/ .
, .
<Esc>.
2002
59
UNIX: .
vi
EX Mode ex
q wq q!
ex.
<Enter>
Normal- .
vi
vi.
ex
Q gQ
vi
ex.
<Enter>
Normal-.
e r w q ! shell
<Enter>
q wq q!
: ? / n N
Normal Mode -
,
.
<Enter>
.
, ,
.
a A i I o O x d c C r R s S
u U J p P y g G
vi
view
<Esc>
Insert/Replace Mode
.
.
<Enter> -
.
<Esc> - Normal-.
2002
60
ZZ
<Insert>
a A I i
o O c C
s S R
UNIX: .
,
.
.
, : ,
, wq - (write) (quit) vi;
q! - vi .
vi,
.
vi
, ,
.
) $ vi myfile ;
.
) $ vi + myfile ;
.
) $ vi +19 myfile 19
; .
vi
) : <Esc> <:> <w> <q> <!>
<Enter> Esc, (
), w, q !,
Enter.
) : <Esc> <:> <q> <!> <Enter>
4.2. vi
,
VIM TUTOR ( ).
vim (Vi IMproved, a programmers text editor
vi ).
, ,
, vi
.
2002
61
UNIX: .
$ cp /usr/share/doc/vim-common-6.0/tutor/tutor ~
$ vi ~/tutor
30 .
4.3. ex, vi
, vi ex,
.
, ex,
vi.
vi ex
vi ex <Esc>
, ,
<:> ().
ex ,
ex.
vi gQ,
vi vi ( vi).
4.3.1.
ex - ,
.
, . $ ()
.
, (
) -
p (print). ,
:1,5p
:1,$p
,
# (), ,
:6,$#
, .
.
.
2002
62
UNIX: .
4.3.2.
g (
) ,
, :
:g/speed
int speed;
d = speed x time;
output (speed).
,
. g ,
, :
:/speed
int speed;
4.3.3.
s (substitute),
,
:
:s/eed/ace/
int space;
.
g (global - ) :
:s/wine/water/g
,
.
:
:1,10s/wine/water/g
:1,$s/wine/water/g
:
:g/speed/s/eed/ace/g
:g/speed/s//space/g
2002
63
UNIX: .
,
y (yes).
c (confirmation):
:g/eed/s//ace/gc
u (undo).
u ,
u.
4.3.4. /
.
r (read),
:
:0r file.txt
file.txt
( ).
, ( ! ):
:r !date
:r !ls
w (write), :
:w newfile
:w >> oldfile
.
. ,
.
:w! oldfile
4.3.5.
,
:
2002
64
UNIX: .
$ vi .*
$ ex *.c
n (next), N:
:n
e (edit)
,
:e another
n e
, ,
,
:n! :e! .
4.3.6. ex
! ( ),
:
:!cc %
% .
:
:!!
2002
65
UNIX: .
4.4. vi
vi
. vi,
~/vi-small-help vi
(. 4.1).
4.5.
vi
.
- .
emacs
emacs [filename]
,
. ,
,
<Esc>, <Ctrl> <Alt> (). emacs
: -x <Ctrl>+<x>; M-a <Alt>+<a>.
emacs:
C-x,C-c <Ctrl>+<x>, <Ctrl>+<c>.
, vi, .
,
emacs - , 300 .
,
.
. emacs
, .
joe
joe [global-optns][ [local-optns] filename ]...
jstar [global-optns] [ [local-optns] filename ]...
jmacs [global-optns] [ [local-optns] filename ]...
jpico [global-optns] [ [local-optns] filename ]...
2002
66
UNIX: .
ASCII- .
, .
, PC:
, WordStar Borland's . joe
UNIX: pico, emacs,
WordStar.
joe
<Ctrl>+<K><H>. joe
4.2.
pico
vi.
, ASCII.
.
vi:
, ,
, ,
.. :help vi_diff.txt
vim vi.
2002
67
UNIX: .
4.1. vi
VI QUICK REFERENCE
------- TEXT
<Esc>
~
a
i
A
I
o
O
<Ins>
<Backspace>
<Del>
r
R
cw
cc
C
s
S
x
X
dw
d$
dd
Nx
Ndw
Ndd
2002
MOVING
moving
moving
moving
moving
moving
moving
----------------------------------cursor left
cursor down
moving cursor up
cursor right
cursor right
cursor in begin next line
cursor in begin previous line
68
UNIX: .
------- SEARCH
/pattern
?pattern
/
?
n
N
-
4.2. joe
JOE QUICK REFERENCE
CURSOR__________
^B left ^F right
^P up
^N down
^Z previous word
^X next word
SEARCH_______
^KF find text
^L find next
DELETE__
^D char.
^Y line
^W >word
^O word<
^J >line
^_ undo
^^ redo
GO TO___________
^U prev. screen
^V next screen
^A beg. of line
^E end of line
^KU top of file
^KV end of file
^KL to line No.
MISC________
^KJ reformat
^T options
^R refresh
^@ insert
SPELL
^[N word
^[L file
EXIT_____
^KX save
^C abort
^KZ shell
FILE
^KE edit
^KR insert
^KD save
BLOCK_____
^KB begin
^KK end
^KM move
^KC copy
^KW file
^KY delete
^K/ filter
WINDOW_______________________________
^KO Split the window in half
^KG Make current window bigger
^KN Go to the window below
^C Eliminate the current window
^KE Load file into window
^KT Make current window smaller
^KP Go to the window above
^KI Show all windows / Show one window
HELP_______________
^KH
for help
^KH
turn off help
^[,
prev. screen
ESC , prev. screen
^[.
next screen
ESC . next screen
2002
69
UNIX: .
:
vi
;
vi :
,
,
,
vi,
,
,
,
;
vi ,
VI SMALL HELP ( 4.4);
UNIX, vi vim (
4.5).
2002
70
UNIX: .
5.
UNIX
-.
.
grep, egrep sed.
sed.
- UNIX.
, , regex, grep, egrep, fgrep, sed, ed, pr,
lpr, split, sort, wc, strings, tr, cat, tac, nl, od, fold, head, tile, sum,
chksum, md5sum, uniq, comm, cut, paste
2002
71
UNIX: .
2002
72
UNIX: .
, ,
.
, ,
, ,
.
. .
-
,
.
,
.
- shell
(script-, -,
)
, .
UNIX , ,
, ,
.
.
(, )
.
5.1. (REs)
,
. ,
,
.
, ,
(RE Regular Expression).
2002
73
UNIX: .
:
, d; ,
; ,
IP- .
5.1.1. REs
.
, REs,
perl grep.
:
: emacs, vi ,vim, ed, sed, ex, Nisus Writer;
: grep, egrep, more, less;
: Korn Shell;
: expr, less, flex, Expect;
: Perl, PHP, Java Script, TCL, Python, awk;
: Delphi, MS Visual C++, Balise.
, RE
,
(man regex,
man sed, man perl).
5.1.2. REs
RE :
escape- (metasequence);
(characters);
(characters classes);
(quantifiers);
, (assertions);
(alternative match patterns);
(back references);
(re extensions).
.
, .
2002
74
UNIX: .
5.1. RE,
,
.
5.1.3. REs
.
RE,
.
(RE)
example
^example
example$
^example$
\<example\>
example.$
example\.$
$example
example^
example*
[eE]xample
example[0-9]
example[^0-9]
example[a-zA-Z]
example[[:alpha:]]
example1.*example2
^example1.*example2$
example\.\.\.$
^$
.
.*
^
$
^\$
$$
X*
XX*
\\
[0-9]
[0-9][0-9]*
\<\...\.\>
\<.*([a-z])\1.*\>
2002
example -
example
example
example
example
example
example
$ example
example ^
example exampl exampleeee
example Example
example
example
example
example l10n
example1, 0 , example2
example1 example2
example
, ..
, .. 1
:
, ..
, ..
, 0 X
, X
, \
,
4 .
,
75
UNIX: .
:
$ grep Vasja *student
compstudent: Vasja Pupkin 7
-v (invert) , .
shell. :
$ grep -v "Vasja Pupkin" compstudent
Bill Gates
2
David Korn
9
5.2.1. grep
:
$ grep ".*" compstudent
2002
76
UNIX: .
file
new-file:
$ grep -v "^$" file > new-file
, :
$ grep -v "^ *$" file > new-file
(, d):
$ ls -l /etc | grep ^d
5.2.2. egrep
shell-
,
IPv4- 0.0.0.0 255.255.255.255,
.
.
:
$ ./egrep-script test-file
egrep-script:
#/bin/sh
egrep \<\
([0-9]|[0-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])\.\
([0-9]|[0-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])\.\
([0-9]|[0-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])\.\
([0-9]|[0-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[05])\> \
$1
\
shell. shell
RE ,
. shell
.
5.3. sed
sed (Stream EDitor) ,
ed
.
(ed, vi, vim), sed
,
2002
77
UNIX: .
.
,
. sed ,
,
.
sed:
sed [-n] [script] [-f script-file] file1[ file2]
-n ,
,
;
-f script-file
;
script ,
;
file1[ file2] .
5.3.1. sed
, :
$ sed p file*
file1:
$ sed n 1~2p file1
5 file1, 20:
$ sed n 20~5p file1
file1, RE:
$ sed n /RE/p file1
file1 , RE,
newfile:
$ sed n /RE/d file1 > newfile
sourcefile file1,
RE:
$ sed /RE/r sourcefile file1
2002
78
UNIX: .
file1 RE
sed:
$ sed /RE/q file1
file1 , RE,
abcde wXyZ;
:
$ sed /RE/y/abcd/wXyZ/ file1
file1 RE
NEW :
$ sed n s/RE/NEW/p file1
file1 g global:
$ sed n s/RE/NEW/gp file1
file1 RE1
RE2 NEW:
$ sed n /RE1/s/RE2/NEW/gp file1
5.3.2. sed
sed sed (sedscript) :
$ sed nf sedscript tutor
shell-, , , :
$ cat shellscript
#!/bin/sh
sed nf sedscript $1
<Ctrl>+<D>
- shellscript
$1,
.
shellscript .
sedscript :
(1,100);
(/RE/);
(#);
, :
y ,
p (print),
2002
79
UNIX: .
s (substitute),
d (delete);
:
(add),
i (insert),
c (change);
({});
.
sedscript:
#n , -n
sed
# EXAMPLE COMMAND FILE FOR SED
#
tutor.
# 2
Lesson
# ({):
# (i),
# (y),
#
(s),
#
(s,&)
# (p),
# :
# (a)
#
/ Lesson/{
i\
**************************************************
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUV
WXYZ/
s/^[
]*//
s/.*/\*\*\*&\*\*\*/p
a\
**************************************************\
*************************(C)2002 Vasja Pupkin*****
}
2002
80
UNIX: .
.
shell- UNIX.
.
, ,
,
.
lpr. :
$ lpr econstudent radiostudent compstudent
lpstat
cancel.
lpr lp .
pr (prepare)
, . :
$ pr myfyle | lpr
66 ( - length)
72 ( - width). -w -l
. :
$ pr -w 132 h "SED program" sedscript| lpr
$ pr -l 25 studentpeople | lpr
-h (head) .
,
.
2002
81
UNIX: .
, , ,
500 :
$ split -500 bigfile
, :
$ split bigfile divs
- , .
-
. :
$ pr bigfile > bigpr
$ split -66 bigpr
$ lpr xaf xaj
6- 10- 66
.
5.6. (sort)
sort -
.
:
$ sort compstudent
Bill Gates
2
David Korn
9
Vasja Pupkin 7
2002
82
UNIX: .
.
:
$ sort +1 compstudent
Bill Gates
2
David Korn
9
Vasja Pupkin 7
+1 ,
.
-b (blank) n (numeric):
$ sort -b +2n
Bill Gates
Vasja Pupkin
David Korn
compstudent
2
7
9
o (output):
$ sort -o compstudent +1 students
m
(merge). :
$ sort +1 econstudent > economic
$ sort +1 radiostudent > radio
$ sort +1 compstudent > computer
$ sort -m +1 economic, radio, computer >
sortallstudents
,
-u (uniq),
.
5.7. , , (wc)
,
wc. -l (lines), -w (words), -c (character)
. :
$ wc students
3 9 51 students
$ wc -l students
3 people
$ wc -lc students
10
51 students
2002
83
UNIX: .
5.8. (tr)
tr ,
,
.
:
$ tr a-z A-Z < compstudent
BILL GATES
2
DAVID KORN
9
VASJA PUPKIN
7
-d ,
.
:
$ tr -d 0-9 < compstudent
Bill Gates
David Korn
Vasja Pupkin
2002
84
UNIX: .
5.1.
c
c1c2c3
.
$
^
*
\
[123]
[c1-c2]
[^123]
[^c1-c2]
[c1c2-c3c4]
2002
85
grep egrep vi
sed perl awk
UNIX: .
\< \>
\b
()
\( \)
|
\|
+
\+
?
\?
{n,m}
\{n,m\}
\k
[:class:]
2002
86
egrep vi
sed
perl
egrep
perl awk
grep vi sed
egrep
perl awk
grep
egrep
perl awk
grep vi
egrep
perl awk
grep vi
egrep
perl awk
grep vi sed
grep egrep vi
sed perl awk
egrep vi
sed perl
UNIX: .
RE, :
, , ,
. , . , ^ .
,
.
Basic Regular Expression (BRE), RE,
/, RE.
Extended Regular Expression (ERE), ,
/,
RE.
5.2. UNIX
cat
. .
, .
(-s) .
, (-n), (-b).
chksum
CRC + . md5sum, sum.
comm
, ,
: 1-
; 2- ; 3-
.
crypt
. DES (Data
Encryption Standard). ,
. :
myparol1:
$ crypt myparole1 < source.doc > encrypt.doc
, :
$ crypt < source.doc > encrypt.doc
:
$ crypt myparole1 < encrypt.doc > reencrypt.doc
cut
(-c) (-f)
.
2002
87
UNIX: .
, . ,
1,2,3,10:
$ cut -f1-3,10 testfile
head
(-n) (-c) .
, . ,
. (-q),
, (-v).
md5sum
(128 ) ,
, (-c
__).
. sum.
nl
, ,
, ,
. : "0001", " 1", "1 ".
od
() (,
, ) "sp", "tb".
.
, . ,
.
paste
,
, tab (-t),
, :
-t123 1, 2, , .
strings
( )
( ASCII).
2002
88
UNIX: .
sum
. .
md5sum.
spell
.
tac
cat . ,
, , .
, .
tail
, head, ()
, "+", ,
. :
$ tail -n+2 /etc/passwd
, , root.
, , WAV.
2002
89
UNIX: .
:
;
;
REs sed, grep, egrep;
grep, fgrep,
egrep;
sed;
,
: pr, lpr, split, cat,
join, sort, wc, tr;
, 5.2;
grep, , ;
sed-
Windows UNIX, .
RE, ( -
):
IPv4-, A,
IPv4-, B,
IPv4-, C,
21.03.2002.
2002
90
UNIX: .
6. UNIX
UNIX.
UNIX.
UNIX.
.
, , , foreground, background, ps, jobs, stty, yes,
/dev/null, kill, fg, bg, nice, renice, nohup, cron, at
2002
91
UNIX: .
6.1.
shell,
, |,
.
.
UNIX
,
, , .
(foreground) shell
.
, ()
(background),
&:
$ ls alR / &
[1] 2042
$
(-R ) UNIX.
UNIX ,
,
[1] PID=2042. PID
(Process IDentificator) ,
.
,
.
6.1.1. ()
, ,
shell . ,
(job control),
, .
2002
92
UNIX: .
?
,
- .
,
shell, - .
,
, .
. ,
. ,
,
. .
, .
.
,
(-
"" "" ).
$ ls alR / > listing.txt &
[1] 2042
$
$ ls alR / | wc l > count.txt &
[1] 2048
$
2 ( ls 2042
wc 2048), .
6.1.2. (ps, jobs)
,
ps (process status):
$ ps -a
PID
TTY
2036 02
2042 02
2048 02
2049 02
2050 08
TIME
0:05
0:02
0:00
0:01
0:03
2002
CMD
sh
ls /
wc /
ps
sh
(login - 2)
(background )
(background )
( ps)
(login- 8).
93
UNIX: .
, ls . l, u, x, a
ps
( , , ..),
.
ps
, pstree, top, tload (. 6.1).
, ,
shell- jobs -l.
: Done
(), Running (), Stopped ()
Terminated ():
$ jobs l
[1] + 2048 Running ls alR / | wc l / > count.txt&
(
), , :
.....
[1] 2048 - ls alR / | wc l / > count.txt
$
6.1.3.
().
,
.
, ,
, .
,
. , ,
.
,
, .
stty.
<Ctrl>+<C>, .
, ,
, ,
.
2002
94
UNIX: .
,
<Ctrl>+<C>
.
.
.
.
, .
6.2.1.
/dev/null
, UNIX Space Travel.
/dev/null .
y,
/dev/null.
$ yes > /dev/null
, . ,
shell . ,
y /dev/null.
<Ctrl>+<C>, .
, yes
2002
95
UNIX: .
; shell,
.
yes ,
, :
$ yes > /dev/null &
[1] 164
$
yes ,
y .
,
jobs.
$ jobs
[1]+ Running yes > /dev/null &
$
, yes .
ps yes.
6.2.2.
<Ctrl>+<C>,
kill.
.
, ,
.
%,
, %.
- kill: shell-,
UNIX.
, .
yes 1,
, kill:
$ kill %1
jobs :
$ jobs
[1]+ Terminated yes > /dev/null
$
2002
96
UNIX: .
. yes ,
.
jobs, .
$ jobs
$
,
(PID), ,
( ). PID
164. yes ,
, :
$ kill 164
$ kill -9 164
9 ;
1-15 ( man kill), ,
.
. kill -9
.
kill.
() :
$ kill -9 0
$ kill -9 $$
( root):
# kill -1 1
, :
$ kill -9 $!
, :
$ kill -9 `cat idfile`
97
UNIX: .
,
shell.
, ,
<Ctrl>+<C>, . .
,
<Ctrl>+<Z>:
$ yes > /dev/null
<Ctrl>+<Z>
[1]+ Stopped yes > /dev/null
$
, ,
.
, ,
.
6.3.2.
.
shell- fg (foreground - ).
$ fg
yes > /dev/null
Shell ,
,
.
.
<Ctrl>+<Z>. bg
(background , ).
, &
.
$ bg
[1]+ yes >/dev/null &
$
. jobs ,
yes ,
kill, .
? <Ctrl>+<Z>
, .
2002
98
UNIX: .
,
. fg
, , .
.
, , ,
( ,
).
, .
- ,
, - . ,
:
$ yes &
stdout /dev/null, y
.
<Ctrl>+<C> .
y,
fg
, <Ctrl>+<C>, .
. fg bg
,
, +
, jobs.
,
, ,
fg bg:
$ fg %2
$ bg %3
( 2)
( 3).
.
, , shell.
fg, bg jobs shell.
2002
99
UNIX: .
crontab-
crontab.
at.
nice, renice nohup.
,
6.1 6.2.
,
.
/var/spool/cron/crontabs/
$LOGNAME ( ) ,
.
, .
,
crontab,
/etc/cron.allow ( )
/etc/cron.deny ( ).
crontab., crond
crontab-.
crontab:
crontab <file> crontab- <file>;
crontab -l crontab- ;
crontab -e crontab- ;
crontab -d crontab- ;
, cron.
, mycronfile. crontab-
:
2002
100
UNIX: .
0 0 * * * tar cf /home/vasja/backup
/home/vasja/projects
0,15,30,45 1-8 * * mon-sat hack www.microsoft.com
projects
.
hack
15 8 ,
.
crontab-
MIN HOUR DAY MONTH DAYOFWEEK COMMAND
crontab-
COMMAND:
MIN (0-59);
HOUR (0-23);
DAY (1-31);
MONTH (1-12 jan-dec);
DAYOFWEEK (0-6, 0 sun-sat).
:
;
, ;
, ;
, .
COMMAND , Bourne Shell,
( /bin/sh -c <command>),
.
exec -,
stdout stderr
.
crontab- ,
crontab.
, cron,
.
UNIX root-.
2002
101
UNIX: .
6.4.2. at
at
shell (shell-),
, .
.
.
:
$ at 2300 jun 16 scriptfile
scriptfile 11 16
.
crond.
at , , ,
. at
/etc/at.allow, /etc/at.deny.
6.5. (nice,
renice, nohup)
6.5.1. nice
.
, root .
, . ,
nice. -20 () 19 () (
Linux).
.
nice:
nice [<number>] <command>
nice
10, ls
30:
$ nice ls alR / > listing.txt &
[1] 2099
$
2002
102
UNIX: .
, :
$ nice -5 ls alR / > listing.txt &
[2] 3012
, nice 25,
5 , nice .
:
# nice -20 /bin/sh
[1] 3777
, .
6.5.2. renice
renice,
root:
renice:
renice <priority> [<PID>]
$ renice 10 987
987: old priority 0, new priority 10
6.5.3. nohup
nohup (no hang up
) ,
shell,
nohup.out. nohup
,
, nohup .
nohup:
nohup <command> [> outfile] [&]
$ nohup ls alR / &
[3] 4176
$ nohup: appending output to nohup.out
nice nohup:
$ nice nohup ls alR / > listing.txt &
[4] 4476
, ps -x.
2002
103
UNIX: .
6.1. shell
jobid.
%jobid : % +
( ); - ; n
n.
<command>&
bg [%jobid]
fg [%jobid]
jobs [%jobid ...]
kill [-signo] %jobid
stop %jobid
wait %jobid
(
).
.
.
.
kill UNIX, .
( <Ctrl>+<Z>)
jobid
.
6.2. UNIX
2002
,
. root.
. root.
:
UID, PID, PPID, CPU, MEM, VSZ, TTY, STAT,
START, TIME, COMMAND.
. kill l .
.
stdin
at,
.
stdout. nohup.out.
104
UNIX: .
:
;
:
,
,
,
;
ps;
at, nohup, nice, renice;
, 6.1 6.2.
2002
105
UNIX: .
7. SHELL UNIX
shell.
shell , , .
shell.
shell-.
shell-, , , ,
/tmp, , -, expr, let, (( )), test, [ ], read, echo,
break, continue, return, exit, times, function, for, if, while, until, case,
kill, trap, select
2002
106
UNIX: .
7.1. Shell
shell
UNIX,
shell.
Shell ,
. ,
UNIX
, .
Shell
:
(:, #);
($name, $HOME, $1, $# .);
(:, =, [ ], (( )) .)
(let, test, echo, read .);
(if, for, while, case .);
( );
(kill, trap);
(readonly,
export).
, shell , ..
, shell
, C, shell-
.
shell
- :
, Pascal Fortran
. shell , , ,
.
shell:
Bourne Shell, C-Shell, Korn-Shell, Bash. ,
: C-Shell , Bourne
2002
107
UNIX: .
Shell . Bash
Bourne Shell C-Shell.
UNIX shell
; shell
: .profile, .login, .cshrc, .bashrc ( . 2.7
2.1).
7.1.1. shell
shell ().
script:
$ cat > script
ls l /
sleep 5
clear
<Ctrl>+<D>
,
5 ,
.
.
script:
$ sh script
script:
$ . script
script :
$ chmod 755 script
$ script
$ ./script
, $PATH
.
$PATH : .;
/bin; /usr/bin.
. ,
2002
108
UNIX: .
,
.
7.1.2. shell
shell
: <variable>=<value>.
$:
$ spisok=student
$ echo $spisok
student
#()
#()
#( echo)
(,
, , $IFS) ,
\
(
).
=
. , <variable>:=<value>
,
( ); <variable>+<value>
, .
shell ,
( .5.2):
$ spisok=student; spisok=comp$student; echo $spisok
compstudent
$ spisok=student; sorting=${spisok}sort; echo
$sorting
studentsort
:
, :
shell:
$
$
$
$
S=sort +2
$S compstudent|more
$S econstudent | pr
$S radiostudent > new-radiostudent
2002
109
UNIX: .
|, >, <, & ( ,
).
, ,
for, ,
.
7.1.3. shell
set env,
unset.
(readonly).
( . 2.6).
7.1.4.
``, shell (, bash, ksh)
$():
$ now=`date`
echo $now
Sun Feb 14 12:00:01 PST2001
$ now=$(date)
echo $now
Sun Feb 14 12:00:01 PST2001
,
:
$ myshells=$(cat /etc/shells); echo $myshells
/usr/bin/ksh /usr/bin/sh /usr/bin/tcsh /usr/bin/csh
7.1.5. shell.
shell
, . 7.2.
.
:
$ script compstudent econstudent radiostudent
2002
110
UNIX: .
()
: $1 compstudent; $2 econstudent; $3
radiostudent .. $9.
9
. shift n, n
, 9 ( 9). shift
.
( 9):
$*, $1$2...$N.
$# ().
, $0; $0
$#.
$$ PID shell .
$?.
7.1.6. /tmp
/tmp ,
. ,
,
.
$0$$, , , $0
, $$
, .
/tmp,
.
7.2. , , shell
7.2.1. (# :)
:,
() , .
2002
111
UNIX: .
0 (). shell
($,* ..),
.
UNIX #.
shell- shell
shell -.
, #/bin/sh.
7.2.2. (let, (( )), expr)
, shell
, , bash
((<expression>)) let <expression>,
, .
expr, let
(()) ,
expr.
expr ,
, .
shell.
expr:
$ expr 13 + 49 \* 2 + 1
112
$ expr \( 13 + 49 \) \* 2 + 1
125
$
expr ( 3
). sum3:
$ cat > sum3
expr $1 + $2 + $3
<Ctrl>+<D>
$ chmod 755 sum3;
$ sum3 13 49 2
64
$
112
UNIX: .
.
, (*, (, .) ,
shell . ,
* \(.
7.2.3. test [ ]
test shell-
if. shell (,
bash) test ,
.
test []:
test <>
[<>]
0,
, 1 .
: ;
; .
<op>.
: [<op> F], F .
true, F
<op>,
:
-a ( );
-e ( );
-s ( , >0);
-f ( );
-d ( );
-h ( );
- ( );
-b ( );
-p ( FIFO);
-S ( );
-r ( );
-w ( );
-x ( );
2002
113
UNIX: .
-u ( SUID);
-g ( SGID);
-k ( Sticky Bit) (.
man bash).
: [N <op> M], N, M
, <op> :
<expr1> -eq <expr2> (Equal To <expr1> <expr2>);
<expr1> -ne <expr2> (Not Equal To );
<expr1> -gt <expr2> (Greater Then );
<expr1> -lt <expr2> (Less Than );
<expr1> -ge <expr2> (Greater Than or Equal To
);
<expr1> -le <expr2> (Less Than or Equal To ).
: [S <op> R] [<op> S], S, R -
; <op> :
<string1> = <string2> ();
<string1> != <string2> ( );
-z <string> ( );
-n <string> ( ).
:
:
! ( NOT);
-a ( AND);
-o ( OR).
. mycp :
$2 (-w)
$1 (-r),
$2 $1 (>>),
(echo):
$ cat > mycp
if test -w $2 -a -r $1
then cat $1 >> $2
else echo cannot append
fi
<Ctrl>+<D>
$
2002
114
UNIX: .
2002
115
UNIX: .
myfunc myproc,
times.
.
$ function myfunc
> {
> ls l
> }
$ times myfnc
real
0m0.361s
user
0m0.050s
sys
0m1.145s
$
$
$ cat > myscript
ls l
<Ctrl>+<D>
$ times myscript
real
0m0.621s
user
0m0.100s
sys
0m1.253s
$
7.3. shell
7.3.1. for
for:
for <variable> [in <value1> <valueN>]
do
<commands>
done
for, do, done !
spisok (),
(+1), ,
(pr) (-h)
(lpr):
$ cat spisok
sort +1 students | tr -d 0-9 | pr -h University |
lpr
$
2002
116
UNIX: .
people , :
compstudent, econstudent, radiostudent, ...,
,
for.
spisok,
:
for file in compstudent econstudent radiostudent
do
sort +1 $file |tr -d 0-9 |pr -h University |lpr
done
shell . :
for file in *student # ,
student
do
sort +1 $file |tr -d 0-9 |pr -h University |lpr
done
in ,
, ,
,
( ).
spisok:
for file
do
sort +1 $file |tr -d 0-9 |pr -h University |lpr
done
:
$ spisok compstudent econstudent radiostudent
7.3.2. if
if:
if <command>
then
<commands-true>
[elif <command2>
<commands-elif>]
[else
<commands-false>]
fi
2002
117
UNIX: .
spisok ( $1
) , ,
( $#)
if.
spisok:
if test $# -eq 0
then
echo You must give a filename
exit 1
else
sort +1 $1 |tr -d 0-9 |pr -h University |lpr
fi
exit -
. ,
true=0 (
0). exit 1 1, ..
spisok .
7.3.3. while
while:
while <command>
do
<commands>
done
<command> ,
<commands>. while, do, done
.
2002
118
UNIX: .
spisok,
.
,
:
if test $# -eq 0
#
then
echo Usage: $0 file ... >&2 #
exit
fi
while test $# -gt 0
#
do
if test -s $1
#
then
echo no file $1 >&2
#
else
sort +1 $1 |tr -d 0-9 |pr -h University |lpr
fi
shift
#
#
done
7.3.4. until
while.
until:
until <command>
do
<commands>
done
<command> ,
<commands>. until, do, done
.
spisok :
if test S# -eq 0
then
echo Usage $0 file... >&2
exit
fi
until test S# -eq 0
do
2002
119
UNIX: .
if test -s $1
then
echo no file $1 >&2
else
sort +1 $1 |tr -d 0-9 |pr -h University |lpr
fi
shift
#
done
7.3.5. case
case:
case <string> in
<string1>)
<commands1>
;;
<string2>)
<commands2>
;;
...
*)
<commands*>
;;
esac
<string> = <string1>,
<commands1>, <string> = <string2>,
<commands2> , <string>
<stringN>, <commands*>
( * ).
case, string1), string2), *), esac
.
. spisok h (help),
:
myhelp = no
case $1 in
-h) myhelp = yes
shift
;;
-?) echo $0: no option $1
exit
;;
2002
120
UNIX: .
esac
if test $myhelp = yes
then
echo Help for $0 ...
...
fi
if test S# -eq 0
then
echo Usage $0 file... >&2
exit
fi
until test S# -eq 0
...
? . -?, .. , -h,
, ,
. shell,
?, *, [, -, ]. ,
case.
2002
121
UNIX: .
7.4. shell
, :
echo
, .
-v (verbose = ) shell
.
-x (execute) shell
(); .
.
7.5.
,
.
$#, , .
( case).
(,
), (,
*). ( ?
t p), .
t p ,
.
# /bin/bash
# ,
# , .
#
# .
# :
# -t (together) - ;
# -p (printer) - .
if test $# -eq 0
then
echo Usage: $0 file ... > &2
exit
fi
together=no
print=no
2002
122
UNIX: .
> /tmp/$0$$
if $print = no
then
cat /tmp/$0$$
else
lpr -c /tmp/$0$$
fi
rm /tmp/$0$$
exit
else
if test -s $1
then
echo no file $1 > &2
else
sort +1 $1 | tr -d 0-9|pr -h University \
> /tmp/$0$$
if $print = no
then
cat /tmp/$0$$
else
lpr -c /tmp/$0$$
fi
rm /tmp/$0$$
fi
shift
fi;;
esac
done
2002
123
UNIX: .
7.6. (kill
trap)
shell-
kill. .
kill [-sig] <PID1> <PID2> sig (1,2, ...)
, PID
(N) (%N).
,
, <Ctrl>+<C>,
,
( ).
trap:
trap:
trap <command> <signals>
trap <command1>; <command2>; <signals>
"".
, .
:
1 - "" ( )
2 - ;
(15 ), man trap.
trap ,
,
.
case $1 in
...
*) trap rm /tmp/*; exit 2 1 #
if test -s $1
...
rm /tmp/*
2002
124
UNIX: .
:
trap rm /tmp/* > /dev/null; exit 2 1 #
, /tmp/$0$$
null-.
7.7. select
select ,
.
#
clear
echo -e ? \c
read name
if test $name = $LOGNAME
then
echo !
else
echo ! : $LOGNAME
name=$LOGNAME
fi
echo Hi, $name!
#
echo , , $name?
select doroga in \
nalevo\
napravo\
pryamo;
do
if [-n $doroga]
then
echo -n : $doroga -
case $doroga in
napravo) echo () !
;;
pryamo)
echo ( `uname`)
!
;;
nalevo) echo !
;;
esac;
break;
2002
125
UNIX: .
else
echo $REPLY , 1,2 3.
fi;
done
#
echo , $name. $SECONDS
,
echo $(($SECONDS/60))
echo $[$SECONDS/3600] .
sleep 10
clear
2002
126
UNIX: .
7.1. bash
.
, info bash.
:
help bash;
info GNU
;
man , .
. source.
: true.
[ test, , ,
.
(( let , [ test.
[[ , , [.
alias .
break , n,
n .
builtin bash,
.
cd .
command ,
.
continue for, while, until,
select. n , n- , n >= 1.
declare (
, bash
).
dirs .
echo .
enable
.
eval ,
(
, ).
2002
127
UNIX: .
exec exec, . . ,
, , ;
- (wrapper scripts),
.
exit (, ,
)
export
, .
false .
getopts ,
; ,
.
help (built in)
bash. : help [<>...]. <> ,
, ,
.
kill ;
SIGTERM, ;
.
let ;
, .
local ( ) .
logout ,
.
popd .
printf (
).
pushd
.
pwd .
read
.
readonly .
return
.
select ,
( ,
2002
128
UNIX: .
, while
for).
set :
+<option>, -<option> -o <option>
(
)
$1 $2 ... $N. help set
set; bash,
info bash.
shift ($1 $N, $2
$N+1, $3 $N+2 ..).
sleep . :
sleep <number>[s|m|h|d], <number> ,
, , : , ,
.
source , (
,
).
test ;
(? ? ? ? . .),
.
times
shell , shell ( ).
trap .
true .
type ,
( , , . .).
ulimit
().
umask
. ,
;
chmod.
unalias .
unset .
2002
129
UNIX: .
7.2. shell
Shell
:
. bash
9 ( shift)
-.
( ) shell;
shell .
(),
( 0,
); .
PID shell;
, PID .
PID , .
, .
, .. $@=$1 $2 $3
, .
, .. $@=$1 $2 $3
7.3.
.
man 4 console_codes.
-e -n echo Linux
ESC-
.
\033 <ESC> ESC-.
ESC-
tput.
\033[#A # ;
\033[#B # ;
\033[# # ;
\033[#D # ;
\033[#E # ;
\033[#F # ;
\033[#G ;
\033[#;#H (, ).
2002
130
UNIX: .
\033[0m ;
\033[1m ( );
\033[2m (-, );
\033[4m (-, );
\033[5m ;
\033[7m ( , -- );
\033[22m ;
\033[24m ;
\033[25m ;
\033[27m .
\033[30m ;
\033[31m ;
\033[32m ;
\033[33m ;
\033[34m ;
\033[35m ;
\033[36m ;
\033[37m / ;
\033[40m ;
\033[41m ;
\033[42m ;
\033[43m ;
\033[44m ;
\033[45m ;
\033[46m ;
\033[47m / .
.
\033[1m\033[5m\033[36m
\033[1;5;36m.
1:
:
$ echo -en "\33[37;1;41m ATTENTION \33[0m"
2: ( root):
$ echo -en "\033[41m"
3: : ;
, ;
:
$ PS1="\033[1;5;32m\t\033[25;31m\w\$ \033[0m"
2002
131
UNIX: .
\007 ;
\033[10;#] ;
\033[11;#]
.
. , \033[10;55]\033[11;30]
\033[10;55;11;30].
:
5000Hz 200ms, :
$ echo -en "\033[10;5000;11;200]\7"
cat -v ( )
(<F1>, <> . .).
cat -v, ,
, <Ctrl>+<D>. ,
<F1> <> :
$ cat -v
^[OP
^[[C
,
.
2002
132
UNIX: .
:
shell-,
;
shell-;
,
shell-:
,
,
,
,
;
shell-
, :
,
,
;
;
, , :
a, b
;
-, , :
ls, -l, -d
(,
echo * , );
, , ,
. 7.7.
2002
133
UNIX: .
UNIX.
, , !
UNIX
Internet.
, ,
30- UNIX,
,
.
, 5 UNIX-
-,
.
,
,
, .
Linux.
UNIX
Linux.
X
:
UNIX;
LINUX;
ILU .. I love UNIX.
,
UNIX .
, :
UNIX ;
UNIX ;
UNIX ;
UNIX ;
UNIX.
2002
134
UNIX: .
8.
. UNIX: . .
.: , 1985. 318 ., .
9.
., .. : . . .: ,
1988. 392.
2002
135
UNIX: .
21. . : .
. .: , 2000. 720 .
22. . . . .:
, 2001. 352 .: .
23. . . UNIX. .: , 2001. 528 .: .
24. . Linux UNIX: shell.
: . . .: BHV, 2001. 464 .
25. / .. , .. . .:
, 2001. 544 .: .
26. . . : . .: ISMA,
2002. 104 .: .
1.
!RTFM (http://rtfm.vn.ua)
, ,
.
2.
(http://www.bookcase.ru) ,
10000 .
3.
(http://www.bib.com.ua/doc/) , ,
( ).
4.
IT.ACADEMY.LV (http://it.academy.lv)
Web- & Sys- -
.
web-, web-,
.
5.
Linux - (http://www.linuxve.chat.ru).
Linux.
6.
. . UNIX. 1991-1997.
(http://lib.ru/LINUXGUIDE/dgt-ux/).
7.
. . UNIX. ( ).
8.
OC UNIX (http://www.sparc.spb.su/Oio/)
.
2002
136
UNIX: .
9.
(!) (http://citforum.ru).
, !
10. . . Unix. (
).
11. Linux Mac OS X (http://www.softerra.ru/test/13693/).
.
12. OpenNet (http://www.opennet.ru) UNIX
: , , , , ,
.
13. . UNIX. .
. "Tricks of the UNIX Masters" by Russel G. Sage
(http://lib.ru/LINUXGUIDE/).
14. . sed awk. . ( ).
15. . shell (UNIX). .
( ).
16. (http://www.softerra.ru) .
17. . Linux . Linux Installation and
Getting Started Copyright (c) 1992-1994 Matt Welsh 205 Gray Street NE,
Wilson NC, 27893 USA mdw@sunsite.unc.edu Copyright (c), 1996,
"". ( Matt Welsh)
). ( ).
18. Linux
(http://webcenter.ru/~intec/alexey/hobcon.html).
.
19. Collection of Net references (http://www.sai.msu.su/~megera/htl/).
,
. (General Software, WWW,
Sysadmin Tools/Tips ).
20. GNU's Not Unix! - the GNU Project and the Free Software Foundation
(FSF) (http://www.gnu.org).
.
21. InfoCity (http://www.infocity.kiev.ua)
.
22. Internet FAQ Consortium (http://www.faqs.org) This archive contains
Usenet Frequently Asked Questions (FAQ) postings in Hypertext format
and in FTP archive textual format.
2002
137
UNIX: .
2002
138
UNIX: .
2002
139
UNIX: .
2002
140
UNIX: .
2002
141