Script help - loading variables within a while loop - possible?

Is loading variables within a while loop even possible, or a limitation?

data... contained in a variable called data

aaa
bbb
ccc

existing variables... to be populated within a while loop

SITE1=
SITE2=
SITE3=

The variables also require to be used external to the while loop in other parts of the script.

outcome...

SITE1="aaa"
SITE2="bbb"
SITE3="ccc"

I have a working setup using a /tmp/data file for this purpose, however cannot work out how to do this using the data variable and while loops.

while read THERULES; do
   echo $THERULES
   SOMETHING="while-needs-to-run-free"
done <<SSS
$(echo "dont-access-variables-from-pipes")
SSS
echo $SOMETHING
1 Like

thanks for the tip @anon50098793 but I'm not any closer :roll_eyes:

Here the snippet of code prior to wanting to populate the SITE1 SITE2 SITE3 etc variables.

site_names=$(echo "$sorted_region_data" | while read -r output ; do echo "$output" | cut -d "," -f1 ; done)

the output for variable site_names is:

This is where I need to to populate the SITE1= SITE2= SITE3= variables using the data in site_names, so the variables require to be available throughout the script like this:

SITE1="AU Melbourne"
SITE2="AU Perth"
SITE3="AU Sydney"
...

do a web search for 'ash array' or 'busybox array' or something like that and click any link that says stackexchange

there are 1000 ways to do what your asking... and you are much better off learning how to fish...

no success? get creative 'linux script numered variables' etc. etc. etc.

i.e.

eval $(ls -1 /usr/share/zoneinfo/America/. | while read this; do num=$((num + 1)); echo STATE${num}=$this; done)

good luck!

( a simpler solution is to use iterative logic and handle each line as it comes... without numbered variables check @vgaetera s masterpiece opkg-script.sh for ideas )

2 Likes

I've been fishing all day @anon50098793 ! I wouldn't be posting here if I did catch something!

The main issue I have is getting the data into the SITE1 SITE2 SITE3 variables. Easy to do if you write the data to file then retrieve into these variables, but using the data from a variable is where I'm getting burnt!

1 Like

It's not clear why can't you place the code in a loop and loop through the list of sites.
Looks like you are trying to use an array, which you don't actually need.
In this case, the code suggested by @anon50098793 appears to be the most appropriate.


If you really need to index lines:

SITES="/tmp/sites"
for I in $(seq "$(sed -n -e "\$=" "${SITES}")")
do echo "${I}:$(sed -n -e "${I}p" "${SITES}")"
done
1 Like

ALL OK now!

It was my brain that needed sorting, I tried to simplify things by not writing more code than I required by introducing a while loop which didn't agree to incrementing the SITEx variables withing the loop.

I settled for outputting the data to 3x /tmp files:

  echo "$sorted_region_data" | while read -r output ; do echo "$output" | cut -d "," -f1 ; done > "$CONFIGDIR/sites.name"
  echo "$sorted_region_data" | while read -r output ; do echo "$output" | cut -d "," -f2 ; done > "$CONFIGDIR/sites.id"
  echo "$sorted_region_data" | while read -r output ; do echo "$output" | cut -d "," -f3 ; done > "$CONFIGDIR/sites.other"

then loaded the SITEx SITE_IDx SITE_DATAx variables using sed:

  SITE1="$(sed -n 1p "$CONFIGDIR/sites.name")"  &&  SITE_ID1="$(sed -n 1p "$CONFIGDIR/sites.id")"  &&  SITE_DATA1="$(sed -n 1p "$CONFIGDIR/sites.SITE")"
  SITE2="$(sed -n 2p "$CONFIGDIR/sites.name")"  &&  SITE_ID2="$(sed -n 2p "$CONFIGDIR/sites.id")"  &&  SITE_DATA2="$(sed -n 2p "$CONFIGDIR/sites.SITE")"
  SITE3="$(sed -n 3p "$CONFIGDIR/sites.name")"  &&  SITE_ID3="$(sed -n 3p "$CONFIGDIR/sites.id")"  &&  SITE_DATA3="$(sed -n 3p "$CONFIGDIR/sites.SITE")"
  SITE4="$(sed -n 4p "$CONFIGDIR/sites.name")"  &&  SITE_ID4="$(sed -n 4p "$CONFIGDIR/sites.id")"  &&  SITE_DATA4="$(sed -n 4p "$CONFIGDIR/sites.SITE")"
  SITE5="$(sed -n 5p "$CONFIGDIR/sites.name")"  &&  SITE_ID5="$(sed -n 5p "$CONFIGDIR/sites.id")"  &&  SITE_DATA5="$(sed -n 5p "$CONFIGDIR/sites.SITE")"
  ...

Thanks for the input @anon50098793 @vgaetera :wink:

If there is a better method of loading variables (not from a file location) I'm all ears :ear: :ear:


EDIT : I have now rejigged my script to include @anon50098793 's solution :+1:

eval $(echo "$site_names" | while read output; do num=$((num+1)); echo SITE$(printf %03g ${num})=$output; done)
eval $(echo "$site_ids"   | while read output; do num=$((num+1)); echo SITE_ID$(printf %03g ${num})=$output; done)
eval $(echo "$site_geos"  | while read output; do num=$((num+1)); echo SITE_GEO$(printf %03g ${num})=$output; done)

results with these variables created and loaded!

SITE001="AU Melbourne"
SITE002="AU Perth"
SITE003="AU Sydney"
...
SITE_ID001="aus_melbourne"
SITE_ID002="aus_perth"
SITE_ID003="aus"
...
SITE_GEO001="false"
SITE_GEO002="false"
SITE_GEO003="false"

Thanks for the help!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.