Suramya's Blog : Welcome to my crazy life…

February 13, 2010

Determine If Shell Input is Coming From the Terminal or From a Pipe

Filed under: Knowledgebase,Linux/Unix Related — Suramya @ 2:23 AM

Figuring out if the input to a script is coming from the terminal or from a pipe is not something that I have ever had to use but I found the possibility very interesting so sharing it over here:

#!/bin/bash

stdin="$(ls -l /dev/fd/0)"
stdin="${stdin/*-> /}"
ftype="$(stat --printf=%F $stdin)"

if   [[ "$ftype" == 'character special file' ]]; then 
	echo Terminal
elif [[ "$ftype" == 'regular file' ]]; then 
	echo Pipe: $stdin
else
	echo Unknown: $stdin
fi

– Suramya

Source: Linux Journal

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress