Ticket #9528: create-pipestatus.sh

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

Bash script to create specific version of "pipestatus" in $SAGE_ROOT/spkg

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 [ -z "\$1" ]; 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
38(set -o pipefail; eval "\$1 | \$2")
39
40exit \$?
41EOF
42
43  else # bash version <3.0:
44
45    # Use redirection.  Adapted from the comp.unix.shell FAQ.  See
46    # http://www.unix.com/shell-programming-scripting/92163-command-does-not-return-exit-status-due-tee.html
47
48    echo "for bash version <3.0"
49
50    cat >> $SAGE_ROOT/spkg/pipestatus <<EOF
51exec 3>&1
52eval \`
53exec 4>&1 >&3 3>&-
54{
55    eval "\$1" 4>&-; echo "ES1=\$?;" >&4
56} | eval "\$2"
57echo "ES2=\$?;" >&4
58\`
59exit \$ES1
60EOF
61
62  fi
63
64  chmod +x $SAGE_ROOT/spkg/pipestatus
65
66fi