Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

linux - How to use global arrays in bash?

I was searching through SO and google for a solution to my problem for 2 days but I had no luck. My problem is that I have an array and I initiate its elements with 0's, and I modify its elements' values inside forwhile loop scope. But when I print the array's values outside the loop, I find it all 0's as their initial values!!

Here is my code:

#!/bin/bash
#
# This is a script to reformat CSV files into pretty-formatted txt files.

clear

echo 'This is a script to reformat CSV files into pretty-formatted txt files.'
echo

# Get number of elements.
elements=$(cat CSV_File | sed -n 1'p' | tr ',' '
' | wc -l)
# "cat CSV_File" for getting text of the file.
# "sed -n 1'p'" for getting only first line.
# "tr ',' '
'" for replacing ',' with '
'.
# "wc -l" for getting number of lines (i.e number of elements).

# Get number of lines.
lines=$(cat CSV_File | wc -l)
# "cat CSV_File" for getting text of the file.
# "wc -l" for getting number of lines.

# Initiate an array for elements' lengths.
for ((i=1; $i < (( $elements + 1 )) ;i++))
do lengths[$i]="0"
done

# Find tallest element of each column and put the values into 'lengths' array.
for ((i=1; $i <= $lines ;i++))
do
   j="1"
   cat CSV_File | sed -n ${i}'p' | tr ',' '
' | while read element
   do
      if [ ${lengths[$j]} -le ${#element} ]
      then lengths[$j]=${#element}
      fi
      (( j++ ))
   done
done

echo ${lengths[1]} # It should print 5 not 0
echo ${lengths[2]} # It should print 4 not 0
echo ${lengths[3]} # It should print 19 not 0
echo ${lengths[4]} # It should print 11 not 0
echo ${lengths[5]} # It should print 2 not 0
echo ${lengths[6]} # It should print 5 not 0
echo ${lengths[7]} # It should print 14 not 0
echo

...

and here is CSV_File contents:

Jones,Bill,235 S. Williams St.,Denver,CO,80221,(303) 244-7989
Smith,Tom,404 Polk Ave.,Los Angeles,CA,90003,(213) 879-5612

Note that when I put echo inside the if statement, it prints the correct value:

if [ ${lengths[$j]} -le ${#element} ]
then lengths[$j]=${#element}
     echo ${lengths[$j]}
fi

What is wrong I'm doing? Am I missing something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The | while read element; do ... done is running in a sub-shell, so its updates to the global are lost when the sub-shell exits.

One solution is to use bash's 'process substitution' to get the input to the while loop to run in a subshell instead. See: Reading multiple lines in bash without spawning a new subshell?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...