5

I'm on a AIX box and need a program that when used after a pipe does nothing.

I'll be more exact. I need something like this:

if [ $NOSORT ] ; then
    SORTEXEC="/usr/bin/doesnothing"
else
    SORTEXEC="/usr/bin/sort -u"
fi
# BIG WHILE HERE
do

done | SORTEXEC

I tried to use tee > /dev/null, but I don't know if there is another better option available.

Can anybody help with a more appropriate program then tee?

Thanks in advance

  • You need `done | "$SORTEXEC"` (where the double quotes may or may not be advisable -- you cannot then have a command with arguments in the variable, but in this particular scenario you hardly want that anyway; and if you really do, putting the complex command in a function is probably a good idea for many reasons. See also http://mywiki.wooledge.org/BashFAQ/050). – tripleee May 20 '15 at 14:02

2 Answers2

7

Use tee as follows:

somecommand | tee

This just copies stdin to stdout.

Or uUse true or false. All they do is exit EXIT_SUCCESS or EXIT_FAILURE.

somecommand | true

Notice, every output to stdout from somecommand is dropped.

Another option is to use cat:

somecommand | cat
chaos
  • 8,162
  • 3
  • 33
  • 40
4

: is the portable, do-nothing command in the POSIX specification.

if [ "$NOSORT" ] ; then
    SORTEXEC=:
else
    SORTEXEC="/usr/bin/sort -u"
fi 

: is guaranteed to be a shell built-in in a POSIX-compliant shell; other commands that behave similarly may be external programs that require a new process be started to ignore the output.

However, as tripleee pointed out, strings are meant to hold data, not code. Define a shell function instead:

if [ "$NOSORT" ]; then
    SORTEXEC () { : ; }
else
    SORTEXEC () { /usr/bin/sort -u; }
fi

while ...; do
    ...
done | SORTEXEC
chepner
  • 497,756
  • 71
  • 530
  • 681