LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-27-2011, 03:04 AM   #1
RamPant_CaR
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Rep: Reputation: Disabled
Smile Read from printf question


I have a relatively simple question,


I would like to execute the following line in a script that I have made:

# printf "google.com\ntldp.org\n" | ./myScript

All I need myScript to do is take the input lines
from printf and write them to some file (or variable) within the script.

I can achieve the desired effects from bash:

# printf "google.com\ntldp.org\n" > file1.txt
# cat file1.txt
google.com
tldp.org

I can use the read command in the script to read just the first line (google.com) but the second line does not seem to get saved. Is there anyway to handle this?



Thanks!
 
Old 11-27-2011, 03:05 AM   #2
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,232

Rep: Reputation: 299Reputation: 299Reputation: 299
In your script you can put 'somevar=$(cat /dev/stdin)'
 
Old 11-27-2011, 03:24 AM   #3
RamPant_CaR
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
One more question:

Because of the way I process other forms of input in the script,
it would be convenient if the entried were seperated by new lines.

They are separated by a space, but I would like to to be a new line. Any help?

thanks
 
Old 11-27-2011, 10:07 AM   #4
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,232

Rep: Reputation: 299Reputation: 299Reputation: 299
I guess one way is to use sed

Code:
somevar=$(cat /dev/stdin | sed 's/ /\n/g')
 
Old 11-27-2011, 10:17 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by dive View Post
In your script you can put 'somevar=$(cat /dev/stdin)'
Put double quotes around the $() to preserv whitecpace characters in the input.
 
  


Reply

Tags
pipes, printf


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
error viewing manpages for read, write, printf.... onurozkol Linux - Software 3 07-05-2007 09:18 AM
Printf and ctime question about output when used together. shadow5277 Programming 2 06-22-2007 03:46 AM
C string and printf question exvor Programming 7 12-10-2005 11:53 AM
c printf question. fredgt Programming 5 09-09-2004 06:06 AM
Simple printf question AMMullan Programming 3 02-13-2004 11:12 PM


All times are GMT -5. The time now is 02:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration