On 26/07/2022 14:25, kf wrote:
Obviously I've reduced and rewitten it to better point out the problem in this script. The question is, how should the variable "count" be declared so that it isn't treated as two, completely different variables (with exactly the same name)?
If you want to try to run it, it takes a filename as an commandline argument.
======================== #!/bin/bash
#How to declare the variable "count"??? count=0
cat $1 | while read line do count=$((count+1)) echo -n "$count " done
echo echo File $1 has $count lines.
========================
Thanks.
The pipe character ('|') at line 6 creates a subshell => the result of the increment is not passed on the the father. Use input re-direction instead, and it works: ---------------------<cut>----------------------- #!/bin/bash #How to declare the variable "count"??? count=0 while read line; do count=$(( $count + 1 )) echo -n "$count " done < $1 echo echo File $1 has $count lines. ---------------------<cut>----------------------- This being said, 'wc -l' would do the trick, e.g.: ---------------------<cut>----------------------- count=$(wc -l $1 | cut -d ' ' -f 1) ---------------------<cut>----------------------- (unless the point was not about counting lines, of course) HTH Ph. A. -- *Philippe Andersson* Unix System Administrator IBA Particle Therapy | Tel: +32-10-475.983 Fax: +32-10-487.707 eMail: pan@iba-group.com <http://www.iba-worldwide.com> Disclaimer | Use of IBA e-communication<https://iba-worldwide.com/disclaimer> The contents of this e-mail message and any attachments are intended solely for the recipient (s) named above. This communication is intended to be and to remain confidential and may be protected by intellectual property rights. Any use of the information contained herein (including but not limited to, total or partial reproduction, communication or distribution of any form) by persons other than the designated recipient(s) is prohibited. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free. Ion Beam Applications does not accept liability for any such errors. Thank you for your cooperation.