Re: How to determine if STDIN has piped data?
From: Adam (lists_acc_at_comcast.net)
Date: 04/03/04
- Next message: Randal L. Schwartz: "Re: test - please ignore"
- Previous message: Smoot Carl-Mitchell: "Re: Using $_ in a function if no argument is passed"
- Maybe in reply to: Bryan Harris: "Re: How to determine if STDIN has piped data?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Randal L. Schwartz: "Re: test - please ignore"
- Previous message: Smoot Carl-Mitchell: "Re: Using $_ in a function if no argument is passed"
- Maybe in reply to: Bryan Harris: "Re: How to determine if STDIN has piped data?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|