while read line | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

while read line

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-13-2010
timber333 timber333 is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 13 June 2010, 10:27 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
while read line

Hi,

I have this type of bash code:


Code:
#!/bin/bash
while read -r line; do
  echo $line | /usr/local/example/program.sh
done

My question is, why if I put an echo statement inside the while loop it does not execute such as this:


Code:
while read -r line; do
 echo $line > file #this doesn't work
  /bin/echo $line | /usr/local/example/program.sh #this works
done

Yet, program.sh does process the $line.

For the record I am executing the script like this in shell terminal:

Code:
echo "statement" | /usr/local/programs/input.sh

What black magic is this?

Last edited by Scott; 06-13-2010 at 06:56 AM.. Reason: Please use code tags
Sponsored Links
    #2  
Old 06-13-2010
pravin27 pravin27 is offline Forum Advisor  
Advisor
 
Join Date: Sep 2009
Last Activity: 23 April 2014, 8:23 AM EDT
Location: ./India/Mumbai
Posts: 1,206
Thanks: 55
Thanked 268 Times in 261 Posts
Hi,
I have tried your code and its work fine for me.Please post the program.sh and error if you are recieving

Code:
while read -r line; do
 echo $line > file #this  work for me
  ##/bin/echo $line | /usr/local/example/program.sh #this works
done

Sponsored Links
    #3  
Old 06-13-2010
timber333 timber333 is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 13 June 2010, 10:27 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
program.sh is working for me.
Anything else doesn't, no errors. I have tried this exhaustively. I think I'll just give up cause its driving me nuts.

The actual scripts I wrote are much longer. It didn't work for them, and when I narrowed it down this was the main problem.

---------- Post updated at 09:21 AM ---------- Previous update was at 08:34 AM ----------

Okay, so my fault here I over simplified my example in OP.

This is more of what I'm doing which for the line previous to the send_nsca binary program is not working:

in shell:


Code:
echo "host,log,0,message" | program_below.sh

program:


Code:
while read -r line; do
  STRING=$line
        echo $STRING > /usr/local/nagios/syslog/debug #and I see no output in debug file
        echo $STRING | /usr/local/nagios/libexec/send_nsca -H 192.168.93.55 -d ',' -c /usr/local/nagios/etc/send_nsca.cfg #this works
done

HELP!!! LOL

---------- Post updated at 10:27 AM ---------- Previous update was at 09:21 AM ----------

These are all working, my bad, but for some reason they didn't before.

Solved the problem.

I am using syslog-ng program() as a destination for a log.

Apparently, the problem has to do with the absolute path in the script. I'm guessing if calling with program() it leaves the directory that script is called from, so the absolute path is necessary. Either way, it works now to:
absolute_path/debug.log

Thanks for the response, hours upon hours again wasted for nothing, I will ALWAYS freaken add an absolute path to every line of every script from now on. Lesson learned.

Last edited by timber333; 06-13-2010 at 08:40 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
bash: read file line by line (lines have '\0') - not full line has read??? alex_5161 Shell Programming and Scripting 6 04-14-2010 04:58 AM
shell script to read file line by line ani12345 UNIX for Dummies Questions & Answers 4 07-13-2009 04:50 PM
cat file1 read line-per-line then grep -A 15 lines down in fileb irongeekio Shell Programming and Scripting 6 01-28-2009 05:30 AM
I need suggestion on problem read a file line by line and do stuff madi3d8 Shell Programming and Scripting 3 01-15-2009 10:33 AM
query on how to search for a line and read 4th word from that line jaggesh UNIX for Dummies Questions & Answers 4 07-01-2008 10:21 PM



All times are GMT -4. The time now is 07:41 PM.