Ticket #9528: create-pipestatus-v2.sh

File create-pipestatus-v2.sh, 1.4 KB (added by leif, 11 years ago)

Creates a slightly improved version of pipestatus, no changes to the script code itself

Line 
1#!/usr/bin/env bash
2
3# Check if $SAGE_ROOT/spkg/pipestatus already exists, otherwise
4# create it to allow upgrade from Sage <4.5:
5
6if [ ! -f $SAGE_ROOT/spkg/pipestatus ]; then
7
8  echo -n "Creating pipestatus "
9
10  cat > $SAGE_ROOT/spkg/pipestatus <<EOF
11#!/usr/bin/env bash
12
13if [ \$# -ne 2 -o -z "\$1" -o -z "\$2" ]; then
14    echo "Run two commands in a pipeline 'CMD1 | CMD2' and exit"
15    echo "with the exit status of CMD1, *not* that of CMD2."
16    echo "\$0 cmd1 cmd2"
17    exit
18fi
19
20# This is useful, for example, in a makefile, where we tee the output
21# of a build command to a log file.  Because \$? is the exit status of
22# the last command in a pipeline, make won't stop if tee succeeds but
23# a preceding command in the pipeline fails.
24
25EOF
26
27  # Write the simple version if Bash is >=3.0:
28
29  VER=${BASH_VERSINFO[0]}
30
31  if [ $VER -gt 2 ]; then
32
33    # Use bash 3.0's pipefail option.
34
35    echo "for bash version >=3.0"
36
37    cat >> $SAGE_ROOT/spkg/pipestatus <<EOF
38set -o pipefail
39eval "\$1 | \$2"
40
41exit \$?
42EOF
43
44  else # bash version <3.0:
45
46    # Use redirection.  Adapted from the comp.unix.shell FAQ.  See
47    # http://www.unix.com/shell-programming-scripting/92163-command-does-not-return-exit-status-due-tee.html
48
49    echo "for bash version <3.0"
50
51    cat >> $SAGE_ROOT/spkg/pipestatus <<EOF
52exec 3>&1
53eval \`
54exec 4>&1 >&3 3>&-
55{
56    eval "\$1" 4>&-; echo "ES1=\$?;" >&4
57} | eval "\$2"
58echo "ES2=\$?;" >&4
59\`
60exit \$ES1
61EOF
62
63  fi
64
65  chmod +x $SAGE_ROOT/spkg/pipestatus
66
67fi