Need help capturing pipe to a file in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help capturing pipe to a file in shell script
# 1  
Old 11-02-2005
Need help capturing pipe to a file in shell script

The command:
echo "this is some text" | shellscript abc def ghi

My problem:
How to capture "this is some text" so that I can process it,
I.e. capture to a file.

What I'm attemting to do is process the text echo'd into a file after writing the
parameters passed first. No problem capturing abc, def, ghi into environment
variables. I just can't get the text that's being piped to shellscript. I'm trying to
simulate mail/mailx in shellscript.

Thanks in advance for your help.

Heinz
# 2  
Old 11-02-2005
It sounds like you're trying to use a shell script to read from stdin (this is what you must do to capture "this is some text" from the echo command.)

Try inserting this line into your shell script. This captures the text from echo "this is some text" and stores it into $TEXT

------------------------------------
#!/bin/sh
TEXT=""

for foo in `cat /dev/stdin`
do
TEXT=$foo
done
echo "Captured Text: $TEXT"

-------------------------------------------

A helpful article can be found here:

http://lists.debian.org/debian-user/.../msg01220.html
# 3  
Old 11-02-2005
Thank you flyingpenguin. Close, but no cigars yet.

No /dev/stdin in interix (MS Win2003 Services for Unix).

Searching for equivilant. At least now I have something to search for.

heinz
# 4  
Old 11-02-2005
The first command in the script must do something with the piped input. So maybe use something like this....
Code:
#!/usr/bin/ksh

cat > /tmp/captured.txt
:

# 5  
Old 11-02-2005
That did it Ygor.

I could've sworn I tried that early in the game, but guess not.

Thank you very much.
# 6  
Old 11-03-2005
Use tee command

Something like this would work

Code:
echo "this is some text" | tee file_name | shellscript abc def ghi

Jerardfjay
# 7  
Old 11-03-2005
Yes, that would work, but not in may case as I can't change the command structure itself. I had to replace mailx as for some reason it broke and I found it easier to replace it than to dig into why it broke. That will be done some other time when we can risk possible downtime.

The cat > /tmp/file.txt did the trick. I'm still shaking my head as to what I did (or didn't) do right initially as I knew that should work. Maybe I didn't have it in as first command. Anyways, don't care as I have it working now :-)

In case you're wondering, I found eemailer that runs as a service on Win2003 Server so I replaced mailx with my own, I.e. the script, which just builds the mail message and deposits it into the directory eemailer watches. Unix (interix) on Win Server is turning out to be a good platform for us (knock on wood).

Thanks again everyone, especially Ygor who had the winner :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for capturing FTP logs

I have a script #!/bin/bash HOST=ftp.example.com USER=ftpuser PASSWORD=P@ssw0rd ftp -inv $HOST <<EOF user $USER $PASSWORD cd /path/to/file mput *.html bye EOF the script executes sucessfully I need to capture the FTP logs to a logfile should contain FTP Login successful ... (1 Reply)
Discussion started by: rajeshas83
1 Replies

2. Shell Programming and Scripting

Capturing name of the user who is running a shell script

Hello All, I am writing a shell script which will run on build server where multiple users can login.These users can run this script at the same time. I want to capture the name of the user who ran the script for every instance.How can I do that ? Regards,... (1 Reply)
Discussion started by: anand.shah
1 Replies

3. Shell Programming and Scripting

Capturing output of procedure in variable in shell script

Hi guys I am calling one DB2 stored proc through unix. It is giving me below output. I want to capture the value 150 in one UNIX variable in shell script. Please let me know how I can achieve this. Thanks in advance Value of output parameters -------------------------- Parameter Name :... (5 Replies)
Discussion started by: vnimavat
5 Replies

4. Shell Programming and Scripting

Capturing log of a shell script

Hi, I have a script which does multiple tasks in sequence. When i execute the script, it runs and displays lot of messages for each of the steps on the console. Is there any way I can capture those messages and store it for audit purposes ? Best Regards, bornon2303 (2 Replies)
Discussion started by: bornon2303
2 Replies

5. UNIX for Dummies Questions & Answers

Capturing Input Parameters on Shell Script

i have this basic line of code that doesn't work. i simply want to get the input parameter strings but when the script is run it appears that the first parameter is assigning the value to the second parameter. #!/bin/sh pdir=`pwd` p1=$1 p2=$2 echo "directory: $pdir\n" echo "parameter... (2 Replies)
Discussion started by: wtolentino
2 Replies

6. Shell Programming and Scripting

Capturing Sybase SP output in Shell Script

Greetings, I need to capture the output of a Sybase stored procedure, inside my shell script( k shell). Based on this output, I need to call another perl script, with input arguments as the result set of the procedure execution. I need to keep looping through and call the perl script, ... (2 Replies)
Discussion started by: rajpreetsidhu
2 Replies

7. UNIX for Dummies Questions & Answers

Need help with shell script for chekking a column in txt file - pipe delimited

Hi: I have a text file date(pipe delimited) which is loaded in to the DB using sql loader(&CTL files) after some initial validation by the shell script. Now i have a situation where the shell script needs to check a column in the text file and if it is NULL then it needs send this record/row... (12 Replies)
Discussion started by: ravi0435
12 Replies

8. Shell Programming and Scripting

Pipe data to shell script

Sorry about the noobish question but... How do I capture data thats piped to my script? For instance, ls -al | myscript.sh How do I access the output from ls -al in myscript.sh? (3 Replies)
Discussion started by: tomjones07
3 Replies

9. Shell Programming and Scripting

Capturing shell script command output

I am trying to check to see if a file exists on a ftp server, well, I know that cant be done, atleast directly, So I came up with this small script ftp -n $HOST <<END_SCRIPT quote USER $USER quote PASS $PASSWD cd public_html/crap dir $FILE quit END_SCRIPT Where the $ variable... (2 Replies)
Discussion started by: designflaw
2 Replies
Login or Register to Ask a Question