2017-07-25 111 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

谢谢伊斯梅尔。我会试试这个。 –