Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe that does nothing

Tags:

bash

sh

ksh

nop

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

like image 600
Fabio A. Mazzarino Avatar asked May 20 '15 13:05

Fabio A. Mazzarino


People also ask

What is a bash pipe?

A pipe in Bash takes the standard output of one process and passes it as standard input into another process. Bash scripts support positional arguments that can be passed in at the command line.

What does pipe mean in command line?

Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on. It can also be visualized as a temporary connection between two or more commands/ programs/ processes.

Is Empty bash?

To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"

What does pipe less do in Linux?

Redirect output to less command using a pipe Output from other commands can be piped to less command to make it easier to scroll along each line at a time.


2 Answers

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
like image 80
chaos Avatar answered Oct 13 '22 01:10

chaos


: 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
like image 27
chepner Avatar answered Oct 13 '22 01:10

chepner