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.
... and it should, cuz that's what you asked Bash to do...
#!/bin/bash
cat /home/test/list | while read line; do echo $line; sleep 2; done
If you want to know what went wrong with your script try running it as "bash -x <scriptname>" (and add "2> <scriptname>.log" if you want it logged as well).
Originally posted by crabboy The IFS by default is set to any whitespace, "newline, tab, space" if you set the field seperator to be newline only, the script works.
#!/bin/bash
IFS='
'
for line in `cat web.xml`; do
echo $line
sleep 1
done
This is useful anyway, I think, because I found out that piping the cat output to 'while read line do {' causes the following code to be executed in a sub-shell, so that any variable that you set within do { } done is automatically forgotten after that. So I've got some questions:
1) How to make the 'cat $file | while read line do {' stuff work and not execute the code in a subshell?
2) Or otherwise, how can I make changes done in a subshell persist after exit? For example, if I change dir and want to stay there after the script, or if I set a variable and want to keep it.
3) Apart from all of this, how can I get a specific line (whose number is known) from a file?
Thanks!
> You can't persist variables upwards to the calling script, sorry!
Shame!
> You can 'dot' a file like you do your ~/.bashrc which runs inline.
Don't get that... I've tried to write a script ".env" that sets $ENV to "Hello", but doesn't exist afterwards.. I think I did't understand what you meant.
Thanx!
you can create functions which you can
use for setting environment or changing directory:
e.g
create a file called say, funcs with the following function:
Code:
billym.devserver>cat funcs # here is a file i created
mycd()
{
cd $1
}
billym.devserver>pwd
/tmp
billym.devserver>. funcs # this puts the function into the current env.
billym.devserver>mycd $HOME # so this now works
billym.devserver>pwd
/export/home/billym
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.