| 1 | |
| 2 | = shell 裡 for ... in 與 運算 的應用 = |
| 3 | |
| 4 | * example 1 : 批次殺程序 |
| 5 | |
| 6 | {{{ |
| 7 | #!/bin/bash |
| 8 | |
| 9 | #str=$(ps axw -eo pid,command | grep "org.apache.catalina.startup.Bootstrap" | grep "start" | awk '{print $1}') |
| 10 | |
| 11 | str=$(ps axw -eo pid,command |\ |
| 12 | grep "apache" | grep "start" |\ |
| 13 | awk '{print $1}') |
| 14 | echo "str = $str" |
| 15 | |
| 16 | count=1 |
| 17 | for i in $str |
| 18 | do |
| 19 | echo "string[$count]= $i" |
| 20 | echo "kill -9 $i" |
| 21 | count=$(($count+1)) |
| 22 | done |
| 23 | |
| 24 | }}} |
| 25 | |
| 26 | * example 2 : 批次改檔名 |
| 27 | |
| 28 | {{{ |
| 29 | cd ~ |
| 30 | for i in *.[tT][iI][fF]; do |
| 31 | #若目錄內無 .tif 檔案 $i 會傳回 "*.[tT][iI][fF]" |
| 32 | if [ "$i" != "*.[tT][iI][fF]" ]; then |
| 33 | #取得副檔名 |
| 34 | subname="`echo $i | awk -F '.' '{print $NF}'`" |
| 35 | #取得主檔名 |
| 36 | filename="`echo $i | sed -e s/\.${subname}$//`" |
| 37 | #將 .tif 轉成 .pdf |
| 38 | #tiff2pdf -o "${filename}.pdf" "$i" |
| 39 | mv ${filename}.tif ${filename}.pdf |
| 40 | #若執行成功則刪除 .tif 檔案 |
| 41 | if [ $? -eq 0 ]; then |
| 42 | rm -rf $i |
| 43 | fi |
| 44 | fi |
| 45 | done |
| 46 | }}} |