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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-21-2009, 06:16 PM
|
#1
|
|
LQ Newbie
Registered: Jul 2009
Location: Poland
Distribution: openSUSE, Debian
Posts: 15
Rep:
|
[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).
|
|
|
|
08-21-2009, 06:26 PM
|
#2
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,867
|
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.
|
|
|
|
08-22-2009, 03:38 AM
|
#3
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Your script is
Code:
#!/bin/sh
files="`cat $1`"
for x in $files
do
echo "$x"
done
Is "files" really "lines"?
|
|
|
|
08-22-2009, 05:20 AM
|
#4
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,867
|
@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.
|
|
|
|
08-22-2009, 05:47 AM
|
#5
|
|
LQ Newbie
Registered: Jul 2009
Location: Poland
Distribution: openSUSE, Debian
Posts: 15
Original Poster
Rep:
|
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.
|
|
|
|
08-22-2009, 08:58 AM
|
#6
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Hello Druuna 
Quote:
Originally Posted by druuna
@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
|
|
|
|
08-31-2009, 04:12 PM
|
#7
|
|
LQ Newbie
Registered: Jul 2009
Location: Poland
Distribution: openSUSE, Debian
Posts: 15
Original Poster
Rep:
|
Quote:
Originally Posted by catkin
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:19 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|