תרגול מס' 8

Embed Size (px)

DESCRIPTION

תרגול מס' 8. קבצי script ניהול תהליכים ב- UNIX. לי-טל משיח litalma@cs.technion.ac.il. קבצי סקריפט. קבצי script הם קבצים אשר מכילים סדרה של פקודות shell תסריטים אלו מהווים מעין תוכניות הכתובות בשפת ה Shell תוכניות אלו נכתבות בקובץ רגיל ולאחר מכן מורצות - PowerPoint PPT Presentation

Text of תרגול מס' 8

  • ' 8 script -UNIX

    - litalma@cs.technion.ac.il

  • script shell Shell . source.

    >source clean clean clean ( ) :rm *.o mv ex1 ex1.old , object ex1 ex1.old .

  • C-shell C-shell: File name expansion Unix -C-shell . unix utilities C-shell " -" " "

  • CShell -- CShell C

  • CShell - repeat: CShell command count .

  • CShell - while:

    while ()end

    > source count100123456789# count10 script:@ i = 0while ($i

  • CShell - . ,

    :amortize.c, helloworld.c, prog.c, :foreach F (*.c)echo $Fend:amortize.chelloworld.cprog.c foreach - while break - continue - C. foreach ()end

    :foreach

  • if : : if ($a > 0) : if ($a == "kiko") c-shell:if () if () then[else]endif:if if (-f diary)lpr diaryendif "" diary, , if (-d $dir) thencd $dir..endif dir ,

  • - ( " ==) ~= : $, ?, ] [ ~= ~! .

    > if (end_with_z =~ *[zZ]) echo matchmatch> if ("no point in string" !~ *.*) echo "no point"no point> if ("no point - . - in string" !~ *.*) echo "no point"> (, >= ') . ==, =!, ~= ~! .

    > if (0123 != 123) echo NOTNOT

  • switch () case : [breaksw] case : [breaksw] . . .case : [breaksw][default: ]endsw

    switch ($F) case "*.c":lpr $Fbreaksw case "*.o":rm $Fbreakswendsw

    :switch

  • goto:- - :goto if (-f diary) then lpr diaryelse goto endprogendifif (-f diary2) thenlpr diary2endifendprog:

    diary , . , (!) goto .

  • script : " source -shell , "" executable , "

    > chmod +x myscript myscript executable. , :> myscript myscript script, shell , ""

  • script :

    #! /usr/local/bin/tcsh -f . which, -PATH- -shell , . shell -shell

  • setup setup script " . :.login - .cshrc - shell , , alias. :< cat .cshrcset path = ($path ./)set history=40alias ll 'ls -al'alias m 'more'alias . 'pwd'alias .. 'cd ..'alias ... 'cd ../..'

  • script. - script :$0 - $1 - ( : ${n} -n).$* - {- ${#argv .- ${argv[n]} n.

    print #! /usr/local/bin/tcsh -fforeach F ($*)lpr $Fend : > print a.c b.c prog.c

  • $ set str2 = ($ echo $str2This line is typed by the user> echo $str2[1]This> echo $str2[2]line str2 standard input - -cshell !!! - standard input

  • , -script -standard input script - "", -script , : pipelining -standard input

  • >cat footballAlon Miz. 2 23/10/93 Macabi-Haifa Macabi-Tel-AvivIzak Zoh. 1 12/11/93 Macabi-Tel-Aviv Hapoel-Beer ShevaRonen Ha. 3 27/12/93 Hapoel-Tel-Aviv Macabi-Tel-AvivReuven A. 2 12/11/93 Macabi-Haifa Hapoel-Tel-AvivEyal Ber. 1 20/11/93 Macabi-Haifa Macabi-Tel-Aviv Izak Zoh. 1 12/11/93 Macabi-Tel-Aviv Hapoel-HaifaAlon Miz. 2 26/10/93 Macabi-Haifa Beitar-JerusalemIzak Zoh. 2 12/12/93 Macabi-Tel-Aviv Macabi-HiafaAlon Miz. 2 23/12/93 Macabi-Haifa Macabi-Pet-TikvaRonen Har.3 27/11/93 Hapoel-Tel-Aviv Macabi-Haifa football . , , .

  • script player, football , " .

    , Alon Miz. :> player Alon Miz.Alon Miz. 2 23/10/93 Macabi-Haifa Macabi-Tel-AvivAlon Miz. 2 26/10/93 Macabi-Haifa Beitar-JerusalemAlon Miz. 2 23/12/93 Macabi-Haifa Macabi-Pet-Tikvatotal number of goals: 6

  • - script player - football , -script calc_total -standard input., script player -calc_total, football -standard input .

    :

  • > cat calc-total#!/usr/local/bin/tcsh -fset line = ($
  • script scores , football. , . , .>cat footballAlon Miz. 2 23/10/93 Macabi-Haifa Macabi-Tel-AvivIzak Zoh. 1 12/11/93 Macabi-Tel-Aviv Hapoel-Beer ShevaRonen Ha. 3 27/12/93 Hapoel-Tel-Aviv Macabi-Tel-AvivReuven A. 2 12/11/93 Macabi-Haifa Hapoel-Tel-AvivEyal Ber. 1 20/11/93 Macabi-Haifa Macabi-Tel-Aviv Izak Zoh. 1 12/11/93 Macabi-Tel-Aviv Hapoel-HaifaAlon Miz. 2 26/10/93 Macabi-Haifa Beitar-JerusalemIzak Zoh. 2 12/12/93 Macabi-Tel-Aviv Macabi-HiafaAlon Miz. 2 23/12/93 Macabi-Haifa Macabi-Pet-TikvaRonen Har.3 27/11/93 Hapoel-Tel-Aviv Macabi-Haifa

  • : > scores "Alon Miz." "Izak Zoh." "Ronen Ha." "Reuven A."1 Alon Miz. 61 Ronen Ha. 62 Izak Zoh. 43 Reuven A. 2

  • > cat scores#!/usr/local/bin/tcsh -f@ i = 1while ( $i >! temp @ i++endsort -n +2 -r temp | calc_placeif (-f temp) then rm -f tempendif " '

  • > cat calc_place#!/usr/local/bin/tcsh -fset line = ($
  • UNIX -Unix , , " ( ),

  • - Unix : foreground " : -prompt background " &: &

    >gcc Calc.c -o Calc .>gcc Calc.c -o Calc &> .

    .

  • jobs -shell -"+" "" -"-" "" -"+"

    < more forever.cmain(){ printf("I am going to run forever !!!\n"); while (1) ;}> forever &[1] 587> I am going to run forever !!!> jobs[1] + Running forever> kill -9 %1> [1] Killed forever>jobs[1] + Running cc prog.c[2] - Running troff -me big_doc.roff

    : ps -top. -man

  • :%n - n.%s - s.%?s - job s.%% - ( "+").%- - ( "-"). :%1%forever%for%?ever

  • [jobIDs] .fg [jobIDs]

    > more try_input.cmain(){ char str[20]; printf("I shall try to eat something"); scanf("%s" , str); printf("%s" , str);}> try_input&[1] 828> I shall try to eat something[1] + Suspended (tty input) try_input> jobs[1] + Suspended (tty input) try_input> fg %1try_inputgoodies goodies>

  • [jobIDs] bg [jobIDs]

    > jobs[1] + Suspended forever> bg %1[1] forever &> jobs[1] Running forever>

  • Ctrl-z - . " fg bg.stop jobIDs jobIDs> nroff -ms reportCtrl-z[1] + Suspended nroff emacs &[1] 982> forever&[2] 991> jobs[1] + Running emacs[2] - Running forever> stop %2> [2] + Suspended (signal) forever> jobs[1] - Running emacs[2] + Suspended (signal) forever>

  • Ctrl-c - kill -9 jobID jobID.

    , kill . "" (SIGTERM) " !" (SIGKILL). kill, , "-n", n . , 15 "". 9 " !". "" ( shut down ) , " !" ( )< forever&[1] 660> I am going to run forever !!!> kill -9 %fo> jobs>