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.
I am not quite unfamiliar with bash scripts, yet, I am faced with a rather lame problem.
I have a short bash script code that is something like this:
Code:
IFS="
"
cat $file | while read line; do
len=${#line}
let "total = $total + $len"
echo "$total"
done
echo "$total"
My problem is that the value of variable "total" is always zero outside the loop. I.e. the final echo command always reports 0, whilst the echo command inside the while loop reports the correct total.
Are bash variables local to the while loop in this syntax? If yes, is there a workaround, to use their values outside the loop?
P.S.
The above code snippet is of course just a meaningless example to show the problem. The code would be part of a script that splits up a large text file into smaller chunks having a fixed maximum number of characters.
The "split" command has proven to be unsuitable for this purpose, as it happily breaks unicode characters in two.
Anyway, if there is an other code snipped for the purpose using bash/awk/sed, it would be welcome.
The external call works if you use redirection instead of a pipe. I'm not sure why though. It probably has something to do with the loop running in a subshell.
Code:
IFS="
"
while read line; do
len=${#line}
let "total = $total + $len"
echo "$total"
done <$file
echo "$total"
Besides, redirection is a bash built-in function, so it's more efficient than having to call the external cat command.
cat $file | while sets up a pipeline with the while running in a subshell. Variable settings made in the subshell are lost when the subshell exits.
Using input redirect avoids the problem
Code:
while IFS='
' read line
do
len=${#line}
let "total = $total + $len"
echo "$total"
done < input
echo "Loop done, total: $total"
I just found out in another thread that the above idiom saves you having to reset IFS after the loop (but you may want its usual value in the loop, in which case it's no good o)).
Edit: David the H said it while I was typing
Best
Charles
Last edited by catkin; 07-19-2009 at 07:10 AM.
Reason: See comment in post
PS: can you explain in more detail exactly what you're trying to accomplish in "splitting a large text file"? Please give us an example of the text you want to split, and the final form you want it to be in, so we can help you find the solution you need.
Thanks for your help, redirection works like a charm!
P.S.
The code is part of a larger converter script that makes e-books (in the most common document formats, except lit) readable by iPod (in iPod Note format).
The e-book is first converted to an UTF8 text file, then split up to max. 4012-characters-long chunks (iPod Note), each having a html-like link at the end to the next chunk.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.