LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 12-06-2011, 05:35 AM   #1
splintercdo
Member
 
Registered: Feb 2011
Posts: 141

Rep: Reputation: 11
Shell variable value disappears


Hey guys,

As I understand now I have 2 row variables one global for script second local for loop.

How do I assign value from local row to global, basically, how to get value of local variable row out of cycle?

Code:

Code:
x=0; row="blabla"; wget -q "http://www.linuxquestions.org/" -O - | grep ".lqconsulting." | cut -d/ -f3 | cut -d. -f2 | while read row && [ $x -lt 1 ]; do x=$(( x=$x + 1  )); echo "1. $row"; done; echo "2. $row";
Result:
1. lqconsulting
2. blabla
 
Old 12-06-2011, 06:49 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Not sure why you want the loop, given that it only executes once, but this may be what you are looking for:
Code:
#!/bin/bash

while read row
do 
    echo "1. $row"
    break
done < <( wget -q "http://www.linuxquestions.org/" -O - | grep ".lqconsulting." | cut -d/ -f3 | cut -d. -f2 )
The technique is called process substitution.
 
Old 12-06-2011, 02:06 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Your main question is explained here:

http://mywiki.wooledge.org/BashFAQ/024

But more than that, I can't quite understand what the command is supposed to do. The whole logic seems off to me.

Are you just trying to add a number prefix to each of the found instances? If that's the case it would probably be better to simply save the results to an array and use a separate loop to do the print work.

Code:
lqarray=( $(wget -q "http://www.linuxquestions.org/" -O - | grep ".lqconsulting." | cut -d/ -f3 | cut -d. -f2 ) )

i=1
for text in "${lqarray[@]}"; do
	echo "$i. $text"
	(( i++ ))
done
There are probably also cleaner ways to extract the text you want than chaining multiple grep and cut commands, if I knew exactly what text you were trying to match.

Could you please explain what your real goal is, so we can help you to come up with a cleaner solution?
 
  


Reply



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] Variable and shell expansion timbCFCA Linux - Server 2 07-15-2011 01:21 PM
awk and shell variable markux Linux - Server 6 01-19-2009 03:54 AM
Shell scripting : how to name a variable with the value of another variable JimOrJoe Programming 9 06-15-2008 09:36 PM
shell variable sniffer_raghav Linux - Newbie 4 06-12-2008 05:34 AM
variable export in shell MarioT Programming 3 05-16-2006 11:52 PM

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

All times are GMT -5. The time now is 11:20 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
Open Source Consulting | Domain Registration