wiki:jazz/10-07-22

Version 2 (modified by jazz, 14 years ago) (diff)

--

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
    exec 2>&4
    date
    echo "Date sent to fd4."
    abc