Unix Notes

Embed Size (px)

DESCRIPTION

Βασικές εντολές του λειτουργικού συστήματος UNIX. Διαχείριση αρχείων.Ο διορθωτής κειμένου vi.Δικαιώματα και ασφάλεια.Προγραμματισμός φλοιού.

Citation preview

  • UNIX.

    1 1.0 UNIX. (logging in), (logging out). ' (commands). UNIX: date, who am i, who .. 1.1 (Logging in) (System Administrator) , login. RETURN ( ENTER). , () RETURN. .. guest, RETURN: login:guest RETURN : login:guest password:_ , . . UNIX , , . , UNIX : login incorrect ( ) login. 1.2 (Password) . UNIX . , . passwd .

    1

  • , , UNIX , . , , .. 1.3 (Prompt): $ , $ (cursor) . prompt UNIX (UNIX command interpreter), (Shell) . prompt $, % #. UNIX prompt, . 1.4 (logging out) prompt ($, % .) , . UNIX . . . UNIX : exit, logout logoff. ctrl-D . . (shell), . ctrl-D , login. 1.5 O (The shell command interpreter) $ . UNIX, ( ), UNIX. O (interface) (). . (Shell), . UNIX . RETURN, . :

    2

  • $ date = user Fri Mar 12 16:52:13 PST 1993 = system reply $ = other command? UNIX . (login name), : $ who am i = or whoami guest tty02 Mar 12 09:50 = system reply $ = other command? who. , : $ who = who is there? root tty01 Mar 12 09:40 guest tty02 Mar 12 09:50 makis tty1a Mar 12 10:16 equil tty8b Mar 12 11:45 $ = system reply , : $ qwho = wrong command qwho: not found = not understood $ 'qwerty' . . , ( (System Adninistrator or Super User). 1.6 : stty , . , stty, : $ stty erase "^H" = Ctrl-H $ stty kill "^u" = Ctrl-u $ Ctrl , Ctrl U. 1.7 : Del UNIX , , (interrupt key) .

    3

  • . Del, Break Ctrl C (Ctrl-c). 1.8 : ; UNIX . , ; : $ date; who am i = user Fri Mar 12 17:46:57 PST 1993 john tty8b Mar 12 12:28 = reply from system

    4

  • 2 2.0 . (standard output redirection). , , , .. UNIX (special characters or metacharacters). 2.1 / (Standard input and output) . UNIX , . , , . , . , , ' . , , (pipe). ' , . 2.2 (redirecting output to a file): > , echo. H , .. , . : $ echo "Hello there" = user Hello there = sends the result to screen $ , > ( ): $ echo "Hello there" > hello = sends all to file 'hello' $ . hello . . ..

    5

  • $ who am i > amnesia = sends the user identity to file 'amnesia' $ who > curr_users = sends current users to file 'curr_users' 2.3 : cat , . cat . cat . , . $ cat hello = let's look this file Hello there = contents of file 'hello' $ cat vals = open vals vals: No such file or directory = 'vals' does not exist cat . $ cat file1, file2 ... = for display $ cat file1, file2 ... > grfile = for merging 2.4 (Adding information to the end of a file) : >> ' . . $ echo " " > shmera = note the quotes $ cat shmera $ echo " ." > shmera; cat shmera . $ shmera echo . >> >. $ echo " " > shmera $ echo " ." >> shmera = add $ cat shmera . $ echo . echo : $ echo "test for writing = first line > two lines at once" = second line test for writing = output two lines at once $

    6

  • > , prompt . . prompt , prompt , . 2.5 (Redirecting standard input): < , tolower . $ tolower = command name and CR ABCDEFGH = key in abcdefgh = this is the result $ = Ctrl-D to exit echo : $ echo " THIS IS A BOOK" > capitals $ tolower : $ tolower < capitals = accept input from 'capitals' this is a book = reply 2.6 (Listing file names): ls , ls. ls . $ ls = let' s list our files amnesia capitals curr_users hello shmera test $ = that's all for now 2.7 (Copying a file) 2.7.1 cp (With cp) cp , . . $ cp hello ciao = hello to ciao $ cat ciao ello there

    7

  • 2.7.2 cat cat. , , , . $ cat shmera = display file shmera $ cat shmera > weather = shmera is copied to weather $ cat weather $ . $ cat shmera = shmera + weather =? $ cat shmera >> weather $ cat weather $ , . . 2.8 : mv mv. cat cp mv . $ mv shmera 123 $ To shnmera 123 ls $ ls 123 amnesia capitals ciao curr_users hello Weather $ 2.9 : rm H rm. .

    8

  • $ ls = two files are removed 123 amnesia capitals ciao curr_users hello Weather $ . $ rm letter $ rm: letter nonexistent $ : $ rm c* $ ls. $ ls amnesia hello . ; 2.10 (metacharacters) UNIX . x (metacharacters). "*" , "apitals", "iao" "urr_users". "*" , . , "a*" "alpha" "alarm", "a". "*" ( ). : $ rm * = think about it! -i (interactive), . y, n.

    9

  • $ rm -i * amnesia: = reply as you wish $ , ? , . $ cp hello test1 $ cp hello test2 $ ls = here are the files amnesia hello test1 test2 $ ? rm. $ rm test? $ls amnesia hello $ (test1 test2). H ( ). $ rm ?? rm: nonexistent = system message $ "*" "?" rm.

    10

  • 3 UNIX (UNIX file system), . , cd , pwd .. . 3.1 (Directories) UNIX . : ) , ) , ) . . , . . 3.2 (Home directory) , (home directory). O . 3.3 : pwd , . , ... pwd (current directory). $ pwd = where are we? /usr/guest = so that's where we are $ . "/" (slash) (root) UNIX. O usr, ( ). "/" usr guest , . 3.4 : cd cd (Change Directory). H , ' .

    11

  • $ cd = let's go home $ pwd = where are we? /usr/guest = here we are , cd . $ cd /usr = go to users $ pwd = where are we? /usr = we are there $ cd = let's go home $ pwd /usr/guest /usr : $ ls /usr fred guest mary $ . . $ cd / = let's go to the top $ pwd = where are we? / = this is the root $ ls. $ ls = same as 'ls /' bin boot dev etc lib mnt tmp usr $ , . UNIX. . bin UNIX, cat, cp, ls ..

    12

  • $ ls /bin as awk cat cc chgrp chmod cmp cp $ ls /bin 3.5 . cd .. . $ cd .. ; pwd = go in the above directory /usr $ cd; pwd = go back home /usr/guest $ cd ../../; pwd = climb to levels / = the top $ , . cp . 3.6 -: mkdir -. mkdir. H - . $ cd; pwd; ls = verify everything /usr/guest = home amnesia hello $ mkdir newdir1 $ mkdir newdir2 $ ls amnesia hello newdir1 = new sub-directory newdir2 = new sub-directory . newdir1. $ date > curr_date $ cp curr_date newdir1 = copy file to newdir

    13

  • $ ls newdir1 curr_date $ curr_date. guest newdir1. : $ date > newdir1/curr_date. $ 3.7 : cd guest newdir1. $ cd; pwd /usr/guest = home $ cd newdir1 ; pwd = go to newdir1 /usr/guest/newdir1 $ newdir1. newdir2, (parent directory). $ pwd /usr/guest/newdir1 $ cd newdir2 newdir2: bad directory = error $ , newdir2. $ cd ../newdir2 = up and down $ pwd /usr/guest/newdir2 = here we are $ : $ cd /usr/guest/newdir2 = absolute path newdir2 guest newdir2. , "/". : (relative path) (absolute path) . 3.8 : cp cp hell newdir2 . $ pwd

    14

  • /usr/guest/newdir2 $ cp ../hello . $ ls = note single period hello $ hello , ciao: $ cp ../hello ./ciao = note periods $ ls hello ciao $ : $ cp /usr/guest/hello /usr/guest/newdir2 $ cp /usr/guest/hello /usr/guest/newdir2/ciao 3.9 : mv mv . cp. H . *** *** ( cp rm) mv. . 3.10 mv , . $ mv /usr/guest/newdir1 /usr/guest/exec 3.11 . , , rmdir . $ pwd /usr/guest/exec $ rm * = delete all files $ cd .. = go to the parent directory $ rmdir exe = remove directory exec $ 3.12 : ls -l ls. ls -l, -, o .

    15

  • $ cd ; pwd /usr/guest = return home $ ls -l total 3 -r--r--r-- 1 guest group 20 Mar 11 14:12 amnesia -rw-r--r-- 1 guest group 5 Mar 11 14:15 hello drwxr-xr-x 2 guest group 512 Mar 11 14:18 newdir1 drwxr-xr-x 2 guest group 512 Mar 11 14:20 newdir2 . , d (directory). newdir1 newdir2 . "-". .

    16

  • 4 . VI 4.1 VI vi vi filename A filename vi . vi , filename ' . 4.2. VI vi : i) . ) . : esc : . i : . 4.3 VI vi , , vi . vi : i : . a : . : , . 4.4 ENTER vi 512 . enter. 4.5 A vi . : :w vi. :x unix. :w new_name .

    17

  • 4.6 VI vi : :q A vi :q! 4.7 . 4.7.1 : i) M . , . ii) n+ : n . n- : n . iii) nG : n. 4.7.2 H : . L : . 4.7.3 nw : n . nW : n . nb : n . nB : n . 0 : . $ : . 4.8 ctr-U : . ctr-D : . ctr-F : ( ). ctr-B : ( ).

    18

  • 4.9 vi : r : , r enter. J : J. . 4.10 : :set number : . :set nonumber : . :1, 2 nu : . ..

    1,5 nu 5 . ,$nu .

    4.11. 4.11.1 : nx : n

    . nX : n

    . 4.11.2 : ndw : n . 4.11.3 : dd : 2 d

    . n1,n2d : n1 n2. .. :1,5d

    5 . :1,$d .

    4.11.4 vi :

    19

  • u : . 4.11.5 vi ( vi ) . : . () : . 4.12 / vi : :r filename : filename . filename .

    20

  • 5. 5.0 . (password) . 5.1 : passwd UNIX passwd. $ passwd Setting password for user: guest1 Old password: = current password Choose password you can choose whether you pick a password or have the system create one for

    you. 1. Pick a password 2. Pronounceable password will be generated for you Enter choice (default is 1): Please enter new password (at least 1 character): New password: = new password Re-enter password = type new password again . , . 8 . . 5.2 (multi-user) , , . , , , . :

    21

  • $ pwd /usr/guest1 $ ls -l /usr/guest2 = let's list the file total 6 -rw-r--r-- 1 guest2 group 45 Jun 4 17:17 blocknotes -r--r--r-- 1 guest2 group 197 Apr 22 17:12 calendar -rw-r--r-- 1 guest2 group 112 Oct 9 16:22 photex -rwxr-xr-x 1 guest2 group 312 Jun 4 17:17 prog_c drwxr-xr-x 1 guest2 group 197 Apr 22 17:12 signex $ cat /usr/guest2/blocknotes These are the minutes of the next meeting. $ 5.2.1 UNIX, : (permission) (ownership). / (directory) (file). : , () . 1) : . : .

    2) : . : .

    , , .

    3) : : .

    5.2.2 , UNIX . (owner) .

    (group), ,

    . (other), .

    , UNIX. ls -l ..

    22

  • $ pwd /usr/guest1 $ ls -l total 6 -rw-r--r-- 1 guest1 group 45 Jun 4 17:17 blocknotes -r--r--r-- 1 guest1 group 197 Apr 22 17:12 calendar -rw-r--r-- 1 guest1 group 112 Oct 9 16:22 photex -rwxr-xr-x 1 guest1 group 312 Jun 4 17:17 prog_c drwxr-xr-x 1 guest1 group 197 Apr 22 17:12 signex O d , r , w e . (guest1 ), guest1 ( group) (others). d r w x r w x r w x me friends others O - . 5.3 : chmod chmod . $ cat > whoami = create a file who am i $ = end with CTRL-D . $ ls -l whoami -rw-r--r-- 1 guest1 group 12 Mar 21 00:36 whoami $ chmod : $ chmod +x whoami $ ls -l whoami -rwx-r-xr-x 1 guest1 group 12 Mar 21 00:36 whoami $ . . who am i whoami .

    23

  • 2.3.1 chmod r, w x: + - . u user (you) g group of users (friends) o others (others users) , whoami : $ chmod ugo+rwx whoami $ ls -l whoami -rwxrwrwx 1 guest1 group 12 Mar 00:36 whoami ( ) : $ chmod go-w whoami $ ls -l whoami -rwxr-xr-x 1 guest1 group 12 Mar 0:36 whoami

    . (super-user)

    . 5.3.2 UNIX . : 4 2 1 0 ' :

    24

  • 6 4+2=6 5 4+1=5 3 2+1=3 7 4+2+1=7 chmod (0-7 ). , . : $ chmod 755 whoami whoami , , . 5.4 : chown chgrp chown chgrp . . chown. $ pwd /usr/guest1 ls -l whoami -rwxr-xr-x 1 guest1 group 8 Mar 20 23:46 whoami $ chown guest2 whoami; chgrp informix whoami $ls -l -rwxr-xr-x 1 guest2 informix 8 Mar 20 23:46 whoami $ chown guest1 whoami guest1: Not owner guest2, whoami.

    25

  • 6. UNIX 6.1 6.1.0 . UNIX: ksh, sh csh . 6.1.1 ksh, sh csh. UNIX : sh Steve Bourne ksh David Korn csh Berkley.

    ksh sh ( ksh sh), C C . : .

    '

    , >> . ,

    2> (2 , 1 0 .

    (sh ksh C ). 6.1.2 ' . 18 C. 6.1.3 =. . var1=/usr/guest/bin set var1=/usr/guest/bin mhkos=100 set mhkos=100

    26

  • var2= set var2= ( ) $ . ls -l $var1 : /usr/guest/bin echo " = $mhkos" : = 100. (string) *, ?, >, > |, . . $ ` \ $, ", ` \. , . . file1, file2 file3 : echo * file1 file2 file3 echo * "*" "*" file1 file2 file3 * * message="hello there" set message="hello here" echo $message "$message" '$message' : hello there hello there $message \n, \c \t echo , tab . . `. ` . : shmera=`date` set shmera=`date` echo $shmera . . . users=`who|wc -l` set users=`who | wc -l` echo " $users " : .

    27

  • 6.5 . # . 6.6 UNIX : [] ... :

    [] "-"

    . ...

    : " ". , UNIX. , ' : [k]sh ... csh ... (command file) (shell procedure) script file ( ). test1 : echo `date;who` : [k]sh test1 csh test1 "[k]sh" csh", chmod. $1,$2...$9 ( -positional parameters). prog1 : who | grep $1 : [k]sh prog1 guest : who | grep guest To $# ($#argv C). $0, $* $0.

    28

  • phones : 8824567 5678901 2829748 lookup . : grep "$1" phones grep . : lookup " " append : echo "$1\t\t$2" >> phones echo "o $1 " : append " " 6890456 insert, : echo "$1\t\t$2" >> phones sort phones > temp mv temp phones echo "o $1 " delete : sed /$1/d phones > temp mv temp phones 6.7 UNIX $? ($status C) . 0 . UNIX test 0 1 $? ($status) .

    29

  • test -r filename test -r filename echo $? echo $status 0 . test ( sh ksh) ( csh). : expr . : 1) . expr $a = $b ($a == $b) expr "$my_name = "john" ($my_name" =="john") 2) + - \* / % () . expr 1 + 2 3 expr 1+2 1+2 expr 5 "*" 3 15 a=`expr $a + 1` a 1 @ a += 1 c a b @ c = $a - $b a 1 c @ c = $a++ c a @ c += $a c a @ c *= $a c a @ c /= $a 6.8 6.8.1 if if (test ) if ( test) then then elif then else elif fi endif A test then. else (elif).

    30

  • if (test -f arxeio) if ( -f arxeio) then c1 then c1 else c2 else c2 fi endif c1 c2. lookup : if [ $# -ne 1 ] then echo "\n usage is: lookup name \n" else mess = grep "$1" phones if [ -z "$mess" ] then echo "$mess not found" else echo "$mess" fi fi 6.9 6.9.1 while while while () do done do done. end month=1 set month=1 while [ "$month" -le 12 ] while ($month < 12) do echo $month echo $month month=`expr $month + 1` @ month += 1 done end 6.9.2 for foreach for i in 1 2 .. foreach i (1 2, ..) do done end

    31

  • H i in ( csh ). for i in 1 2 3 4 foreach i (1 2 3 4) do echo $i echo $i done end

    32