Operating System - HP-UX
1847005 Members
3904 Online
110257 Solutions
New Discussion

Re: Shell script pipe (STDIN/STDOUT/STDERR)

 
SOLVED
Go to solution
Bjarne Dein
Advisor

Shell script pipe (STDIN/STDOUT/STDERR)

I've seen your website and like it - and thought that somebody here might have the answere to my question.

Let's say I've made a (ksh) script named: "ShowPipes.sh" and run the script in the 3 following ways:

$ echo "This is a test" | ./ShowPipes.sh -f /tmp/pipes.txt
$ ./ShowPipes.sh -f /tmp/pipes.txt
$ ./ShowPipes.sh

How do I test within the script if the first case is true - and catch the piped test "This is a test"?

The last 2 way I know, but the problem is, when fx. catching the left input by

LeftInput=`cat`

it hangs, if there is nothing, so I have to get a way how to "kill" that part, if nothing comes in - and "how do we do that"???

PS. And let me just say, I have made a "few" scripts, and as fare as I can see, there is no "simple" way - or...


Kind Regards

Bjarne Dein System Administrator - Unix

Scandinavian IT Group A/S Direct: +45 3232 4610 Mobile: +45 2322 4610 mailto:Bjarne.Dein@SAS.dk

Knowlegde is not to know something, but how to get to...
5 REPLIES 5
Justo Exposito
Esteemed Contributor

Re: Shell script pipe (STDIN/STDOUT/STDERR)

Hi Bjarne,

I think that you want to check the number of parameters received, isn't?
If yes you can do by checking the shell variable $# this tell you how many parameters received, there are other shell variables interesting like that:
$* all the parameters
$1 the first parameter
$2 the second parameter
shift command skip to the next parameter

Hope this help you,

Justo.
Help is a Beatiful word
Justo Exposito
Esteemed Contributor

Re: Shell script pipe (STDIN/STDOUT/STDERR)

Hi Again,

You can put your script here and I try to help you better.

Welcome to the forum.

Regards,

Justo.
Help is a Beatiful word
Bjarne Dein
Advisor

Re: Shell script pipe (STDIN/STDOUT/STDERR)

Ok - here is one of my trials!!!

The big point is NOT, what is comming after the command - like "killme.sh -f /tmp/file.txt", but what might be piped in to it or not!

So the real question is - is it somehow possible to check for pipe-in, like "cat /tmp/file.txt | killme.sh".

If there is some, you can just do:
STDIN1=`cat`
and that's done, but if nothing is comming in, it will HANG!!! ... till EOF ups!!!

Kind Regards

Bjarne
Knowlegde is not to know something, but how to get to...
John Palmer
Honored Contributor
Solution

Re: Shell script pipe (STDIN/STDOUT/STDERR)

Hi,

The shell is pretty limited in its ability to check the status of a file descriptor. However, you can differentiate between the examples you supplied by checking if standard input is from a terminal
if [[ -t 0 ]];
then
although this won't work for a cron job for instance.

Another possibility is to use the read command to try to read data from standard input.
while read RECORD
do

done

Regards,
John
Deepak Extross
Honored Contributor

Re: Shell script pipe (STDIN/STDOUT/STDERR)

This works for me:

#!/usr/bin/sh
if [ -t 0 ]
then
echo "No piped input"
else
read data
echo "Piped input is $data"
fi
echo "Command Line parameters are $*"