0

I would like to capture command output to variable in bash, but display it as well to console.

exec 5>&1
STATUS=$(zypper info rar|tee >(cat - >&5))
echo $STATUS

It works in console expected way. When calling within following simple script, it works as well expected way.

#!/bin/bash
exec 5>&1
STATUS=$(zypper info rar|tee >(cat - >&5))
echo $STATUS

But when calling within following script, it produces error.

#!/bin/sh
#
# description: currency_trader_tools installation script
# Currency_Trader software.
#
# prerequisities:
#   OpenSuse Leap 42.1 x86_64
#            clean installation of Minimal Server Selection (Text mode)
# install:
#       Midnight Commander - linux file manager
#       x11vnc  -   X11 vnc server
#       xvfb-run    -   X11 virtual frame buffer server
#       java - latest JDK environment rpm
#
# commit_id =               "0f46a17011ca82c57ddb7f81636984c7bebd5798";
# build_revision_full =     "Build 0144 created 2016-05-11 18:04:00 based on commit 0f46a17011ca82c57ddb7f81636984c7bebd5798";
# build_revision_short =    "0f46a17";
# build_revision =          "0144";

RETVAL=0

ZIP_FILE_VERSIONED="Currency_Trader_Bash_Scripts_0_9_1- r-0144-0f46a17.zip"
ZIP_FILE="Currency_Trader_Bash_Scripts_0_9_1.zip"


# See how we were called.
if [[ ! `whoami` = "root" ]]; then
    echo "You must have administrative privileges to run this script"
    echo "Try 'sudo ./currency_trader_tools_install'"
    exit 1
fi
exec 5>&1
STATUS=$(zypper info rar|tee >(cat - >&5))
echo
echo $STATUS
case "$1" in
    all)
        install_all
        ;;
    *)
        echo $"Usage: currency_trader_tools_install {all}"
        exit 1
esac

exit $RETVAL

Error is:

./Currency_Trader_Bash_Scripts_0_9_1-Install-Script: command substitution: line 34: syntax error near unexpected token `('
./Currency_Trader_Bash_Scripts_0_9_1-Install-Script: command substitution: line 34: `zypper info rar|tee >(cat - >&5))'

Any recommendation, how to make the same using sh and not bash?

2
  • Are you sure you wouldn't rather just change your shebang to #!/bin/bash? The system you're running on already has bash installed as sh (based on your error message), and this would let you keep using [[..]] and $".." which currently already requires bash. Commented May 11, 2016 at 16:52
  • Yes, I am sure. I know, on OpenSuse I can switch to bash. But I really want to know answer. Thanks Commented May 11, 2016 at 17:14

1 Answer 1

1

>(...) is not part of the POSIX standard, so you would need to use an explicit named pipe. However, managing this properly could get tricky. Just capture the output, and output to the console explicitly.

STATUS=$(zypper info rar)
echo "$STATUS"

(The script is already outputting the captured output to the terminal; there doesn't seem to be any need for tee in the first place.)

Sign up to request clarification or add additional context in comments.

8 Comments

But output to console is much better structured, than writing using echo. So I am looking how to make this working using sh, without not to good option of echoing STATUS.
If you quote $STATUS (as John Kugelman fixed for me), the output will be the same.
It is not the same, try it. echo $STATUS print out only few really long line. There are no line breaks, so it looks different way and not too usefull. And as well when using with some time consuming task, redirection print during task and not only at the end.
echo "$STATUS", not echo $STATUS.
You probably don't want to be checking the output of zypper anyway, just its exit status: zypper info rar || { echo "Usage ..."; exit 1; }.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.