如何使用全局数组在bash? [英] How to use global arrays in bash?

查看:165
本文介绍了如何使用全局数组在bash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过SO搜索和谷歌为解决我的问题2天,但我没有运气。我的问题是,我有一个数组,我开始它的元素0的,我修改其元素的值里面\\ while循环范围。但是,当我打印外循环数组的价值观,我觉得这一切0作为其初始值!

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 for\while loop scope. But when I print the array's values outside the loop, I find it all 0's as their initial values!!

下面是我的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 ',' '\n' | wc -l)
# "cat CSV_File" for getting text of the file.
# "sed -n 1'p'" for getting only first line.
# "tr ',' '\n'" for replacing ',' with '\n'.
# "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 ',' '\n' | 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

...

和这里是CSV_File内容:

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

需要注意的是,当我把回声 if语句里面,它打印正确的值:

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?

推荐答案

|而读元件;操作完成在子壳运行,所以其对全球更新都将丢失子壳退出时。

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.

一个解决方法是使用bash的进程​​替换,让输入while循环在子shell中运行,而不是。看到:
<一href=\"http://stackoverflow.com/questions/2376031/reading-multiple-lines-in-bash-without-spawning-a-new-subshell\">Reading多条线路在bash不产卵一个新的子shell?

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?

这篇关于如何使用全局数组在bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