LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-10-2010, 11:57 AM   #1
zizou86
LQ Newbie
 
Registered: Jan 2010
Posts: 16

Rep: Reputation: 0
Piping cat output to variable?


Hi,

New to ubuntu and shell scripting in general...

Will like some help in some fundamental syntax concepts please...

Alright, currently I stored some data into a text file. Right now, I would like to output the data from the text file and store it into a variable. Here's what I have so far:

READ_FILE=$(cat $FILE_NAME)

This definitely works and READ_FILE has the necessary data. However, this command will trigger an output to std output and I will see data on the screen, which is not what I want.

I tried:
cat $FILE_NAME | $READ_FILE

and various other variants of this. It does not output to std output but neither does anything gets stored into $READ_FILE.

I tried:
cat $FILE_NAME >> $READ_FILE

and it arrived at an error of "ambiguous redirect".

Please assist.

Thank you.
 
Old 01-10-2010, 12:01 PM   #2
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 547

Rep: Reputation: 115Reputation: 115
READ_FILE=`cat FILE_NAME`
 
Old 01-10-2010, 12:11 PM   #3
zizou86
LQ Newbie
 
Registered: Jan 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by devnull10 View Post
READ_FILE=`cat FILE_NAME`
Hi,

Thanks alot for your fast reply.

I tried it, however I got the following error:

FILE_NAME="books"
READ_FILE=`cat FILE_NAME`

cat: FILE_NAME: No such file or directory

I tried:

READ_FILE='cat FILE_NAME'

and all I got was the string "cat FILE_NAME" gets stored into READ_FILE.
 
Old 01-10-2010, 12:13 PM   #4
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 547

Rep: Reputation: 115Reputation: 115
sorry, my mistake, I missed out the dollar from the variable!

Code:
READ_FILE=`cat $FILE_NAME`
The reason you get just "cat FILE_NAME" stored in the variable is because you have used single quotes when they are in fact back-tick quotes (key to the left of the number 1 on your keyboard if you are unsure )
 
Old 01-10-2010, 12:25 PM   #5
zizou86
LQ Newbie
 
Registered: Jan 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by devnull10 View Post
sorry, my mistake, I missed out the dollar from the variable!

Code:
READ_FILE=`cat $FILE_NAME`
The reason you get just "cat FILE_NAME" stored in the variable is because you have used single quotes when they are in fact back-tick quotes (key to the left of the number 1 on your keyboard if you are unsure )
Hi,

Thanks for the reply again.

Yup I noticed the backticks, so I tried both backticks and single quotes (in case you mistyped it or something).

I tried
Code:
READ_FILE=`cat $FILE_NAME`
It does work, however the system will be default throw out an echo to the screen, in the sense that it displays `cat $FILE_NAME` output to the screen as well. Is there anyway I can disable this so the system will silently just store it into READ_FILE?
 
Old 01-10-2010, 12:46 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
I thought it is better practice to use "$()" instead of backticks "``", because they can't be confused with commas and are easier to nest.

Code:
READ_FILE=$(cat $FILE_NAME)
 
Old 01-10-2010, 01:51 PM   #7
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,234

Rep: Reputation: 189Reputation: 189
Quote:
Alright, currently I stored some data into a text file. Right now, I would like to output the data from the text file and store it into a variable. Here's what I have so far:

READ_FILE=$(cat $FILE_NAME)

This definitely works and READ_FILE has the necessary data. However, this command will trigger an output to std output and I will see data on the screen, which is not what I want.
are you sure your not doing echo READ_FILE ?
 
Old 01-10-2010, 02:28 PM   #8
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 547

Rep: Reputation: 115Reputation: 115
Quote:
Originally Posted by zizou86 View Post
It does work, however the system will be default throw out an echo to the screen, in the sense that it displays `cat $FILE_NAME` output to the screen as well. Is there anyway I can disable this so the system will silently just store it into READ_FILE?
It seems strange that it would do this.
You could try:

Code:
READ_FILE=`cat $FILE_NAME > /dev/null`
Quote:
I thought it is better practice to use "$()" instead of backticks "``", because they can't be confused with commas and are easier to nest.
It might be, I just use backticks out of habit lol.
 
Old 01-10-2010, 02:56 PM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
Quote:
I thought it is better practice to use "$()" instead of backticks "``", because they can't be confused with commas and are easier to nest.
Correct, though I think you mean single-quote(') rather than comma(,).

I recommend using
Code:
read -r -d '' contents < "$filename"
This is a built-in, so you don't spawn extra processes you don't really need.
 
Old 01-10-2010, 03:00 PM   #10
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
This should work also:
READ_FILE="$(cat $FILE_NAME)"
I find it strange that you get an echo to stdout with the original command. What shell are you using?
 
Old 01-10-2010, 03:05 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by tuxdev View Post
Correct, though I think you mean single-quote(') rather than comma(,).
That's right.
 
Old 01-13-2010, 12:08 PM   #12
zizou86
LQ Newbie
 
Registered: Jan 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Hi,

Code:
READ_FILE="$(cat $FILE_NAME)"
This line seems to work now. No idea why it did not work initially, I probably messed up something.

Thanks all for your brilliant help thus far!i
 
Old 01-13-2010, 04:17 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by zizou86 View Post
Hi,

Code:
READ_FILE="$(cat $FILE_NAME)"
This line seems to work now. No idea why it did not work initially, I probably messed up something.

Thanks all for your brilliant help thus far!i
You did not put quotes around $(cat $FILE_NAME) in your initial post.
 
  


Reply


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
Piping NC output to Logger for syslog karmacop81 Programming 2 11-23-2009 04:52 AM
piping output of find to vim babu198649 Linux - Newbie 4 07-24-2008 07:25 AM
Piping output between processes. C. banan Programming 1 11-13-2006 12:25 AM
Piping output to text files corbis_demon Linux - General 3 10-12-2004 04:33 AM
Piping output to a text file joadoor Linux - Newbie 7 04-19-2002 04:50 AM


All times are GMT -5. The time now is 08:49 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration