�� ����� ��������� ����� ��� �������� ��������� ��� ����������� ������� Unix ��� �� ��������� ���. �� ������������ ���� ������������ ����������� ��� ��� �� ������� �� ������������ ���� ���������� ��� ��� ������ �������. �������� �������������� ��� ��� ��������� ��� ������������ ����� ��� ����������� �������� ��� ����� ��� Windows. ���� ����� �������������� DOS �� ���������� ����� ������ ������.
M� ��� �������� Unix � ������ Unix-like ������������ �� ��� �������� ���������� ������������ ����������. ������ ��� ���� ����� �� linux, �� Mac OS X, �� Solaris, �� FreeBSD ��� ����.
�� ����������� Unix ����� ����������� ���������� ���� �� �������� ���������� �������� ��� ���������� ������������ ������� ��� ������ �� ����������� �� �������� ���� �� ������ ������ �����������. ����� ������� ���� � ��������� ��� ���� ���������� �� Unix �� �� �������� ����������� ��� servers. ��� �� ���� �������� �� ������������ ��� �� internet ������� ���� ��� Unix. ��������, ���� ���� ��� �������� ������ email � �������� ������ ����������, ����� ������ ������ ��� ������ ����� ������� server ��� ������ Unix.
��� ���� ���� ��� ������ ������� ��� ���������� ��� ��������� �����:
����� �� ����� ������� �� ������������/����� Unix?���� ����� ���� ����� ��� �� ����� �������� �� ���� ������. � ������� ���������� ������������ ���������� ����� ������ ������ ��������� ��� ������� ���� ������. To ����� ������� �����, ����� �� ��� ����� ������� �� ����� Unix;
To Unix ����������� ������ �� ��������� ������� ��� ��������� �� "���� ��������". �� ��� mainstream ����������� ����������� ������ ������������ ��� ���������� ��� ��� ������, ���������� ��� ������ ���, ������������ �� ������ �����������. �� Unix �� ��������� ����� �� ������ ������� ��� ������ �����������������. ������������� ���������� ������ �������������� ���:
��� ��������� �� 1969 ���� ��� ��������� ��� Bell Labs ��� AT&T, o Ken Thompson ��� o Dennis Ritchie ������������ �� UNIX, ��� ������� ����� �� �� MULTICS, �� ����� �������� ����������� �������. ���� ���� ������� 5����, �� ��������� ��������� �� ����������� ����, ����������� �� ������������� ��� ��� ����������� ����������, ������ ��� ������ ��� � ������ C.
��� ���� ��� ��������� ��� 1970, � ��&T ������� �� UNIX ��� ����������� ������������. �� �������� ���������� ��� ������ ��� UNIX ��� ������ ����� ������� ����� ���� ����������. �� ������� ����� �������������� ���� ������ ��� UNIX ��� ��� ������ ���� ��������� ��� ������������ ��� Berkeley. ���� � ��������� ��� UNIX ���������� Berkeley Software Distribution (BSD). ��� �� ������� 15 ������, �� ��� ��������� �������� ��� ������������ ������������ ��� ���������� ��� BSD Unix.
������ �� ���� ��� ������ ��������� ������ Open Source Unix ��� ���� �����. ��� ���� ��� ��������� ��� '80, � Richard Stallman �������� �� GNU Project, ��� ������ ��� ���� �� ����� �� ���������� ��� ������ ���������� ��������� ������������ ����������. ���� ����� ��� ��������� ��� �90, �� GNU ���� ������ ������, ������ ������ ������� ������������ ������� (kernel). �� ��� ������� ���� ����������� � �������� �� ���� ��������� Linus Torvalds, ��� ����� ���� ����������� ���� kernel ��� ������ ���� �������� ����� ���. ��� ��� ��������� ��� ��� �������� �� ������ �� ����� GNU/Linux �����������.
������� ��� ���� �������, �� ��������� ��� ������������� ��� Berkeley ����������� ��� ������� �� 98% ��� ������� ������ ��� UNIX ���� �������������� ��� ��� ����������� ��� �������� ��� ���������� �� ��� ��������������� �� Open Source. ������ ��� ��� ����� ������ ������� �� ��� AT&T, �� ������������ ���������� ��� �� 1994 �� BSD ����� Open Source. ������ ��� ������ ��������� ��� BSD, �� ��� ������ ����� �� FreeBSD, �� NetBSD, �� OpenBSD ��� �� Mac OS X.
To ����������� ������ ��� Unix
��� ������ ���� �� ����������� �� �� OpenBSD, �� ��� �� �������� ��� ������� ���� ����� ��������� �� ����� �� �� �������� Unix.
���� ������� �� ����� ������ ��� ����������� ����� � �������:
[21:45] eirik@sandbox ~> _
��� �������� ��� � ����������� ���� ����������� ���� ��� �������� ��� �������� ��� ������� ������ ���. ���������� ��� ������� ���� prompt ��� ������� �������. ��� prompt ������������ ����������� ���� � ���, �� ����� ������, �� ����� ��� ���������� ����� ��� � ������� ���� ������ �����������. ����� ��� prompt �������� �� �������� ��� ������� ���.���� ������� ��� Unix, �� ������� ����� ������� ��� �������� �����:
������ [������1] [������2] ...
���� �� �������� ����� ����������� ������� �� ��� ������� ��� ���� �������.������, �������� ��� �� Unix ����� ����� case sensitive, ������ ������� �������� ������ ������ ��� ��������� ���������.
passwd : ������ ������� who : �������� ������������ ������� whoami : �������� �������� ������ finger : ������������ ����������� ��� ���� ������� finger [user] : ����������� ��� ��� ������ user banner [message] : ��������� �� ������ ��� banner date : �������� ����������� ��� ���� cal : ���������� ��� ��������� ���� cal May 1992 : ���������� ��� ��� ��� 1992 fortune : fortune cookies! lynx [url] : Terminal Browser wget [url] : Command-line download manager
ls : (list) �������� ������� ��� �������� ������� touch [file] : ���������� ��� ������� file mkdir [dir] : ���������� ��� ������� dir ls -F : �������� ������� ������� �� �� ����� ���� ls -l : �������� ������������ ��� ���� ������ ls -a : �������� ��� ��� ��������� ������� (dotfiles) ls -Fal : ���������� ���� ��� ������������ man [command] : ���������� ����������� ��� ������ ������ whatis [command] : ����������� ��� ��� ���������� ��� �������
� ��������� ������� ��� Unix ������� ���� ��� ��� Windows. �� ������� ��� ��������:
C:\WINDOWS\system32\alg.exe : Windows /usr/local/bin/vim : Unix
��� Unix, ���� ��� C:\> �������������� �� / ��� �� ������������ ��� ������ (root) ������. ������, ��� �������������� ���� �������� �������� (\) ��� ��� ������������ ��� �������.pwd : �������� ��������� ������� cd [directory] : ������ �������
������� �� ��������� ������������ ��������� �������:
/ : root ������� . : ������ ������� .. : o ������� "��� ����" ������� ��� ��������� (parent) ~ : � �������-����� (home) ��� ������
�� ��������� ����� ������� �� �����������:[6:52]eirik@bender local/bin> cd /usr/local/bin/ [6:52]eirik@bender local/bin> pwd /usr/local/bin [6:52]eirik@bender local/bin> cd ../../bin [6:53]eirik@bender /usr/bin> pwd /usr/bin
������� ����� �������...
ls [directory] : �������� ������������ ��� ������� cp [source] [dest] : ��������� ������� (copy) mv [source] [dest] : �������� � ����������� ������� (cut) rmdir [dir] : �������� ������� rm [file] : �������� ������� ln -s [source] [dest] : ���������� link ��� ������ (shortcut)
touch {file1, file2} : ���������� ��������� ������� ls file? : ��� �� ������ ���� ������ ��� ��� �������� �� file �� ���� ������������ ��������� ��� ����� ls ??? : ��� �� ������ �� 3 ���������� ls * : �������� ���� ��� ������� ��� ������ ls f* : ��� �� ������ ��� �������� �� f rm * : �������� ���� ��� ������� ��� ������ echo [message] : �������� ��� ��������� echo [message] > [file] : ���������� ��� ��������� �� ������ cat [file] : �������� ������������ ��� ������� ���� ����� echo [message2] >> [file] : ���������� �������� ��� ����� ��� ������� ls -l > [file] : ���������� ��� ������������� ��� ls ��� ������ more [file] : �������� ��� ������. ��� ������� ���� ������ less [file] : ���� ��� �� more head [file] : �������� ��� ������ 10 ������� ��� ������� head -n 30 [file] : �������� ��� 30 ������ ������� ��� ������� tail [file] : �������� ��� ���������� 10 ������� ��� ������� grep [key] [file] : �������� ��� ������� ��� ��������� �� key wc -l [file] : ������� ��� ������� ��� ������� ��� ������� ls -l | grep ssh : ����������� ��� grep �� �� ������������ ��� ls ls -l | grep ssh | wc -l: ������ ������� ��� �������� �� grep
����� ������� ������� �� �������� �� ��������������� ������ �������� ��� ������ �������. �������� ����� ��������� ����������� ��� �� �� �������� ����, ����� ������ �� ���������������� ��� vim. ��� �� ��������� ��� vim
vim
���� ��� �� ��������� ��� ������������ ������
vim [file]
��� �� ��������� �� �������� �������, ������� ��� ���� �� insert ��� �� ������ ��� insert mode. ���� �������� ���� ��� �� ���� ����������� ������������ ��� Windows. �������� ������� ���� �� insert ��������� ��� replace mode, ���� ���� ��� ��� Windows.
���� � ������ ��� vim ��� ������������ �� ���� �� ������� modes. ��� �� ������ ��� ���� ������� �� escape. ���� ����� �� vi mode. �� ���� �� mode ������ ���� ������� ����������� �� ������ ������. �� ����� ������� ����� ������� ��� vi mode:
x : �������� ���� ��������� i : insert mode � : �������� ���� ����� ������� ��� ������� ��� insert mode u : undo dw : �������� ����� dd : �������� (cut) ��� �������� ������� ��� �������� 5dd : �������� 5 ������� v ��� y : ������� (copy) ���� ������ ��� �������� (visual mode) p : paste / : ���������
��� �� vi mode, ������ ��� ���� ��� ���� ��� ���� ������' ( : ). �� ����� ��� ���� �������� �� ��� ���������� ��� ��� ���� ������ ���� �� �������� �� ������� �������. ���� ����� � ������ ������� ��� vim. ����� ��� ��������� �������� ��� ��� ����� �� ������� ���������� �������� ������������ �������� �� ����� ����� ������� (����������� ���� ��� ������� ��� sed). �� ����� ������ ������������:
w [file] : ���������� ��� �������� �� ��� �������� file w : ���������� ��� �� ������ ����� q : ������ ��� �� vim q! : ������ �� override (����� ������ ��� �������) wq : ���������� ��� ������ w! : ���������� �� override wq! : ���������� ��� ������ �� override %s/old/new/ : ������������� ������ %s/old/new/c : ������������� ������ (interactive)
��������� ��� ���� ����� ��������� ��� ������� ��� vim. ��� �� ������ ����������� ��� ��� ������������, ������� � ������������� vimtutor, ��� interactive tutorial ��� ��� ������� �����������:
vimtutor
�� ��� ������ ������� �������� ls -l, �� ����������� �������� ��� ������
-r-xr-xr-x 3 root bin 325024 Mar 2 2006 /bin/sh
��� ��� ���� ������ ��� ������� ���. ��� �� ������������ �� ��������� ����� �� ��������, ������ �� ����� ��� ����������� �� ���������� (permissions) ��� Unix. ��� Unix, ���� ������ ������ 3 ���������� �������. O ���������� ��� ������� (user � u), � ����� ������� ��� ������� (group � g) ��� ���� �� ��������� ������� (others � o).�� ���� ��������� ������� �������� ���� ���� permissions: read, write ��� execute (r-w-x). ����������� ��� �� ���������� ���������� ��� �������� ��� �������� ��� ls -l ���������� ������� �� permissions ��� ���������, ��� ������ ��� ��� ���������.
�� ���������� ���� ��� ������� �� ��������� �� permissions �� ��� ������. ���� ������ �� ������ ���� ������ ������� �� ���������� ��� �������, �� ��� ������ chmod. �� ��� ���������� ������� �� ����������� �� �������� ������ (����������) ��� ������� ���� �����, ��������
chmod g+w file
��� ���������� ��� �� ����������� �� �������� ��������� ��� ���� ���������� �������chmod o-r file
��� ��� �� ����������� ���� �������� ��� ��� ����� ��� ���� ���������� ������� ��������chmod go-rwx file
�����, ��� �� ������� ��� ������ ���������� ��� �����, ��������chmod +x file
���� �������� ������ �� ������������� �� permissions ��� Unix ������ �� ����� �� ����: �������� ��� �������� ����������� ������ permissions ��� �������
| r | w | x |
| 4 | 2 | 1 |
�������� ���� �� ��������������� ��� �� permissions ��� ��� ������������ ��������� ������� �� �� �������� ��� ������� ��� ������������ ��� �������������� ����������. A� ��� ���������� � ��������� ���� ��������� read ��� execute, ���� ��� ������������� ��� ������ 5. ���� read ��� ����� 4. Write ��� execute 3, ���. �������� ���� ��� �������� ������ ��� ������������ ��� �� permissions ��� 3 ���������� ������� �� �� ����� user, group, others. � ������� ����� ��������� ������ �� permissions ��� ��� ������ �� �������������� ��� �� chmod. ��� ����������
chmod 777 file
����� ����� ���������� �� ����� ���� �������. ������
chmod 640 file
����� ���������� ������ ��� ��������� ���� ��������� ��� ��������� ���� �����, ��� ������ �������� ��� ����� �� ��������� �������. T�
chmod 755 file
����� ���������� ��������� ��� ��������� ���� ����� ��� ���� ���������� �������.
ps : �������� ���� ��� ������������ (processes) ��� ������. ps auxw : �������� ���� ��� processes ��� ����������. ps auxw | grep eirik : �������� ���� ��� processes ���� ������. top : real-time ���������� ��� processes ��� ����������. pgrep program : �������� ��� PIDs ���� ������������. kill <PID> : ����������� ������������ pkill program : ����������� ������������ �� ���� ��� ��������.
���� ��� ������ ������ ��� ���� ��������� �� C ��� ������ hello.c.
�������� �� �� ������� compile ��� ������ ������� ���������
cc hello.c -o hello
��� ��� �������� �������� �� ��������� ��� ���������
./hello
����� ���� ��������� ���������� ������ path ��� �� ����������. ���������� ���������� compilers ��� interpreters ��� �������� ��� Unix:
cc : C compiler c++ : C++ compiler f77 : Fortran77 compiler perl : Perl interpreter python : Python interpreter javac : Java compiler java : Java Virtual Machine
Shell (�������) ���������� ������ �� ��������� ��� Unix ��� ��������� ������� ���� ������ ��� �� ����������� �������. ���������� ���� �� ������������ �� ��� kernel (������) ��� ������������ ����������, ����� ����� �� ��� "���������" ������� ��� ������������ ����������. �� shell ������ ����� ����������� �� �������� ������� ��� ��� ������, �� ��� ��������� ��� ��� �������� �� ����� �� ����������� ��� ���� ������������. �������� ��������, ����� �� ��������� ��� ������� ��� ������ ������� (���� �������� ��� windows, shell ���������� �� ������� ���������� ��� ��� ����� ���� �� ������� �����������).
�������� ������ ������������ ����������� ��� shell ��� Unix ��� ����� ������� �� �������� ���� ��� ���� shells ��������� �� ���� �������. ���������� ��������� ������ ��� �� ��� ������:
echo $SHELL
�������� ���� ������ �� ��������� ��� ��� shell ����� ��� shell ��� ����� ����������� ���
ksh
��� ���� ��������� �������� ��� ������
exit
�������� �� ��������� �� default shell ��� ����������� ��� ���������
chsh
� ������ ������� ��� ��������� �� �������� ����������
set x = 5 set y = 5 ; set name = �������
�������� ��� �������� �� ���������� ��� ���� ���� ���������� �������� ��� �������� ($) ������� ��� �� ����� ���:
echo $x echo "O $name ���� $x ����"
�� � ��������� �������� ���������� ���� �������� �� ������� ��� ������� ������������ �� ������� @ ���� ��� set
@ x = $x * 2 echo $x
������ ����� ������������:
set sentence = "�� ��������� �� ���� �������� ����������" set file = /etc/hosts ; cat $file set command = ls && set options = "-Fal" && $command $options
�� ����� backquotes `` �������� �� ��������� �� output ������� �� ����������
set name = `whoami` set now = `date` set files = `ls`
��� �� shells ����� �� ���������� �� ��������� ��� �� �������� ������� ���� ���
������ ������. ��� ������ ��� �������� ��� ������� ��� ������� ���������� shell script.
�� �������������� ��� ������ hello ��� �� �������� ���� ��� ������
echo "Hello, World!". �������� �� �������� �� ������ �� ��� ������
csh hello
����� ������ ������� �� ������� ������� ��� script ��� ��� �������� �����:
#! /bin/csh # ��� ������ echo "Hello, World!"
���� � ����� ������ ������ ������� �� shell ��� ������� �� ������ �� script. M������� ���� �� �������� �� script �� ���������� ���������.
chmod +x hello ./hello
� ������ if
#! /bin/csh set directory = `pwd` set number = `ls -l | wc -l` if ( $number == 0 ) then echo "��� �������� ������ ���� ������ $directory" else echo "�������� $number ������ ���� ������ $directory" endif
� ������ while
#! /bin/csh mkdir ~/testdir cd ~/testdir set i = 0 set max = 10 while ( $i < $max ) set file = file.$i echo "���������� ��� ������� $file" echo "E���� �� ������ $file." > $file # ������ ��� i @ i = $i + 1 end
� ������ foreach
#! /bin/csh # ���������� ��� foreach foreach x (1 2 3 �������� ������� "������� �����������" ) echo $x end
#! /bin/csh # ������� ���������� echo �� ������� ���� ���� ���������� �������: set count = 0 foreach username (`ls /home`) @ count = $count + 1 echo "${count}�� �������: $user" end
�� ������� ��� csh ��������� �� ����
set array = (����� ������� ����� ������� 5 6 7) echo $array[1] ; echo $array[2] ; echo $array ; echo $array[*]
�� ������ ��� ��������� ��� ������ array ������� ��� ��� ��������� #array
echo ${#array}
H foreach ������� �� ��������� ��� �� �������� ���� ������� array
#! /bin/csh # O ������� argv ���������� ��� �� �������� ��� �������� ��� script set count = 1; echo "�������� �� $0 ������ ${#argv} ��������" foreach arg ( $argv ) echo To ${count}� ������ ����� �� $arg end
A� ������� ��� ��������� ����������. �� �������� ��������� ������� �� ������ ��� ������� �� ����� ������.
#! /bin/csh if ( ${#argv} == 0 ) then # ��� ���� ����� ������ ������ echo $0 : ��� ����� ������� ��������. exit 1 endif set file = $argv[1] if ( ! -e $file ) then echo $0 : �� ������ $file ��� �������. exit 1 endif # �������� ��� ����� ��� ������� if ( -d $file ) then echo To $file ����� �������. else if ( -f $file ) then echo To $file ����� ������. set size = `du -hs $file | awk '{print $1}'` set lines = `wc -l $file | awk '{print $1}'` echo To ������ $file ���� ������� $size. echo To ������ $file ���� $lines �������. else echo To $file ����� ���� ����. endif # ������� ��� ����������� ��� ������� if ( -r $file ) then echo � ������� $user ���� ���������� ��������� ��� $file. else echo O ������� $user ��� ���� ���������� ��������� ��� $file. endif if ( -w $file ) then echo O ������� $user ���� ���������� ������ ��� $file. else echo � ������� $user ��� ���� ���������� ������ ��� $file. endif if ( -x $file ) then echo O ������� $user ���� ���������� ��������� ��� $file. else echo O ������� $user ��� ���� ���������� ��������� ��� $file. endif
��������� �� ������� ������� ������� ������� ��� Unix
cat file | mail <email> : �������� ������� �������� �� email at -f [script] [time] : ��������������� ��������� script. ftp [host] : ftp client
����� ��� ���������� ��� ��� ��� ����������. ��������� ��� ��� �� ���������� ���� �� ����� �� ������ �� ������ ������ Unix ��� ����� ��� ���� �� ���������� �� ���������� ����. ��� ��� �������, � ��� ������� ������ ��'���� ����� � man ;).