ProgrammingThis 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.
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).
@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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.