LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-21-2009, 07:16 PM   #1
Coolmax
LQ Newbie
 
Registered: Jul 2009
Location: Poland
Distribution: openSUSE, Debian
Posts: 15

Rep: Reputation: 0
[bash] Read file's content into variable problem


Hi, I'm trying to read content of file to variable and use this variable in for loop. The problem is, when I have c++ comment style in file - /*. Spaces in line are also interpreted as separated lines. For example:

Code:
mateusz2@susek:~> cat file.txt
first_line
second_line
third_line
/*
some
text
*/
problem line
again_ok

mateusz2@susek:~> cat script.sh
#!/bin/sh
files="`cat $1`"

for x in $files
do
	echo "$x"
done

mateusz2@susek:~> ./script.sh file.txt
first_line
second_line
third_line
/bin
/boot
/dev
/etc
/home
/lib
/lost+found
/media
/mnt
/opt
/proc
/root
/sbin
/srv
/sys
/tmp
/usr
/var
some
text
dw/
scripts/
problem
line
again_ok
Changing $files to "$files" eliminate these problems but causes that whole content of variable is treated as one string (one execution of loop).
 
Old 08-21-2009, 07:26 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
This should work, if you are willing to change your script:

Code:
#!/bin/sh

cat $1 | while read THISLINE
do
  echo "${THISLINE}"
done
The double quotes around ${THISLINE} are needed.
 
Old 08-22-2009, 04:38 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Your script is
Code:
#!/bin/sh
files="`cat $1`"

for x in $files
do
	echo "$x"
done
Is "files" really "lines"?
 
Old 08-22-2009, 06:20 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
@catkin: What are you trying to say?
files is just a name of a variable, why would it matter if it is files, lines or anything else?

Please elaborate if I misunderstand your remark/question.
 
Old 08-22-2009, 06:47 AM   #5
Coolmax
LQ Newbie
 
Registered: Jul 2009
Location: Poland
Distribution: openSUSE, Debian
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks druuna, but loading file to "files" variable I have in "case of" statement (after getopt), so I change:
Code:
cat $1 | while read x
to:
Code:
echo "$files" | while read x
and this work too Thanks again. Best regards.
 
Old 08-22-2009, 09:58 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Hello Druuna
Quote:
Originally Posted by druuna View Post
@catkin: What are you trying to say?
files is just a name of a variable, why would it matter if it is files, lines or anything else?

Please elaborate if I misunderstand your remark/question.
I was quoting from Coolmax's OP, not your suggestion.

You are 100% correct, of course, any variable names will work but choice of variable names typically reflects the programmer's intentions.

Sometimes understanding what the LQ questioner wants to do is half the task of answering. In this case Coolmax's description and code suggested different things; the variable $files was used to hold lines of a single file. I wanted to clarify the contradiction before answering.

Best

Charles
 
Old 08-31-2009, 05:12 PM   #7
Coolmax
LQ Newbie
 
Registered: Jul 2009
Location: Poland
Distribution: openSUSE, Debian
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Hello Druuna I was quoting from Coolmax's OP, not your suggestion.

You are 100% correct, of course, any variable names will work but choice of variable names typically reflects the programmer's intentions.

Sometimes understanding what the LQ questioner wants to do is half the task of answering. In this case Coolmax's description and code suggested different things; the variable $files was used to hold lines of a single file. I wanted to clarify the contradiction before answering.

Best

Charles
@catkin, I called this variable $files, because lines are URLs to files. Script is simply downloader.
 
  


Reply

Tags
bash, loop, read, variable


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
how to clear a file's content? iclinux Linux - Newbie 7 03-30-2009 03:01 PM
how to search file's content with shell? arcow Linux - Software 2 02-08-2006 11:00 AM
Add file content to a variable (bash)? LinuxSeeker Programming 4 12-19-2005 02:41 PM
Shell script: substitute a file's content according to a map? Chowroc Programming 11 11-15-2005 05:08 AM


All times are GMT -5. The time now is 12:15 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration