| 37 | |
| 38 | == Shell Script == |
| 39 | |
| 40 | * 如何在 shell script 裡面把標準輸入跟標準輸出重新導向到另一個路徑呢?! |
| 41 | * [參考] [http://www.linuxquestions.org/questions/linux-general-1/shell-scripting-how-to-redirect-output-from-within-the-script-itself-446558/ Shell scripting: How to redirect output from within the script itself? ] |
| 42 | {{{ |
| 43 | #!sh |
| 44 | #! /bin/bash |
| 45 | |
| 46 | TARGET="target-file" |
| 47 | |
| 48 | # file descriptor 4 prints to STDOUT and to TARGET |
| 49 | exec 4> >(while read a; do echo $a; echo $a >>$TARGET; done) |
| 50 | # file descriptor 5 remembers STDOUT |
| 51 | exec 5>&1 |
| 52 | |
| 53 | # now STDOUT is redirected |
| 54 | exec >&4 |
| 55 | date |
| 56 | |
| 57 | # wer're done |
| 58 | exec >&5 |
| 59 | echo "Date sent to fd4." |
| 60 | }}} |