reading from stdin in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading from stdin in a shell script
# 1  
Old 09-11-2008
reading from stdin in a shell script

Hello,

I've managed to get my .procmailrc file to work. At least it triggers a script which creates a file. But the file is empty. How do I get at the data that's been piped? I've done much creative googling to no avail. I belive it should be in stdin, but I can't figure out how to access it.

Thanks
Mike
# 2  
Old 09-12-2008
When procmail passes it via pipe to your script, your script might look like for example:
Code:
while read LINE; do
   echo ${LINE}    # do something with it here
done

exit 0

Not sure how your script looks like as you didn't show it to us.
# 3  
Old 09-17-2008
Thanks Zaxxon,

I had tried variations of that. I don't really have anything to post because I was stuck at the beginning. Turns out the problem is with my understanding of procmail since your code works fine if I pipe to my script from another script. Here's the procmailrc if anyone has any knowlege of this stuff:

Code:
:0 b
|$HOME/test.sh

My understanding is that this should pipe the body of the email to the script test.sh. As it stands right now, the script is triggered but there doesn't appear to be anything piped. I've used your suggestion in a script named "test.sh":
Code:
while read LINE; do
   echo ${LINE}  > t.log 
done

exit 0

if I write a small script that says:
Code:
echo "test"

save it as t.sh and run it at the command line like this:
Code:
./t.sh|./test.sh

then the file t.log is created and contains the line:
Code:
test

If I send an email to this mailbox the script test.sh is triggered but the file t.log contains only a blank line. That's where I'm at and I realize this isn't a procmail forum, but any help will be appreciated.

Last edited by rbatte1; 07-05-2017 at 09:44 AM.. Reason: Code tags
# 4  
Old 07-03-2009
keep it simple people..


Code:
#!/bin/sh
# Converts all lowercase text from
# stdin to uppercase
#
tr '[:lower:]' '[:upper:]' < /dev/stdin

# edward
# ebaddouh@gmail.com

you should call your script as follow:

Code:
$ echo "keep it simple" | myscript.sh
KEEP IT SIMPLE

cheers
# 5  
Old 07-04-2009
Try next cmdline, script caller set the output, script only read from stdin and write to stdout.
Code:
| $HOME/myscript.sh > $HOME/my.log

And myscript.sh, including also how to ex. to set uppercase without external command (ex. tr). Also no need to tell input, if it is stdin.
Code:
#!/usr/bin/ksh
typeset -u input
while read  input
do
     print "$input"
     # if you like to add line to some file, then
     # print some >> somefile
     # single > is overwrite = file include only the lastline
done

But if you like to put all while output to file then >> must be after done = end of while cmd.
Code:
while read line
do
     print some
done >> outfile
# or ex. done | outpipecmd > outfile
# all output, which are done between do-done goto outfile

Script are more generic, if script read from stdin and write to sdtout. Caller make define for input and output. Whole idea of *nix systems/commands - io-redirection and pipes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script reading file slow

I have shell program as below #!/bin/sh echo ======= LogManageri start ========== #This directory is getting the raw data from remote server Raw_data=/opt/ftplogs # This directory is ready for process the data Processing_dir=/opt/processing_dir # This directory is prcoessed files and... (4 Replies)
Discussion started by: Chenchireddy
4 Replies

2. Shell Programming and Scripting

Reading arguments for a shell script from file

I have a shell script that takes 2 arguments. I will have to execute this script multiple times with different values for the arguments. for example, ./shscript env1 value1 ./shscript env1 value2 ./shscript env2 value3 ./shscript env3 value4 ./shscript env1 value5 ./shscript env3... (24 Replies)
Discussion started by: goddevil
24 Replies

3. Shell Programming and Scripting

script hangs when reading from stdin

script: while read inputline; do if ; then if ; then break fi fi done Looks like the script hangs when stdin is empty or contains space. Any ideas on how to circumvent this? is it possible to use getline to process stdin content? (4 Replies)
Discussion started by: ux4me
4 Replies

4. Shell Programming and Scripting

Shell script to pass multiple stdin to prorgam?

Running on AIX 5.3L. I have a program "foo" written in Fortran that requires 3 levels of inputs from stdin (command prompt). > foo Enter Input 1: a Enter Input 2: b Enter Input 3: c running foo success! > How do I get a shell script to run this automatically? > echo "a" | foo... (2 Replies)
Discussion started by: discoganya
2 Replies

5. Shell Programming and Scripting

Shell script - paralllel reading problem

I have a shell script which has awk utlilty,also,in it. This a single script that many other jobs (informatica) using. Each job ll generate a log file. This script will read the particular log file and need to give output. The log file is act as input file for the script. So , a single script used... (2 Replies)
Discussion started by: Gopal_Engg
2 Replies

6. Shell Programming and Scripting

Reading from Keyboard - Shell Script

How do i read from kb using shell script but i need to read it from same line. Script :- echo "Please Enter Your Choice " read CHOICE But it goes to next line i need it to read it next to Choice and not new line. (4 Replies)
Discussion started by: dinjo_jo
4 Replies

7. Shell Programming and Scripting

Cannot redirect to STDIN in a shell script

I am unable to use STDIn redirection with < (commands) When I do the following, both approaches work and give the same results: 1. $ printf "aaa\nbbb\n" > file1 $ printf "111\n222\n" > file2 $ cat file1 file2 aaa bbb 111 2222. $ cat <(printf "aaa\nbbb\n") <(printf "111\n222\n") aaa... (8 Replies)
Discussion started by: metaltree
8 Replies

8. Shell Programming and Scripting

file reading through shell script

For reading a file through shell script I am using yhe code : while read line do echo $line done<data.txt It reads all the line of that file data.txt. Content of data.txt looks like: code=y sql=y total no of sql files=4 a.sql b.sql c.sql d.sql cpp=n c=y total no of c files=1 (4 Replies)
Discussion started by: Dip
4 Replies

9. Programming

Help:error in reading from stdin

void redirect(int argc, char *argv) { int flag; if (strcmp(argv, ">") == 0) flag = 1; else if (strcmp(argv, "<") == 0) flag = 2; else if (strcmp(argv, ">>") == 0) flag = 3; else printf("Something Wrong,Please Check!\n"); switch (flag) {... (5 Replies)
Discussion started by: zhshqzyc
5 Replies

10. Shell Programming and Scripting

Reading a table in a shell script

Dear all: I want to write a script capable of reading specific rows and collumns of a table, into a variable. Just imagine i have a file named table.dat which contains: GENERAL INFORMATION Col 1 Col2 Col3 1 1 2 2 3 3 4 4 What i want to do... (13 Replies)
Discussion started by: luiscarvalheiro
13 Replies
Login or Register to Ask a Question