Read from a pipe or die in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read from a pipe or die in perl
# 1  
Old 04-26-2010
Read from a pipe or die in perl

I have a perl program that I want to read from a file passed as an argument or from a pipe. If their is no pipe or arguments, I want it to output a help message. I am stuck on how to prevent perl from reading from the keyboard if it isn't fed any file names or data from a pipe. The only things I can find by searching are tutorials about how to read from files and use the diamond operator.


Here are the relevant lines of the program:

Code:
if ($ARGV[0] eq "--help" || $ARGV[0] eq "-h")
{
        &help;
}     

if ( defined($ARGV[0]))
{
         #fill array for sorting
}
else
{
                foreach(<>)
                my @splitter = split(/\s+/);
                foreach(@splitter)
                {
                        if (!/^[0-9]+$/)
                        {
                        die("I only like numbers!\n");
                        }
                        push (@file, $_);
                }

# STDIN and/or file contents get stored in the array @file
if ( ! defined(@file))
{
        &help;
}

# 2  
Old 04-27-2010
Hi,

Try this...
Code:
if (! defined $ARGV[0] || $ARGV[0] eq "--help" || $ARGV[0] eq "-h")
{
        &help;
}

# 3  
Old 04-27-2010
Thanks pravin27. Unfortunately that didn't solve my problem. Adding the "! defined()" stops it from reading from a pipe if no file name is passed to it. Is there anyway to detect if a shell has redirected STDIN?

Last edited by ilikecows; 04-27-2010 at 07:10 AM..
# 4  
Old 04-27-2010
Hi,

Please correct me if i am wrong .You mean to say if there is no command line argument then script should ask for the filename ?

how you are running this script ?
# 5  
Old 04-27-2010
If there is no command line argument and if STDIN is not redirected (file or pipe), the program should exit and output a usage message. The script is meant to behave like a normal UNIX command line utility. I appreciate you taking the time to help pravin27.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX Pipe -Exit when there are no bytes to read

Hi All, I'm creating a program which reads millions of bytes from the PIPE and do some processing. As the data is more, the idea is to read the pipe parallely. Sun Solaris 8 See the code below: #!/bin/sh MAXTHREAD=30 awk '{print $1}' metadata.csv > nvpipe & while do ... (3 Replies)
Discussion started by: mr_manii
3 Replies

2. Shell Programming and Scripting

UNIX fifo concurrent read from a named pipe

I have created a fifo named pipe in solaris, which writes the content of a file, line by line, into pipe as below: $ mkfifo namepipe $ cat books.txt "how to write unix code" "how to write oracle code" $ cat books.txt >> namepipe & I have a readpipe.sh script which reads the named... (2 Replies)
Discussion started by: naveen mani
2 Replies

3. Shell Programming and Scripting

While read pipe input issue

Hello, I have an ffmpeg bash script which is working nice and I need to do the same for other sources. To create new scripts and to deal with multiple bash files sounds not logical. It is a bit hard to manage for me.. I wondered if it was possible to make my input file as variable. Then I... (1 Reply)
Discussion started by: baris35
1 Replies

4. Shell Programming and Scripting

Read pipe data

Hello, I need to read the pipe data as:- cat abc.txt | uuencode abc.txt | mailx -s hi xyz@xyz.com I will override the mailx function so that when mailx is called, it calls my version of maix and in that function I want to read the file which is attached in progional mailx function- abc.txt... (7 Replies)
Discussion started by: shubh05
7 Replies

5. Shell Programming and Scripting

[SOLVED] nawk FS using pipe read variables from file

I have a file data_1.out which contains: 1|abc mail|mail subject|mail body 2|def mail|mail subject|def mail body I am trying to read the variables from data_1.out and use them to print to 2 different files based on the id (first_column) The problem is I am not able to read the file... (8 Replies)
Discussion started by: sol_nov
8 Replies

6. Shell Programming and Scripting

perl problem - another 'die' issue.

two things. why doesn't the 'die' message get displayed - "Error: release log directory creation failed..."? why does the script name and line number get displayed despite the inclusion of a '\n'. apparently adding a newline prevents this from happening. if (! -d "$logdir") { use... (4 Replies)
Discussion started by: mjays
4 Replies

7. Shell Programming and Scripting

perl problem - why isn't 'die' being called?

last week i started learning perl, so have limited skill and knowledge. why isn't 'die' being called and the script exiting before the 'directory created' line? if (! -d "$logdir") { system "mkdir -p $logdir" || die print "\nERROR: release log directory creation failed - $logdir: $!\n";... (4 Replies)
Discussion started by: mjays
4 Replies

8. Programming

pipe read and write with many forked children

I know how to read and write if i have a forked process with only one child. However what is involved with reading and writing with many forked processes. Say one parent that forks 5 children, and needs to communicate with all 5 in half duplex. int temp, counter=0; do{ pipe(temp); ... (5 Replies)
Discussion started by: steveneliuk
5 Replies

9. Programming

Problem in read() from a pipe

Hi, Can any one please help me with this. Am struggling hard to get a solution. I am doing telnet through a C program and getting the stdout file descriptor of the remote machine to pipe. read() function is getting data, But whenl it receives SOH character ie. ^A ( Start of heading = Console... (2 Replies)
Discussion started by: JDS
2 Replies

10. Shell Programming and Scripting

read after pipe problem OSX10.4

I use read often in scripts to filter the right part into a variable like: $ print "abc cde efg" | read k l ; print "k=$k, l=$l" k=, l= This works on linux and unix versions I work with. On OSX 10.4 this doesn't work. I found a workaround but would like to know why the original line... (5 Replies)
Discussion started by: relyveld
5 Replies
Login or Register to Ask a Question