LinuxQuestions.org
Visit Jeremy's Blog.
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

Tags used in this thread
Popular LQ Tags , , , ,

Reply
 
Thread Tools
Old 08-21-2009, 07:16 PM   #1
Coolmax
LQ Newbie
 
Registered: Jul 2009
Posts: 7
Thanked: 0
[bash] Read file's content into variable problem


[Log in to get rid of this advertisement]
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).
linux Coolmax is offline  
Tag This Post , , , ,
Reply With Quote
Old 08-21-2009, 07:26 PM   #2
druuna
Senior Member
 
Registered: Sep 2003
Location: netherlands
Distribution: lfs
Posts: 3,269
Thanked: 78
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.
druuna is offline     Reply With Quote
Thanked by:
Old 08-22-2009, 04:38 AM   #3
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,855
Blog Entries: 6
Thanked: 226
Your script is
Code:
#!/bin/sh
files="`cat $1`"

for x in $files
do
	echo "$x"
done
Is "files" really "lines"?
linux catkin is offline     Reply With Quote
Old 08-22-2009, 06:20 AM   #4
druuna
Senior Member
 
Registered: Sep 2003
Location: netherlands
Distribution: lfs
Posts: 3,269
Thanked: 78
@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.
druuna is offline     Reply With Quote
Old 08-22-2009, 06:47 AM   #5
Coolmax
LQ Newbie
 
Registered: Jul 2009
Posts: 7
Thanked: 0

Original Poster
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.
linux Coolmax is offline     Reply With Quote
Old 08-22-2009, 09:58 AM   #6
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,855
Blog Entries: 6
Thanked: 226
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
linux catkin is offline     Reply With Quote
Old 08-31-2009, 05:12 PM   #7
Coolmax
LQ Newbie
 
Registered: Jul 2009
Posts: 7
Thanked: 0

Original Poster
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.
linux Coolmax is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 06:48 AM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration