LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-23-2017, 04:10 AM   #1
symeon.mattes
LQ Newbie
 
Registered: Sep 2013
Posts: 13

Rep: Reputation: Disabled
visibility of bash variables


Hi,

I have the following piece of bash code which doesn't work as expected.

Code:
interfaces=(eth1)
i=0
## let's consider that from the query I have 4 rows
psql -U $user -d $database -t -c "select all interfaces'"  |
while read record; do
    ((i++))
     interfaces[$i]=$record
     echo $i #1 
     echo ${interfaces[*]} #2
done
echo $i   #3
echo ${interfaces[*]} #4
what I expected for #3 and #4 was to print out i=4 and interfaces="eth1 eth2 eth3 eth4 eth5". However what I get is i=0 and and interfaces=eth1. I print out in #1 and #2 and the values are printed out correctly. What am I doing wrong?

Last edited by symeon.mattes; 06-23-2017 at 04:11 AM.
 
Old 06-23-2017, 05:09 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,568

Rep: Reputation: 5865Reputation: 5865Reputation: 5865Reputation: 5865Reputation: 5865Reputation: 5865Reputation: 5865Reputation: 5865Reputation: 5865Reputation: 5865Reputation: 5865
The pipe creates a subshell so the variables scopes outside the while loop are different. Adding () in this case is the easiest fix.

Code:
 
interfaces=(eth1)
i=0
## let's consider that from the query I have 4 rows
psql -U $user -d $database -t -c "select all interfaces'"  |
( while read record; do
    ((i++))
     interfaces[$i]=$record
     echo $i #1 
     echo ${interfaces[*]} #2
done
echo $i   #3
echo ${interfaces[*]} )#4
By the way your posted SQL statement syntax does not look correct. You have an extra single quote and you did not specify the table i.e.
"select all interfaces from table_name"
 
Old 06-23-2017, 05:40 AM   #3
symeon.mattes
LQ Newbie
 
Registered: Sep 2013
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks for the reply. The thing is that I need that variable everywhere. I found another way:

Quote:
read -ra vars <<< `psql -U $user -d $database -t -c "select all interfaces"`

# put all interfaces into one variable
interfaces=($interface);i=0
for intf in "${vars[@]}";do
((i++))
#if [ -n "$intf" ]; then
interfaces[$i]=$intf
#fi
done

The query is just an example...it's not the actually one!
 
Old 06-23-2017, 06:19 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,630

Rep: Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265
the "usual" way is:
Code:
while read var;
do
....
done < <(command)
see here: http://linuxshellaccount.blogspot.hu...output-to.html
or: https://stackoverflow.com/questions/...loop-using-sed
 
Old 06-25-2017, 04:08 AM   #5
sweepnine
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
If you only care for #3 and #4 you can skip the loop and avoid variable scope issues.
Code:
INTERFACES=($(psql .. -t ..))
N_INTERFACES=${#INTERFACES[@]}
 
  


Reply

Tags
bash, programming, variable


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Visibility of variables not declared in the header file (GNU C). stf92 Programming 15 01-08-2014 04:30 PM
Bash to find all placeholder variables in a file and replace will real variables spadez Programming 6 11-26-2013 12:24 PM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
BASH visibility problem ZT13 Programming 4 12-24-2006 12:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:29 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