Shell & shell script USING csh/tcsh FreeBSD 練 B 磊 callmelei
Part 1 Shell ::Outline:: Shell 什 什 Shell shell shell Csh tcsh 不 C shell
Shell 什??...NO??...YES -- getty --> login --> shell --> logout -- ^ ----------------------------------------------
什 Shell 令 便 錄 利
shell sh ( /bin/bash ) bash--- (bourne-again shell Linux shell) ksh (Kornshell AT&T Bell lab. 來 bash) zsh ( ksh 來 更 shell) tcsh ( C Shell 更 ) csh ( FreeBSD shell Bill Joy BSD UNIX ) shell? echo $shell % cat /etc/shells
/etc/passwd shell, 不 root shell subshell % less /etc/passwd root:x:0:1:super-user:/:/sbin/sh daemon:x:1:1::/: nobody:x:60001:60001:nobody:/: ftp:x:1000:1000:ftp:/:/usr/sbin/nologin
shell 數 懶 XD FreeBSD default shell csh/tcsh Linux default shell sh/bash unix-like OS
Which shell will be introduced?? csh/tcsh
Differences between csh and tcsh csh = shell command interpreter with a C-like syntax tcsh = C shell with file name completion and command line editing DOSKEY (command line editing), history list 令 行 令 (file name completion) Tab 不 令 Ctrl+d 列 sparc csh ssh sparcid@sparc20.cc.ncu.edu.tw
Shell 不 shell 不 Shell 類 sh/bash ksh/zsh csh/tcsh.profile.login.profile.login.cshrc.login.logout
/etc/csh.cshrc /etc/csh.cshrc -> /etc/csh.login -> ~/.cshrc -> ~/.login % more /etc/csh.cshrc # $FreeBSD: src/etc/csh.cshrc,v 1.3 1999/08/27 23:23:40 peter Exp $ # # System-wide.cshrc file for csh(1). set prompt = %B%m [%~]-%n-> # set nobeep # 不
識 路 路 / 路 不 /. # 錄.. # 錄 ~ # 錄 shell 路 路 which command
Csh/tcsh 令 / 念 History 數
令 (1) 令 ls ps aux netstat -an 連 令 mkdir ~/backup ; cp ~/.* ~/backup/;logout
令 (2) 令 grep google samplecom.txt && more samplecom.txt grep google samplecom.txt echo no cc prog.c echo compile error && -> than ->else 令類 令 (built-in command) (utility) %which command
令 (2) 例 % fetch http://tinyurl.com/erug8 % grep google erug8 && who % grep test erug8 echo no
令 (3) 令列 command options arguments ls -al./ rm -i test.txt echo hello world?? man command info option -h or --help
/ < Input Redirection << keyword input redirection > Output Redirection >> Appending Output Redirection % ls./ > result.txt # 料 料 % echo appending text >> result.txt % mail xxx@hotmail.com < result.txt % mail xxx@hotmail.com << End_Of_File > this is a test mail > this is a test mail > this is a test mail > End_Of_File
念 command1 command2 %ls wc w # 數 %cat /etc/passwd %cat /etc/passwd sort # /etc/passwd %history less # history 歷 令
*? [ ]
例 % touch a.c; touch b.c;touch cc.c;mkdir dir1;mkdir dir2 % ls *.c a.c b.c cc.c % ls?.c a.c b.c % ls [ac]* a.c b.c % ls [A-Za-z]*
History set $history = n history 歷 令!! 行 令!n 行 n 令!string 串 string 令 行 event 數!?string? 令行 串?string? 行
History 例 % history 1 8:00 pwd 2 8:01 ls 3 8:02 date 4 8:04 grep google erug8 && who 5 8:06 rm test 6 8:07 history %!3 # 行 3 令 %!! # 行 令 %!grep # 串 行
alias name command unalias name alias alias name.cshrc
% touch test % rm test % alias rm rm -i % touch test % rm test remove test? % who % date % alias ww date;who % ww Thu Oct 5 08:40:53 UTC 2006 callmelei ttyp6 Oct 5 01:42 (v254-44.ntcu.net)
什 (foreground jobs) 行 (background jobs) 令 & 令 Ctrl+c 令 Ctrl+z 令
例 % vi test & % jobs [1]+ Stopped vi test % fg % vi test Ctrl+z % jobs [1]+ Stopped vi test % fg
令 Echo pushd popd dirs repeat exec exit rehash unhash hashstat
令 -- echo echo echo n echo echo $shell echo hello world
令 pushd popd pushd directory 錄 stack 錄 popd directory 錄 stack 錄 Stack?? (first in first out) % pushd /etc /etc ~ % pushd /usr/ports /usr/ports /etc ~ % popd
令 --dirs dirs 令 pwd 令 錄 dirs 來 料 錄 directory stack ) % pushd /etc # 令 /etc ~ # % pushd /usr/ports /usr/ports /etc ~ % dirs /usr/ports /etc ~
令 --repeat repeat 行 數 command 懶 repeat 3 sync % repeat 3 echo hello world # 令 hello world hello world hello world # % repeat 3 sync # 料
令 --exec exec command 不 subshell, 行 理
令 --exit exit (n) echo $? or echo $status shell script
令 --rehash unhash hashstat rehash path 數 令 更 行 令 unhash 令 hash path 錄 降 令 度 hashstat 5 hits, 1 misses, 83% 參數 行 令 參數 行 令
(sigle quote)( ) 兩 不 (double quote)( ) 數 (back quote)(`) 行
例 % set color=red # 數 % echo $color # 數 red # % echo $color # $color # % echo There are `who wc -l` people online # There are 1 people online #
離 tcsh 1.) 2.)100 歷 令 立 錄 ~/.tcsh_local ~/.logout # ~/.logout date >> ~/.tcsh_local history 100 >> ~/.tcsh_local clear
/bin ls /bin/???? /bin a 料 ls l /bin/a* last 料 last grep pcbsd
Part 2 Shell script ::Outline:: Shell script 什 什 Shell script shell script
shell script??
JOKE (COMDEX) 說 車 ( 車 ) 度 度 車 來 烈 了 論 車 車 車 1. 車 理 (Crash) 兩 2. 路 識 車 3. 車 理 路 來, 路 4. 車 車 車 度 5. 車 車 95/98 車 NT/2000 車 不 6. 金 (Macintosh ) 車 (Sun ) 度 易 易 練 路 7. 例 量 量 度 車 8. 9. 10. 車 理 拉 車 車 車 11. 不 車 不 車 車 車 車 不 車 車 車 (Crash) 數 12. 車 車 車 車 車 不 13.
什 shell script script == Like dos 令 行
什 shell script 理 連 令 令 來 歷 vs c vs c++
EDITOR ee vi 離 令 :esc->a->a
shell hello world mkdir cctraining; cd cctraining ee first.csh #!/bin/csh #program: # this is show hello world #date:2006/10/1 echo Hello World!! 離 ee : esc -> a -> a csh first.csh fetch http://tinyurl.com/k2htr csh k2htr
說 什 來 shell script vi(m) ee # # 不 行 shell script chmod +x filename.csh;./filename.csh csh filename.csh
數 set variable_name = value 數不 數 array variable set name = ( value1 value2 value3 value4 ) 行讀 數 set name = $< 數 @ x = $x + 1 @ x = $x * 2
數 例 % set color = red % echo $color red % set color = ( red yellow blue ) % echo $color red yellow blue % set color = $< orange % echo $color orange
數 例 (2) % set x = 1 % x = $x + 1 x: Command not found. % @ x = $x + 1 % echo $x 2
shell script 利 csh 來 1 100 % ee second.csh #!/bin/csh set x = 1 while($x < 100) echo $x @ x = $x + 1 end 離 ee esc -> a ->a
說 數 @ x = $x + 1 不 $ $
流 (1) foreach...end $ ee foreach.csh #!/bin/csh forech color (red yellow green) echo one color is $color'' end %csh foreach.csh one color is red one color is yellow onc color is blue
流 (2) Goto fetch http://tinyurl.com/emf66 $ ee goto.csh #!/bin/csh echo firstline goto second echo thirdline second: echo secondline % csh goto.csh firstline secondline
流 (3) if...then...else...endif fetch http://tinyurl.com/zh6mw $ ee ifelse.csh #!/bin/csh echo enter a number set number = $< if ($number < 0) then echo negtive else if ($number == 0) then echo zero else echo positive endif
流 (4) switch...case...endsw fetch http://tinyurl.com/kzmbo $ ee switch.csh #!/bin/csh echo n enter a number(1 for date,2 for who,3 for pwd, other for illegal choise): set number = $< switch($number) case 1: date breaksw case 2: who breaksw case 3: pwd breaksw default: echo illegal choice breaksw endsw
流 (5) while...end fetch http://tinyurl.com/fwadp csh fwadp 9 % ee while.csh #!/bin/csh set x = 1 while ($x <= $1) set y = 1 while ($y <= $1) @ v = $x * $y echo -n $v @ y ++ end echo @ x++ end
令 -d $var 數 錄 true -e $var 數 true -f $var 數 true (I.e., not a directory) -o $var 行 shell 立 true -r $var 行 shell 讀 true -w $var 行 shell true -x $var 行 shell 行 true -z $var true
Debug $?,$status 令 行 csh -x name.csh --Debug Option
Debug -- example % pwd /usr/local % echo $? 0 % pwd -h pwd: illegal option -- h usage: pwd [-L -P] % echo $? 1
Debug -- example $ vim debug.csh #!/bin/csh set color = (red blue green) set one = 1 set two = 2 echo debug test start echo $color echo $one echo $two % csh -x debug.csh set color = ( red blue green ) set one = 1 set two = 2 echo debug test start debug test start echo red blue green red blue green echo 1 1 echo 2 2
數 1+2+3... 累 數 fetch http://tinyurl.com/edzqo csh edzqo shell script 來 /etc/passwd 行 串 ~/test.txt no data fetch http://tinyurl.com/pftz8 csh pftz8
參 料 http://netlab.cse.yzu.edu.tw/~statu e/freebsd/docs/completed/84.shtml. htm http://netlab.cse.yzu.edu.tw/~statu e/freebsd/ http://www.twbsd.org/cht/book/inde x.php http://netlab.cse.yzu.edu.tw/~statu e/freebsd/docs/csh/ http://www.freebsd.org.hk/html/oth er/shell1.html
http://www.mgt.ncu.edu.tw/~dino/uni x/shell03.htm http://ccca.nctu.edu.tw/~chliao/cgibin/archives/2004/10/unix_tcsh.html Differences between csh and tcsh http://www.eng.hawaii.edu/tutor/csh. html#scripts The C Shell tutorial
freebsd Pink forever http://www.youtube.com/watch?v=n D-EkYxnFtI