problem access array outside of loop in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem access array outside of loop in bash
# 1  
Old 12-10-2011
problem access array outside of loop in bash

Below is a test script I was trying to use so that I could understand why the logic was not working in a larger script. While accessing and printing array data inside the while loop, everything is fine. Outside the loop, i guess everything is null?? The for loop that is meant to cycle through the array prints blank lines. The last two test echo statements after the call of the function in the main body also fail to output array information. The file addon_list_one_line.txt is a single line of strings that can be seen during the initial output below.

Can anyone explain to me what is wrong after examining the script and output below?

Code:
#!/bin/bash

addon_list=addon_list_one_line.txt

 function test_function {
   cat $addon_list|while read line        
   do
     ADDON=( $line )
     echo "\$line is $line"
     echo "whole array is ${ADDON[@]}"
     echo "first array item is ${ADDON[0]}"
     echo "last array item is ${ADDON[14]}"
   done
   for a in {0..14}
      do
           echo "${ADDON[$a]}"    
      done
}

#main body

test_function
echo "last array item is ${ADDON[14]}"
echo "original_array = ${ADDON[@]}"

output of script:

> ./array_example2.sh ~/scripts/wow_addons
$line is auctioneer recount basic-minimap bison deadly-boss-mods dominos heal-bot-continued holy-power-notifier ice-hud omen-threat-meter postal prat-3-0 quartz quest-helper skada
whole array is auctioneer recount basic-minimap bison deadly-boss-mods dominos heal-bot-continued holy-power-notifier ice-hud omen-threat-meter postal prat-3-0 quartz quest-helper skada
first array item is auctioneer
last array item is skada
printing each array element individually...















last array item is
original_array =
# 2  
Old 12-10-2011
This is a classic shortcoming with bash. Easy solution is to use kshell (ksh) instead. Or you can work round it. Check this thread for a complete discussion

https://www.unix.com/shell-programmin...p-outside.html
# 3  
Old 12-11-2011
problem access array outside of loop in bash
=> problem access variables in pipes
Use redirect, not pipe. Then works fine.
Bash/ksh makers has ideological difference.
Code:
cmd1 | cmd2

create variable in second process, ofcource not usable in 1st process. After process die, no variables.

Ksh maker like more to support idea of original command process idea, ksh use shared memory to handle variables in this kind of use. Works logically as original idea has to write command line even
some part of commands has done in other process. Variables are problem. How to handle variables in pipe. There is difference between ksh and bash.
=> use io redirect = one process, no problem.

Code:
while read line         
do      
  ADDON=( $line )      
  echo "\$line is $line"      
  echo "whole array is ${ADDON[@]}"      
  echo "first array item is ${ADDON[0]}"      
  echo "last array item is ${ADDON[14]}" 
done < $addon_list

Here is example which show this difference between bash/dash/ksh/sh/...
Code:
# testcode, a and b variables have created in pipe = subprocess
a1=11
b1=22
export a1 b1
echo $a1 $b1 | (read a b ; echo $a $b) | xargs echo "ab"  ; echo "a1:$a1 b1:$b1 a:$a b:$b"

Test it:
bash testcode
ksh testcode
...

Last edited by kshji; 12-11-2011 at 11:01 AM..
# 4  
Old 12-12-2011
Thanks for the reply. It was a very simple fix to use the redirect instead of the pipe!
# 5  
Old 12-12-2011
Quote:
Originally Posted by agama
This is a classic shortcoming with bash.
As satisfying as it may be to blame the universe's problems on BASH, nearly any bourne shell except ksh does this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash for loop array

Hi there, A bit new to bash and am having an issue with a for loop. I look for filenames in a specified directory and pull the date string from each meeting a certain criteria, and then would like to make a directory for each date found, like this: search 20180101.gz 20180102.gz 20180103.gz... (5 Replies)
Discussion started by: mwheeler12
5 Replies

2. UNIX for Beginners Questions & Answers

Bash array variables are changed in loop runtime

I am trying to check whether particular host and port are responding or not. I am using below script to check. but node_port array that i am using in loop is getting replaced with previous iteration value. Script and output is given. Please help me to understanding why node_port values are... (5 Replies)
Discussion started by: tmalik79
5 Replies

3. Shell Programming and Scripting

Bash - array loop performance

Hi, another little question... "sn" is an array whose elements can vary from about 55,000 to about 150,000 elements. Each element consists of an integer between 0-255, eg: ${sn} contain the value: 103 . For a decrypt-procedure I need scroll all the elements 4 or 5 times. Here is an example of... (15 Replies)
Discussion started by: math4
15 Replies

4. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

5. Shell Programming and Scripting

Loop and array problem

Hi, I have the following problem that is beyond what I can currently do with bash scripting. In file 1, I have ~ 2500000 values. Note this file is not sorted. 3 19 LABEL_A 3 37 LABEL_B 2 12 LABEL_C 1 15 LABEL_D I have a list of values in "file 2" ~ 25000 unique lines: Note -... (6 Replies)
Discussion started by: hubleo
6 Replies

6. Shell Programming and Scripting

Split the file and access that files through array and loop

Hi All, the below is my requirement.. i need to split the file based on line and put that files in a array and need to access that files through loop finally i should send the files through mail.. how can we achieve this ..I am new to shell script please guide me.. I am using KSH.. ... (11 Replies)
Discussion started by: kalidoss
11 Replies

7. Shell Programming and Scripting

Array and Loop Problem

I've got this problem, if I modify an array in the loop and print it, everything is fine as long as I stay in the loop. But, when I print it outside the loop, nothing happens... How can I solve this problem? Here I prepared a sample for you to see my problem; zgrw@Rain:~$ cat test asd 123... (4 Replies)
Discussion started by: zgrw
4 Replies

8. Shell Programming and Scripting

problem in access in array variables

hi all, its me again!!! i've requirement like this: i want to create a file & an array with its name having the filename as its substring. here is the test script!! #!/bin/bash touch $1 declare -a $1_rec; echo -n "$1_rec: " read $1_rec; echo $]; now see output: this is when i enter... (7 Replies)
Discussion started by: tprayush
7 Replies

9. Solaris

Unable to access 3500 FC array. Where is the problem?..

Hi, I have two Sun Fire V490 with Solaris 10 5/08, FC switch and two Sun StorageTek 3500 FC arrays. Each array is connected to switch and to one server at a time. In the last week I installed Solaris 10 5/08 on both servers and set up Sun Cluster, version 3.2. At wednesday all was fine - all... (7 Replies)
Discussion started by: Sapfeer
7 Replies

10. Shell Programming and Scripting

FTP and run a loop for array problem

Hello, I have a problem with my script whereby it does not want to loop. The function of my script is to FTP into a server and go to each directory/volume in the array 'VOL'. The way the loop is suppose to work, is to go into the first volume, get the files of R(yesterday's date) and... (3 Replies)
Discussion started by: tuffgong2008
3 Replies
Login or Register to Ask a Question