2017-07-25 59 views
1

我試圖將現有的bash腳本移植到Solaris和FreeBSD上。它在Fedora和Ubuntu上運行良好。與腳本程序相關的bash腳本移植問題

此bash腳本使用以下命令集將輸出刷新到臨時文件。

file=$(mktemp) 
    # record test_program output into a temp file 
    script -qfc "test_program arg1" "$file" </dev/null & 

腳本程序在FreeBSD和Solaris上沒有-qfc選項。在Solaris和FreeBSD上,腳本程序只有-a選項。我做了以下工作直到現在:

1)更新到最新版本的bash。這沒有幫助。

2)試着找出「腳本」程序源代碼的確切位置。我也找不到它。

有人可以幫我嗎?

+1

https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/tree/term-utils/ script.c – melpomene

+3

爲什麼需要用'script'來捕獲輸出?通常,該程序用於交互式會話。如果您只想捕獲stdout和stderr,請改用'>「$ file」2>&1'。 – ceving

+0

我認爲程序的輸出沒有立即刷新。這就是爲什麼腳本程序正在被使用。 –

回答

2

script是一個獨立的程序,不是shell的一部分,正如您注意到的那樣,只有-a標誌可用於所有變體。 FreeBSD版本支持類似於-f-F <file>)的東西,並且不需要-c

這裏是一個醜陋的,但更便攜的解決方案:

buildsh() { 
    cat <<-! 
     #!/bin/sh 
     SHELL="$SHELL" exec \\ 
    ! 
    # Build quoted argument list 
    while [ $# != 0 ]; do echo "$1"; shift; done | 
    sed 's/'\''/'\'\\\\\'\''/g;s/^/'\''/;s/$/'\''/;!$s/$/ \\/' 
} 

# Build a shell script with the arguments and run it within `script` 
record() { 
    local F t="$(mktemp)" f="$1" 
    shift 
    case "$(uname -s)" in 
     Linux) F=-f ;; 
     FreeBSD) F=-F ;; 
    esac 
    buildsh "[email protected]" > "$t" && 
    chmod 500 "$t" && 
    SHELL="$t" script $F "$f" /dev/null 
    rm -f "$t" 
    sed -i '1d;$d' "$f" # Emulate -q 
} 

file=$(mktemp) 
# record test_program output into a temp file 
record "$file" test_program arg1 </dev/null & 
+0

謝謝伊斯梅爾。我會試試這個。 –