1. switch 的用法,注意每一個 case 必須要以 breaksw 結尾   否則會繼續執行下一個 case 的命令 (1) 另外, $< 的意思是取得使用者的 stand input (2) echo 若加上 -n 的選項,則游標會停留在該行最後 echo -n "Input one color: " set STOPLIGHT = $< switch ($STOPLIGHT) case red: echo "red" breaksw case orange: echo "orange" breaksw case green: echo "green" breaksw default: echo "you input $STOPLIGHT" endsw -------------------------------------------------------------------- 2. 利用 set 來取得變數, set ABC = "I am ABC" 也可以利用 `command` 來取得命令 且外,case 也可以用萬用字元 * 來代替 set VER = `uname -r` switch ($VER) case 5.5: echo "run the setup of $VER" breaksw case 5.3: echo "run the setup of $VER" breaksw case 5.*: echo "like 5.x" breaksw case 4.*: echo "like 4.x" breaksw default: echo "no idea" endsw -------------------------------------------------------------------- 3. if 的語法,比較數字 set n1 = 1 set n2 = 2 if ($n1 == $n2) then echo "$n1 Equal $n2" else echo "$n1 Not Equal $n2" endif -------------------------------------------------------------------- 4. if 的語法,比較字串 set n1 = abcdef set n2 = abcde if ($n1 == $n2) then echo "$n1 Equal $n2" else echo "$n1 Not Equal $n2" endif -------------------------------------------------------------------- 5. if 的語法,比較相似的字串 set n1 = abcdef set n2 = abcde if ($n1 =~ $n2) then echo "$n1 Like $n2" else echo "$n1 Not Like $n2" endif -------------------------------------------------------------------- 6. if 的語法,比較數字的大小 set n1 = 1 set n2 = 2 if ($n1 > $n2) then echo "$n1 > $n2" else echo "$n1 < $n2" endif -------------------------------------------------------------------- 7. 每分鐘執行一次的程式 # mm 等於當天時間的【分鐘】數 set mm = `date | cut -d' ' -f4 | cut -d: -f2` if ( -r $0.out ) then rm $0.out touch $0.out else touch $0.out endif while ( $mm <= 16 ) set mm = `date | cut -d' ' -f4 | cut -d: -f2` echo "$mm now is `date`" sleep 60 #echo "$mm now is `date`" >> $0.out end echo "Over" >> $0.out -------------------------------------------------------------------- 8. 一個迴圈的範例,並且利用 expr 去作加的動作 迴圈的語法如下: foreach number (1 2 3) echo $number end set counter = 0 while ($counter <= 10) echo "sleeping for 5 seconds" sleep 5 counter = `expr $counter + 1 ` end -------------------------------------------------------------------- 9. 設定一個用當天月份與日期作為檔案名稱的程式 如今天是 10/02 , 則 $prefix 會等於 該程式 + 1002 date.csh1002 set prefix = `basename $0``date '+ %m%d'` echo $0 echo $prefix -------------------------------------------------------------------- 10. 移除在 foreach 迴圈內指定的檔案內的 font 字串 foreach file ([b,e,g,h,s]*.html) echo -n "Processing $file, remove the line number `grep -n font $file`" # $log 表示這個 $file 有幾個 font 字串 set log = `grep -c font $file` if ( $log == '0' ) then echo ", pass $file" else # 先找出該檔案的第一次出現 font 的行數,如果 3,則 $cmd = 3d set cmd = `grep -n font $file | cut -d: -f1 | head -1`d # 利用 sed 去執行刪除的動作,並把結果輸出到 ${file}1 sed $cmd $file > ${file}1 # 如果 ${file}1 沒有資料,則 passing if ( -z ${file}1 ) then echo " , ${file}1 is zero" else cp ${file}1 $file rm {$file}1 echo " , $file remove ok" endif endif end # 後來看過 sed 的更進一步用法,發現先前寫的太笨了,試試這個 # sed /font/d $file > ${file}1 # 一次 OK, 我真是大笨蛋 -------------------------------------------------------------------- 11. 功能:將指定的檔案中,出現第一次【回】的那一行,加上 xxxx foreach file (sky*.html) set filetitle = ftitle # 主要部份為 sed 部份 s/^ *// 表示將該行第一個字元前的空白刪除 echo "`grep 回 $file | head -1 | sed -e 's/^ *//'`" > $ftitle # 將剛剛那一行,再插回去 head -1 $file > ${file}head sed 1d $file > ${file}1 cat $ftitle >> ${file}head cat ${file}1 >> ${file}head cp ${file}head $file rm ${file}1 rm $ftitle rm ${file}head echo "$file ok" end -------------------------------------------------------------------- 12. 一個實際建立一個 ftp server 的程式   裡面包括許多應用,相當有參考價值 ( 未完成 ) set path = ( /usr/bin /usr/sbin ) # set true = `grep -c ftp /etc/passwd` if ( $true == 0 ) then echo "no ftp user in your system" echo -n "do you want to create the ftp user? " set answer = $< if ($answer == 'y' || $answer == 'Y') then set maxid = `sort /etc/passwd | tail -1 | cut -d: -f3` echo $maxid set newid = `expr $maxid + 1` echo $newid echo "/usr/sbin/useradd -d /home1/ftp -u $newid -s /etc/false ftp" endif else echo "Good. Your system already has the ftp user. " set ftphome = `grep ftp: /etc/passwd | cut -d: -f6` echo $ftphome endif if ( -z $ftphome ) then echo "ftphome must be non-null" exit 2 endif if ( $ftphome == "/usr" || $ftphome == "/" ) then echo "ftphome can't be / or /usr" exit 2 endif # create the ftp home directory if ( ! -d $ftphome ) then echo "mkdir $ftphome" endif echo "Setting up the ftphome for SunOS `uname -r`" if ( ! -d $ftphome ) then echo "mkdir -p $ftphome/usr/bin" endif cp /bin/ls $ftphome/usr/bin chmod 111 $ftphome/usr/bin/ls chown root $ftphome/usr/bin chmod 555 $ftphome/usr/bin if ( -r $ftphome/bin ) then mv -f $ftphome/bin $ftphome/Obin endif ln -s usr/bin $ftphome -------------------------------------------------------------------- 13. 取得該使用者的 UID if ( $#argv == 0 ) then echo "$0 usage: $1 username" exit 2 endif set uid = `grep $1 /etc/passwd | cut -d: -f3` echo $uid -------------------------------------------------------------------- 14. 將指定檔案內的 html 取代成 htm foreach file ( *.html ) echo "Processing $file ..." sed s/html/htm/ $file > ${file}1 cp ${file}1 $file rm ${file}1 end -------------------------------------------------------------------- 15. 一個簡簡單單的範例,看看就好 #!/bin/csh -f echo ................. echo WELCOME to \* TAPE COPY \* echo ................. echo Enter your name: # $< can read from stand input set name = $< echo " " echo Hi $name \! set D = `date` echo Today\'s date is $D[1] $D[2] $D[3] if ($D[1] == Mon) then echo ------------------------------------------------------------- echo Today is $D[1]day $name, it\'s time to copy your directorys\! echo ------------------------------------------------------------- else echo ------------------------------------------------------------- echo Today is $D[1]day $name, no tape copies today\! echo ------------------------------------------------------------- endif -------------------------------------------------------------------- 16. 一個 finger 的程式 set FINGER = "/usr/ucb/finger" if ( -x $FINGER ) then if ( $#argv == 0 ) then cat << TAG --------------------------------- Hahahah .... --------------------------------- TAG else $FINGER "$*" endif else echo "Cannot find finger on this system." endif -------------------------------------------------------------------- 17. 取得變數的方法 set W = `who -r` echo $W[9] -------------------------------------------------------------------- 18. 更改檔案名稱,將 *.html --> *.htm # rename *.html to *.htm echo -n "This will change *.html to *.htm. Can I continue ? (y/n) : " set input = $< if ( $input != "y" && $input != "Y" ) then echo "Ok. Quit..." exit 2 endif foreach file ( *.html ) echo "Processing $file to `basename $file .html`.htm " mv $file `basename $file .html`.htm end -------------------------------------------------------------------- 19. 更改檔案名稱,將 *.htm --> *.html echo -n "This will change *.htm to *.html. Can I continue ? (y/n) : " set input = $< if ( $input != "y" && $input != "Y" ) then echo "Ok. Quit..." exit 2 endif # rename *.htm to *.html foreach file ( *.htm ) echo "Processing $file to `basename $file .htm`.html " mv $file `basename $file .htm`.html end -------------------------------------------------------------------- 20. 將大寫的檔名改成小寫的檔名 tr string1 string2 會將 standard input 的字串, 所對應到的 string1, 都以 string2 取代 foreach file ( * ) mv $file `echo $file | tr '[A-Z]' '[a-z]'` end -------------------------------------------------------------------- 21. 將小寫的檔名改成大寫的檔名 foreach file (*) mv $file `echo $file | tr '[a-z]' '[A-Z]'` end --------------------------------------------------------------------