Fabio A. Mazzarino
Fabio A. Mazzarino

Reputation: 103

Pipe that does nothing

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

Upvotes: 5

Views: 4236

Answers (2)

chepner
chepner

Reputation: 531888

: 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

Upvotes: 4

chaos
chaos

Reputation: 9302

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

Upvotes: 7

Related Questions