如何使用全局数组在bash?

编程入门 行业动态 更新时间:2024-10-28 18:31:14
本文介绍了如何使用全局数组在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中运行,而不是。看到: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?

本文发布于:2023-11-17 15:29:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1610258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何使用   全局   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!