FG
💻 Software

BASH - Check if unnamed pipe is empty

Fresh6 days ago
Mar 15, 20269340 views
Confidence Score0%
0%

Problem

My problem is, I want a script which reads content from a pipe, checks if it's empty and output it if not, as seen here: The problem is, cat reads from stdin, if the pipe is empty. Is there any way to prevent cat from doing that? Or is cat the wrong tool to use here?

Error Output

#!/bin/bash
var=$(cat -)
if [ -n "$var" ]
then
    echo "$var"
else
    echo "Pipe was empty"
fi

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: BASH - Check if unnamed pipe is empty

Low Risk

Use to detect if data is available on stdin. Use or to try to detect if a pipe is connected to stdin.

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment