LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack Search this Thread
Old 10-24-2008, 02:03 PM   #1
novaraz
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Rep: Reputation: 0
Why does this loop clear variables?


Sorry if this is something obvious, but I can't get this script to work:

Code:
#!/bin/bash

iniconf='c150.dat'
cat $iniconf |while read x1 x2 x3 x4; do
  ((++i))
  echo  $i $x1 $x2 $x3 $x4
  if [ $i -eq 2  ]; then
    npore=$x3
    ngau=$x4
    break
  fi
done 

echo $npore $ngau
If I echo npore and ngau in the loop, it works fine. Once it breaks the loop, the variables return blanks. What's the deal?

The input file contains values separated by spaces, and I need to extract the 3rd and 4rd on the 2nd line.

Thanks!
 
Old 10-24-2008, 02:32 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

your loop starts after a pipe and that's why is executed in a subshell. Variables defined in a subshell are not visible in the parent shell.

To avoid it, use input redirection:
Code:
#!/bin/bash

iniconf='c150.dat'
while read x1 x2 x3 x4; do
  ((++i))
  echo  $i $x1 $x2 $x3 $x4
  if [ $i -eq 2  ]; then
    npore=$x3
    ngau=$x4
    break
  fi
done <$iniconf

echo $npore $ngau
Jan
 
Old 10-24-2008, 03:00 PM   #3
novaraz
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Jan,
The code posted didn't work for me, but the mention of subshell led me to a solution. I don't know whats going on, but this works for now:

Code:
#!/bin/bash

exec 3<>c150.dat
while read x1 x2 x3 x4 x5 x6<&3
do {
  ((++i))
  if [ $i -eq 2  ]; then
    npore=$x3
    ngau=$x4
    break
  fi }
done 
exec 3>&-

echo $npore $ngau
:-)
 
Old 10-24-2008, 03:21 PM   #4
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
That's a bit excessive for this situation:

Code:
iniconf='./c150.dat'
while read x1 x2 x3 x4 < $iniconf
do
  ((++i))
  echo  $i $x1 $x2 $x3 $x4
  if [ $i -eq 2  ]
  then
    npore=$x3
    ngau=$x4
    break
  fi
done

echo $npore $ngau
 
Old 10-24-2008, 03:32 PM   #5
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
or

Code:
$ awk 'NR == 2 {print$3,$4}' c150.dat
 
Old 10-24-2008, 03:44 PM   #6
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

Quote:
Originally Posted by jcookeman View Post
or

Code:
$ awk 'NR == 2 {print$3,$4}' c150.dat
and then assign the output to variables:
Code:
arr=($(awk 'NR == 2 {print$3,$4}' c150.dat))
npore=${arr[0]}
ngau=${arr[1]}
Jan
 
Old 10-25-2008, 08:40 AM   #7
novaraz
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks guys, you rock!
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using for loop on 2 variables muazfarooqaslam Linux - Newbie 8 02-01-2008 08:03 AM
How to check variables in a FOR loop? richikiki Programming 6 07-10-2006 03:57 AM
shell variables becoming zero outside the loop cool244 Programming 4 05-20-2006 03:33 PM
shell variables losing value outside while loop cool244 Programming 1 05-19-2006 10:04 AM


All times are GMT -5. The time now is 04:14 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration