| 1 | |
| 2 | |
| 3 | * 用shell 批次改檔名 |
| 4 | * 此用法應用到 for in , 正規表示法, awk , sed 因此還學滿多的 |
| 5 | {{{ |
| 6 | #!/bin/sh |
| 7 | |
| 8 | |
| 9 | |
| 10 | cd ~/test2 |
| 11 | |
| 12 | echo `pwd` |
| 13 | |
| 14 | for i in *.[tT][iI][fF]; do |
| 15 | |
| 16 | #若目錄內無 .tif 檔案 $i 會傳回 "*.[tT][iI][fF]" |
| 17 | |
| 18 | if [ "$i" != "*.[tT][iI][fF]" ]; then |
| 19 | |
| 20 | #取得副檔名 |
| 21 | |
| 22 | subname="`echo $i | awk -F '.' '{print $NF}'`" |
| 23 | |
| 24 | #取得主檔名 |
| 25 | |
| 26 | filename="`echo $i | sed -e s/\.${subname}//`" |
| 27 | |
| 28 | #將 .tif 轉成 .pdf |
| 29 | |
| 30 | #tiff2pdf -o "${filename}.pdf" "$i" |
| 31 | |
| 32 | echo "tif -> pdf" |
| 33 | |
| 34 | mv ${filename}.${subname} ${filename}.pdf |
| 35 | |
| 36 | #若執行成功則刪除 .tif 檔案 |
| 37 | |
| 38 | if [ $? -eq 0 ]; then |
| 39 | |
| 40 | rm -rf $i |
| 41 | |
| 42 | fi |
| 43 | |
| 44 | fi |
| 45 | |
| 46 | done |
| 47 | }}} |
| 48 | |
| 49 | * awk |
| 50 | {{{ |
| 51 | Examples: |
| 52 | |
| 53 | gawk '{ sum += $1 }; END { print sum }' file |
| 54 | |
| 55 | gawk -F: '{ print $1 }' /etc/passwd |
| 56 | }}} |
| 57 | |
| 58 | * sed |
| 59 | {{{ |
| 60 | Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]... |
| 61 | |
| 62 | |
| 63 | |
| 64 | -n, --quiet, --silent |
| 65 | |
| 66 | suppress automatic printing of pattern space |
| 67 | |
| 68 | -e script, --expression=script |
| 69 | |
| 70 | add the script to the commands to be executed |
| 71 | }}} |