Read data from given filename or stream


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Read data from given filename or stream
# 1  
Old 01-06-2012
Read data from given filename or stream

I have a script that takes 2 parameters (say) as mandatory
script1.sh a b

The 3rd parameter can be filename which it should process or it can come through a pipeline stream
The script should work both ways:

script1.sh a b filec
or cat filec | script1.sh a b

How to put logic in the script?
If 3rd parameter is not passed then how can the script know it was fed data from filec thru pipe?
and what shell variable holds this information?

Thanks,
-srinivas yelamanchili
# 2  
Old 01-06-2012
You'd really need to base it upon some evaluation that you can control. You'd test the inputs, and based on behavior, you'd fork off in whatever direction you need. The following function provides a similar test for logging purposes (with some additional chatter for documentation purposes):

Code:
function LOG
{
 #  Takes positional args or pipes...and print verbatim
  #     -  eg:   LOG   $(date )
  #              LOG   "something happening here... "
  #              echo  "something happened - $(date )" |LOG

  #  Passive assignment of Debugging var...
  ${_dbg_mode:-}

  #  Arg count >0 means args given...
  if    [[ $# -gt 0 ]]
  then
     #  If $MY_LOG is defined...
     if    [[ -n "${MY_LOG}" ]] \
        && [[ -f "${MY_LOG}" ]]
     then
        #  tee to $MY_LOG...
        print "${@}" >&1 2>/dev/null |tee -a "${MY_LOG}"
     else
        #  just echo to screen...
        print "${@}" >&1 2>/dev/null
     fi
  #  No args means we're probably piping...
  else
     #  If $MY_LOG is defined...
     if    [[ -n "${MY_LOG}" ]] \
        && [[ -f "${MY_LOG}" ]]
     then
        #  tee to $MY_LOG...
        read     P
        print "${P}" >&1 2>/dev/null |tee -a "${MY_LOG}"
     else
        #  just echo to screen...
        read     P
        print "${P}" >&1 2>/dev/null
     fi
  fi
}

This User Gave Thanks to curleb For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

Extract & Manipulate continous data stream-- tcpdump

Hello; I have this rather tricky problem to solve --(to me, anyways) .. I am processing the following one liner with tcpdump.. tcpdump -i T3501 -A ether host 00:1e:49:29:fc:c9 or ether host 00:1b:2b:86:ec:1b or ether host 00:21:1c:98:a4:08 and net 149.83.6.0/24 | grep --line-buffered -B... (5 Replies)
Discussion started by: delphys
5 Replies

3. Shell Programming and Scripting

Data stream between min and max

Hi, I have a text file containing numbers. There are up to 6 numbers per row and I need to read them, check if they are 0 and if they are not zero check if they are within a given interval (min,max). If they exceed the max or min they should be set to max or min respectively, if they are in the... (4 Replies)
Discussion started by: f_o_555
4 Replies

4. Shell Programming and Scripting

[Video stream] network stream recording with mplayer

Hi I used this command: mplayer http://host/axis-cgi/mjpg/video.cgi -user root -passwd root \ -cache 1024 -fps 25.0 -nosound -vc ffh264 \ -demuxer 3 -dumpstream -dumpfile output.avi It's ok but... Video Playing is very fast! Why? Is it a synch problem? What parameter I have to use for... (1 Reply)
Discussion started by: takeo.kikuta
1 Replies

5. Programming

read() blocks process until the stream is closed

Hi @all, i really stuck in programming a tool with bidirectional process communication (popen(cmd, "rw") ... something like that ;-)). Here is the code: if(pipe(p_stdin) != 0 || pipe(p_stdout) != 0) { fprintf(stderr, "Aufruf von pipe() schlug fehl.\n"); exit(1); } ... (6 Replies)
Discussion started by: jens.g
6 Replies

6. Shell Programming and Scripting

read mp3 filename and create one XML for each file

Hi: I have a collection of mp3s and I need to create 1 xml file per mp3. I have: recording1.mp3 recording2.mp3 etc and I want to generate this kind of files. recording1.xml recording2.xml and inside each xml file I need to add a url prefix and then the filename at the end. ... (4 Replies)
Discussion started by: jason7
4 Replies

7. Shell Programming and Scripting

Breaking down a Serial data stream

I'm running Debian ib Bash shell. currently I'm streaming the data from ttyS1 . The data stream is 13 fields comma delimited cat /dev/ttyS1 02/04/2009,10:57:18,1401.0,7.5,424.9,0.0,0.0,159.8,1401.0,7.5,265.2,34.4,2.5 The data is grouped for specific systems. 02/04/2009,10:57:18 ... (1 Reply)
Discussion started by: Mikey
1 Replies

8. UNIX for Advanced & Expert Users

how to read the data from an excel sheet and use those data as variable in the unix c

I have 3 columns in an excel sheet. c1 c2 c3 EIP_ACCOUNT SMALL_TS_01 select A.* from acc; All the above 3 col shoud be passed a variable in the unix code. 1.How to read an excel file 2.How to pass these data as variable to the unic script (1 Reply)
Discussion started by: Anne Grace
1 Replies

9. Shell Programming and Scripting

read a part of filename from the list in the script

how can i read a part of filename from the list in the script? all file in directory...will start with "CDBACKUPFILE" then the name is stored in list.txt such as JOHN,MARRY,PETER. After this, is seq number. CDBACKUPFILEJOHN00001 CDBACKUPFILEMARRY00004 CDBACKUPFILEPETER00003 I will use:... (3 Replies)
Discussion started by: happyv
3 Replies

10. UNIX for Advanced & Expert Users

Stream Read And Write Queues

Is there any possibility that a Stream Read and Write queues will interchange messages of any kind. If so what are the different possiblites and under what circumstances ? Thanks in advance. (4 Replies)
Discussion started by: S.P.Prasad
4 Replies
Login or Register to Ask a Question