wiki:jazz/10-07-22

2010-07-22

  • [備忘] 重新改用 root 身份執行的 shell script
    [ "`id -u`" != "0" ] && exec sudo su -c "$0" "$@"
    
  • [備忘] 讓標準輸出(STDOUT)與標準錯誤(STDERR)導入到 log 檔的 bash script 寫法
    #! /bin/bash
    
    TARGET="target-file"
    
    # file descriptor 4 prints to STDOUT and to TARGET
    exec 4> >(while read a; do echo $a; echo $a >>$TARGET; done)
    
    # now STDOUT is redirected
    exec >&4
    # now STDERR is redirected
    exec 2>&4
    # generate output to STDOUT, it will also write to $TARGET
    date
    echo "Date sent to fd4."
    # generate output to STDERR, it will also write to $TARGET
    abc
    
Last modified 14 years ago Last modified on Jul 23, 2010, 11:26:56 AM