LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2015, 12:39 PM   #1
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,232

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
bash: incrementing variable outside scope of loop


Code:
[schneidz@hyper 2015-06-13]$ head test.*
==> test.ksh <==
#!/bin/bash

s=1; cat test.lst | while read line
do
 echo $s - $line
 ((s++))
done
echo total lines = $s


==> test.lst <==
there
are
4
lights !!!
[schneidz@hyper 2015-06-13]$ ./test.ksh 
1 - there
2 - are
3 - 4
4 - lights !!!
total lines = 1
how do i get it to print 4 ?
 
Old 06-17-2015, 01:00 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,968

Rep: Reputation: 1749Reputation: 1749Reputation: 1749Reputation: 1749Reputation: 1749Reputation: 1749Reputation: 1749Reputation: 1749Reputation: 1749Reputation: 1749Reputation: 1749
Your problem is that the use of the pipe causes the whole while loop to run in a separate process. Nothing in that child process can affect variables in the parent. This will work:
Code:
#!/bin/bash

s=0
while read line
do
 ((s++))
 echo $s - $line
done <test.lst
echo total lines = $s
Note that I've changed the initialization and re-ordered the lines to avoid an off-by-one error in the reported total.
 
1 members found this post helpful.
Old 06-17-2015, 01:18 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,232

Original Poster
Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
^ thanks, not quite solved. lets say instead of a file. the input of the loop is a string of commands:
Code:
#!/bin/bash

dw=some.file
s=0
while read line
do
 ((s++))
 echo $s - $line
done < awk 'BEGIN { FS="\n" ; RS=""} /^isos:/ {print $0}' $dw | tail -n+2
echo total lines = $s
 
Old 06-17-2015, 02:28 PM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 & 7
Posts: 2,975

Rep: Reputation: 795Reputation: 795Reputation: 795Reputation: 795Reputation: 795Reputation: 795Reputation: 795
You need to put variable s in the same shell as the while loop. Use parens to make subshell:

Code:
#!/bin/bash

awk 'BEGIN { FS="\n" ; RS=""} /^isos:/ {print $0}' $dw | tail -n+2 | ( 
  s=0
  while read line
  do
     ((s++))
     echo $s - $line
  done
  echo total lines = $s
)
 
Old 06-17-2015, 03:13 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,613

Rep: Reputation: 2938Reputation: 2938Reputation: 2938Reputation: 2938Reputation: 2938Reputation: 2938Reputation: 2938Reputation: 2938Reputation: 2938Reputation: 2938Reputation: 2938
nah ... just use process substitution:
Code:
#!/bin/bash

dw=some.file
s=0
while read line
do
 ((s++))
 echo $s - $line
done < <(awk 'BEGIN { FS="\n" ; RS=""} /^isos:/ {print $0}' $dw | tail -n+2)
echo total lines = $s
Important note, the space is required between the 2 < signs
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH incrementing a variable Garrett85 Programming 3 01-12-2013 09:05 PM
[SOLVED] (BASH) How to keep variable in scope? Dick Dastardly Programming 8 07-15-2011 12:51 PM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
Easy Bash Loop Scope Issue? scsi.king Programming 7 04-25-2007 05:41 AM
incrementing variable in an until loop jeffreybluml Programming 2 05-12-2005 07:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration