Search code examples
rpmscriptlet

Why do i get syntax error for "[...] 2> >(tee -a $logfile >&2) [...]" in a script in rpm, but not when run from commandline and how to get it work?


Why do i get a syntax error for "[...] 2> >(tee -a $logfile >&2) [...]" in a script when run as rpm preinstall scriptlet, but not when script runs from commandline?

The purpose of that construct is to allways forward stderr just to the log file, but to have stderr to both, the logfile and the console.

here the complete example script:

#!/bin/bash
LOGFILE=/tmp/example.log;

if mkdir -v /tmp/example 1>>$LOGFILE 2> >(tee -a $LOGFILE >&2);
  then
    echo "ok";
  else
    echo "ko";
    exit 1;
fi

When run as pre scriptlet of a rpm (build with the maven rpm plugin), i get the error message:

syntax error near unexpected token `>'
`         mkdir -v /tmp/example 1>>$LOGFILE 2> >(tee -a $LOGFILE >&2);'

If i invoke the same script from commandline, i get "ok", as expected.

What do i have to do to make it work inside the rpm?

Thank you for helpfull answers.


Solution

  • This is not rpm related. If you put it in file script and you try to run this from command line, you will get the same error. This is because of script execute bash as posix compatible. See https://stackoverflow.com/a/12122609/3489429

    unset POSIXLY_CORRECT
    

    will work, but you rather should avoid process expansion and use e.g. pipes.