C Shell l A command processor thats run

  • Slides: 60
Download presentation

C Shell l A command processor that's run in a text window § allows

C Shell l A command processor that's run in a text window § allows the user to type commands which cause actions, § can also read commands from a file, called a script. l It supports § filename wildcarding, piping, here documents, § command substitution, variables and § control structures for conditions and iteration l Its interactive features and overall C style § made it easier and faster to use. l http: //www-cs. canisius. edu/ONLINESTUFF/UNIX/shellprogramming. html 3

csh on Mac OS X 4

csh on Mac OS X 4

sh vs csh #!/bin/sh if [ $days -gt 365 ] then echo This is

sh vs csh #!/bin/sh if [ $days -gt 365 ] then echo This is over a year. fi #!/bin/csh if ( $days > 365 ) then echo This is over a year. endif 6

sh vs csh #!/bin/sh i=2 j=1 while [ $j -le 10 ]; do echo

sh vs csh #!/bin/sh i=2 j=1 while [ $j -le 10 ]; do echo '2 **' $j = $i i=`expr $i '*' 2` j=`expr $j + 1` done #!/bin/csh set i = 2 set j = 1 while ( $j <= 10 ) echo '2 **' $j = $i @ i *= 2 @ j++ end 7

시작 파일 l . login 파일 § 환경 변수 설정 echo -n “Enter your

시작 파일 l . login 파일 § 환경 변수 설정 echo -n “Enter your terminal type: ” set termtype=$< set term=vt 100 if (“$termtype” != “ “) set term=“$termtype$” set path=(. /bin /usr/local/bin) stty erase “^? ” kill “^U” intr “^C” eof “^D” set history = 40 set prompt = “! %” l . cshrc 파일 § 이명 설정 등 개인적인 설정 9

변수: 예 %set flag %echo $flag %set color=red %set name=“Graham Glass” %echo $name %set

변수: 예 %set flag %echo $flag %set color=red %set name=“Graham Glass” %echo $name %set … display all local variables %set verb=sing %echo I like $verbing %echo I like ${verb}ing 13

리스트 변수: 예 %set colors=(red yellow green) %echo $colors[1] %echo $colors[2 -3] %echo $#colors

리스트 변수: 예 %set colors=(red yellow green) %echo $colors[1] %echo $colors[2 -3] %echo $#colors %set colors[4]=blue %set colors = ($colors blue) %echo $colors %set colors = $colors black %echo $colors %set girls=(sally georgia) %set boys=(harry blair) %set both=($girls $boys) 15

지역 변수(local variable) l 사전 정의 지역 변수(Predefined local variables) 16

지역 변수(local variable) l 사전 정의 지역 변수(Predefined local variables) 16

명령줄 인수(Command line arguments) # grep -i unix $1 # foreach i ($*) grep

명령줄 인수(Command line arguments) # grep -i unix $1 # foreach i ($*) grep -i unix $i end 17

