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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-24-2008, 02:03 PM
|
#1
|
|
LQ Newbie
Registered: Dec 2006
Posts: 4
Rep:
|
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!
|
|
|
|
10-24-2008, 02:32 PM
|
#2
|
|
Member
Registered: Jun 2008
Posts: 235
Rep:
|
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
|
|
|
|
10-24-2008, 03:00 PM
|
#3
|
|
LQ Newbie
Registered: Dec 2006
Posts: 4
Original Poster
Rep:
|
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
:-)
|
|
|
|
10-24-2008, 03:21 PM
|
#4
|
|
Member
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417
Rep:
|
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
|
|
|
|
10-24-2008, 03:32 PM
|
#5
|
|
Member
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417
Rep:
|
or
Code:
$ awk 'NR == 2 {print$3,$4}' c150.dat
|
|
|
|
10-24-2008, 03:44 PM
|
#6
|
|
Member
Registered: Jun 2008
Posts: 235
Rep:
|
Moin,
Quote:
Originally Posted by jcookeman
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
|
|
|
|
10-25-2008, 08:40 AM
|
#7
|
|
LQ Newbie
Registered: Dec 2006
Posts: 4
Original Poster
Rep:
|
Thanks guys, you rock!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:14 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|