Shell中文本或CSV文件中的数组值 [英] Array Values from Text or CSV File in Shell

查看:233
本文介绍了Shell中文本或CSV文件中的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,其中声明了如下数组:

I am having a script in which I am declaring arrays like below:

declare -a Pool=("Terrace|HouseTerrace_")
declare -a Library=("lib1 lib2 lib3")
declare -a Name=("Guru/Raju Albert Deepak")
declare -a Email=("Guru@american.com,Raju@american.com Albert@american.com Deepak@amaerican.com")
Sub=("Media Rotation")
i=0
while [ $i -lt ${#Pool[@]}  ]
do
command blah balh blah
i=`expr $i + 1`
done

现在我想要的是代替在此处声明aaray的值,我应该从文件(text或csv)中将其导入,或者有没有更好的方法来执行此任务.我不想将脚本文件的访问权限授予用户.

Now what i want is that instead of declaring values in aaray here i should import it from file (either text or csv ) or is there any better way perfroming this task. I don want to give access to script file to users.

推荐答案

这是部分答案,以帮助启动OP工作.

This is a partial answer, to help kick-start the OP effort.

bash"readarray"可用于从文件加载数组.例如,它们的代码declare -a email=("a@a.com" "b@b.com" ...)可以替换为

The bash "readarray" can be used to load an array from a file. For example they code declare -a email=("a@a.com" "b@b.com" ...) can be replaced with

readarray email < email.txt

email.txt在哪里(使用发布的数据)

Where email.txt is (using posted data)

Guru@american.com
Raju@american.com
Albert@american.com
...

另一个建议的改进将是使用for item in而不是while循环来遍历数组.代替i=0 ; while [ $i -lt ${#Pool[@]} ] ; do ... ; done

Another suggest improvement will be to use for item in instead of while loops to iterate over arrays. Instead of i=0 ; while [ $i -lt ${#Pool[@]} ] ; do ... ; done

for p in "${pool[@]}" ; do
   command $p
done

这篇关于Shell中文本或CSV文件中的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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