Re: How to determine if STDIN has piped data?

From: Adam (lists_acc_at_comcast.net)
Date: 04/03/04


Date: Sat, 3 Apr 2004 12:29:44 -0500
To: Bryan Harris <harrisb@dakotacom.net>

Bryan,

The scope of this conversation seems beyond my ability, but I think I
can answer you're question - I'm sure someone will correct me if I'm
wrong :)

You're looking to put together scripts that act like typical
command-line commands like `ls` or `cat`. I use the magic of the
diamond operator `<>`. This allows invocation arguments just like `ls`
or `cat`. Typically its used inside of a look like this..

#! /usr/bin/perl

     print "Script: $_" while(<>);
     exit(0);

Lets say the program's name was `sample.pl` and it was processing a
file with 3-lines of test text in a file named `sample.txt`.

sample.pl sample.txt

Script: Test-1
Script: Test-2
Script: Test-3

You could also use the following command `cat sample.txt | sample.pl`.

As we say in Philly, you feel me dog?

Regards,
Adam

** Back ticks are not part of the actual statement.

On Mar 30, 2004, at 11:35 PM, Bryan Harris wrote:

>
>
>> On Mon, 29 Mar 2004 00:38:50 -0700
>> Bryan Harris <harrisb@dakotacom.net> wrote:
>>
>>>> Alternatively, you can use the '-t' operator:
>>>>
>>>> exit 0 if -t STDIN
>>>
>>>
>>> I've been waiting for this for a LONG time, thanks Smoot.
>>
>> No problem. It took me a while to find the correct operator as well.
>>
>> Please keep in mind that doing this breaks the de facto Unix standard
>> for filters. A simply command which is a filter (e.g takes
>> input from STDIN and sends output to STDOUT) is written without any
>> consideration whether the input is a terminal, pipe, socket or
>> otherwise. This makes it very simple to use the command in a pipeline
>> or
>> standalone from the terminal.
>
> [stuff cut out]
>
> Smoot,
>
> Strangely, I find that we almost always want our scripts to act this
> way.
> If any files or data is passed to the script, then it uses it.
> Otherwise,
> it prints usage instructions (since we'd rather not have man pages for
> scripts, and I don't know how to build them anyway).
>
> I mention this because I'm interested in hearing your thoughts on this
> methodology.
>
> By the way, what's a socket?
>
> - B



Relevant Pages

  • Re: vi horizontal split screen
    ... efficient at processing scripts, and one way to do that was substitute ... Microsoft's work for OLE in Windows 1x through 3x. ... As more and more users come to Linux who have no desire for command line ... effectively a programming environment. ...
    (comp.editors)
  • Re: Cron to email notification of ftp file upload?
    ... or traceroute and getting "command not found" messages and then trying to ... >use sudo command when you need to make root privileged changes, ie, ... [ohmster@ohmster scripts]$ sudo crontab -l ... $ cat ftpwatch ...
    (alt.os.linux)
  • Re: Beginners questions (tcsh) 1. set histdup 2. ??
    ... > learn two shells, one for interactive, and one for scripting. ... What do you do in your scripts which is the ... SAME as what you do on the command line? ... far more comfortable than those of zsh, bash or ksh. ...
    (comp.unix.shell)
  • Re: Primary Differences: FreeBSD/Linux
    ... > shell for scripts, ... Some do, it's in ports. ... ports tree or package tools from a console command line. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: Confusing stack effects
    ... Or you would have the scripts generate several ... But what you really seem to be aiming for is an easy to use GUI ... I am not one to say "Command line pwns your GUI", but instead, ... After that, call "sort" to sort the list, and then remove double ...
    (alt.lang.asm)