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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Originally posted by clinton
Does anyone know of a way to read an entire file line as an argument to a for loop?
Instead of the "IFS-way" above, I think it's better to use a while loop. Because changing IFS has side-effects you may have to take care of (depending on the rest of your script).
Code:
#!/bin/bash
N=0
cat my.file | while read LINE ; do
N=$((N+1))
echo "Line $N = $LINE"
done
or:
Code:
#!/bin/bash
N=0
while read LINE ; do
N=$((N+1))
echo "Line $N = $LINE"
done < my.file
P.S. Instead of [ end code ] use [ /code ]
(without the spaces)
Originally posted by clinton
If $IFS is an environment variable, I don't have that set in my environment. Perhaps it is known as something else.....?
$IFS is a special environment variable, and you do have it set, because it alwasy exists. You only (almost) don't see it, because by default is only contains a line-feed.
It think Tinkster means something like this:
Code:
#!/bin/bash
IFS=$'\n'
set $(cat my.file)
# Now the lines are stored in $1, $2, $3, ...
echo $1
echo $2
echo $3
echo $4
I'm trying to read a file in using the syntax above to read in a list of users and create a user.bat for them for some reason this doesn't seem to work:
Code:
#!/bin/bash
FS=$'\n'
set $(cat users.txt)
for i in $FS; do
`cp logon.bat ./$1.bat`
# Now the lines are stored in $1, $2, $3, ...
echo $1
echo $2
echo $3
echo $4
done
Okay I've made some changes to the file and now it only prints the last line of the file, its not iterating thru the loop:
Code:
#!/bin/bash
i=0;
FS=$'\n'
set $(cat users.txt)
for i in $_; do
#cp ~/scripts/logon.bat ./$i.bat
# Now the lines are stored in $1, $2, $3, ...
echo $i
done
$IFS is a special environment variable, and you do have it set, because it alwasy exists.
It is so important that if you do unset it, bash behaves in all ways as if it had its default value. This means you never need to save the existing value (unless it has been set to something different from the default) as suggested in replies above. It is simpler and works just as well to do
Code:
IFS=whatever_you_need_now
<do stuff using the special $IFS>
unset IFS
Also, strongly prefer the while-read loop over IFS tricks. It's far more intuitive and reliable. See 'Why you don't use "for" for this' in http://mywiki.wooledge.org/BashFAQ/001
Next time, please do not jump into an old thread like this. It's almost always better to start a new thread, especially when an existing one is 4 years old.
Having problems with reading from file. This is my script.
Code:
nr=0
cat lista_ip.txt | while read a
do
if [ xxx]
then
echo "yyyyyyyy"
nr=$(($nr+1))
echo "NR=$nr"
fi
done
echo "NR=$nr"
It does what i needed, counts the number of lines but after it exits the while no values were saved. My $nr variable is back to 0 in the end. Any ideas?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.