환경 변수(environment variable) l 환경 변수 생성 및 배정 l 사전 정의 환경 변수(Predefined

환경 변수(environment variable) l 환경 변수 생성 및 배정 l 사전 정의 환경 변수(Predefined environment variable) % setenv name word 18

식(Expressions) # echo -n “do you like the C shell ? ” set reply

식(Expressions) # echo -n “do you like the C shell ? ” set reply = $< if ($reply == “yes”) then echo you entered yes else if ($reply =~ y*) then echo I assume you mean yes endif 19

식: 예 %set a =2*2 %@ a = 2 * 2 %echo $a %@

식: 예 %set a =2*2 %@ a = 2 * 2 %echo $a %@ a = $a + $a %echo $a %set flag = 1 %@ b = ($a && $flag) %echo $b # echo -n “enter the name of file …” set filename = $< if (! (-w “$filename”)) then. . . 22

C vs csh // C groups from the left // prints 4 int i

C vs csh // C groups from the left // prints 4 int i = 10 / 5 * 2; printf( "%dn", i ); // prints 5 i = 7 - 4 + 2; printf( "%dn", i ); // prints 16 i = 2 >> 1 << 4; printf( "%dn", i ); # C shell groups from the right # prints 1 @ i = 10 / 5 * 2 echo $i # prints 1 @i=7 -4+2 echo $i # prints 0 @ i = ( 2 >> 1 << 4 ) echo $i 24

파일-관련 식 l File-oriented expressions § § § § l -e file -r file

파일-관련 식 l File-oriented expressions § § § § l -e file -r file -w file -x file -o file -z file -f file -d file file file merely exists and is readable by user is writable by user is executable by user is owned by user has size 0 is an ordinary file is a directory Boolean operators § ! § && § || -- negate -- logical and -- logical or 25

파일-관련 식: 예 l l l if (! -e somefile) then # does not

파일-관련 식: 예 l l l if (! -e somefile) then # does not exist if (-f somefile && -w somefile) then # the file exists, is not a directory and I can write it if (-e somefile) then grep $1 somefile else echo "Grievous error! Database file does not exist". endif 26

이명(Alias) l 명령어 alias [word [string]] unalias pattern l 유용한 것 alias alias ls

이명(Alias) l 명령어 alias [word [string]] unalias pattern l 유용한 것 alias alias ls rm rm h ls-l mroe ls -F rm -i ‘mv !* ~/tomb’ history ls -l more 27

제어 구조 l 프로그래밍 언어처럼 만들기 위해 n n C Shell script CGI programming

제어 구조 l 프로그래밍 언어처럼 만들기 위해 n n C Shell script CGI programming in Internet # foreach color (red yellow green blue) echo one color is $color end 30

제어구조: foreach 31

제어구조: foreach 31

제어구조: 예 l if. csh # echo -n ‘enter a number: ‘ set number

제어구조: 예 l if. csh # echo -n ‘enter a number: ‘ set number = $< if ($number < 0) then echo negative else if ($number ==0) then echo zero else echo positive endif 32

제어구조: repeat % repeat 10 echo hi there 34

제어구조: repeat % repeat 10 echo hi there 34

제어구조: 예 l menu. csh #!/bin/csh echo menu test program set stop = 0

제어구조: 예 l menu. csh #!/bin/csh echo menu test program set stop = 0 while ($stop == 0) cat << ENDOFMENU 1 : print the date 2, 3 : print the cwd 4 : exit ENDOFMENU echo -n ‘your choice ? ’ ‘ set reply = $< switch ($reply) case “ 1” : date breaksw case “ 2”: case “ 3”: pwd breaksw case “ 4”: set stop = 1 breaksw default: echo illegal choice breaksw end 35

제어구조: switch 36

제어구조: switch 36

제어구조: while l while … end while (expr) command. List end 37

제어구조: while l while … end while (expr) command. List end 37

제어구조: 예 multi. csh # set x = 1 while ($x <= $1) set

제어구조: 예 multi. csh # set x = 1 while ($x <= $1) set y = 1 while ($y <= $1) @ v = $x * $y echo -n $v “ @ y++ end echo “ “ @ x++ end %multi. csh 7 1 2 3 4 5 2 4 6 8 10 3 6 9. . . 6 12 7 14 “ 38

샘플 프로젝트: Junk #! /bin/csh set file. List = ( ) set list. Flag

샘플 프로젝트: Junk #! /bin/csh set file. List = ( ) set list. Flag = 0 set purge. Flag= 0 set junk = ~/. junk foreach arg ($*) switch ($arg) case “-p” : set purge. Flag = 1 breaksw case “-l” : set list. Flag = 1 breaksw case -* : echo $arg is an illegal option goto error breaksw default: set file. Flag = 1 set file. List = ($file. List $arg) breaksw end 40

샘플 프로젝트: Junk @ total = $list. Flag + $purge. Flag +$file. Flag if

샘플 프로젝트: Junk @ total = $list. Flag + $purge. Flag +$file. Flag if ($total !=1) goto error if (! (-e $junk)) then ‘mkdir’ $junk endif if ($list. Flag) then ‘ls’ -lg. F $junk exit 0 endif if ($purge. Flag) then ‘rm’ $junk/* exit 0 endif if ($file. Flag) then ‘mv’ $file. List $junk exit 0 endif exit 0 error: . . . 41

예: 파일 수 세기 #count number of files and directories in argument 1 #or

예: 파일 수 세기 #count number of files and directories in argument 1 #or current directory #usage: numfiles [directory] if ($#argv == 0) then set dir = “. ” else set dir = $argv[1] endif if (! -d $dir) then echo $0: $dir not a directory exit 1 endif 42

예: 파일 수 세기 echo $dir: @ fcount = 0 @ dcount = 0

예: 파일 수 세기 echo $dir: @ fcount = 0 @ dcount = 0 cd $dir foreach file (*) if (-f $file) then @ fcount++ else if (-d $file) then @ dcount++ endif end echo $fcount files $dcount dirs 43

인터럽트 처리 l onintr label 인터럽트가 발생하면 해당 레이블로 제어 이전 # onintr control.

인터럽트 처리 l onintr label 인터럽트가 발생하면 해당 레이블로 제어 이전 # onintr control. C while (1) echo infinite loop sleep 2 end control. C: echo control-C detected 46

자기호출 스크립트 % headers # Recursive script to print the head of each file

자기호출 스크립트 % headers # Recursive script to print the head of each file # in the current directory and in every subdirectories foreach i (*) if (-f $i) then echo "===== $i ======" head $i endif if (-d $i) then (cd $i; echo direcory $i; ~/lecture/sp/sample/headers) endif end 47

디버깅 l csh -vx script [arguments] % csh -v menu. csh % csh -x

디버깅 l csh -vx script [arguments] % csh -v menu. csh % csh -x menu. csh % csh -vx menu. csh 48

향상된 기능들 l A shortcut for command re-execution l The { } metacharacters l

향상된 기능들 l A shortcut for command re-execution l The { } metacharacters l Filename substitution l Redirection l Built-in 53

역사(History) l 명령어 숫자 붙이기 % set prompt='! %'. . . ! means the

역사(History) l 명령어 숫자 붙이기 % set prompt='! %'. . . ! means the event number l 명령어 기억 크기 % set history = 40 % set savehist = 32 % history l 역사 읽기 history [-rh] [number] 54

명령어 재실행 l A shortcut Re-execute the previous command with a slight modification ^pat

명령어 재실행 l A shortcut Re-execute the previous command with a slight modification ^pat 1^pat 2 %cc fil. txt %^file l Metacharacter { } a{b, c}d means abd acd %cp /usr/include/{stdio, signal}. h /tmp 56

파일이름 대치 l 파일 이름 대치 무효화 § $noglob 변수 set %echo a* p*

파일이름 대치 l 파일 이름 대치 무효화 § $noglob 변수 set %echo a* p* %set noglob %echo a* p* l 매치되지 않는 상황 § $nonomatch 변수 set %echo a* p* %echo a* b* echo: No match. %set nonomatch %echo a* b* 57

리디렉션 l Redirect stdout %cc a. c > errors l Redirect stdout & stderr

리디렉션 l Redirect stdout %cc a. c > errors l Redirect stdout & stderr %cc a. c >& errors l Redirect only stderr %(cc a. c > out) >& errors l Protecting file overwrite %ls -l errors %set noclobber %cc a. c >& errors: File exists 58

파이프(pipe) l Pipe stdout command 1 | command 2 %cc a. c| more l

파이프(pipe) l Pipe stdout command 1 | command 2 %cc a. c| more l Pipe stdout & stderr command 1 |& command 2 %cc a. c |& more l Pipe only stderr (process 1>file) |& process 2 %(cc a. c > out) |& more 59

기타 l Terminating login - Control-D - exit - logout %set ignoreeof %^D Use

기타 l Terminating login - Control-D - exit - logout %set ignoreeof %^D Use “logout” to logout %logout l Source Execute a script on the original shell %source. login 60