将列表从文本文件加载到Bash脚本 [英] Load List From Text File To Bash Script

查看:112
本文介绍了将列表从文本文件加载到Bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.txt文件,其中包含

I've a .txt file which contains

abc.com
google.com
....
....
yahoo.com

我有兴趣将其作为列表加载到bash脚本中(即Domain_List=( "abc.com" "google.com" .... "yahoo.com")).有可能吗?

And I'm interested in loading it to a bash script as a list (i.e. Domain_List=( "abc.com" "google.com" .... "yahoo.com") ). Is it possible to do?

其他信息,一旦获得列表,它就会在for循环和if语句中使用.

Additional information, once the list is obtained it is used in a for loop and if statements.

 for i in "${Domain_list[@]}
 do
     if grep -q "${Domain_list[counter]}" domains.log
   ....
   ....
     fi
 ....
     let counter=counter+1
 done

谢谢

更新: 我将格式更改为Domain_list =("google.com ...." yahoo.com),使用source Doamin.txt允许我将Domain_list用作bash脚本中的列表.

Update: I've changed the format to Domain_list=( "google.com .... "yahoo.com" ), and using source Doamin.txt allows me to use Domain_list as a list in the bash script.

#!/bin/bash

counter=0
source domain.txt
for i in "${domain_list[@]}"
do
   echo "${domain_list[counter]}"
   let counter=counter+1
done
echo "$counter"

推荐答案

我使用了source命令,并且工作正常.

I used the source command, and it works fine.

 #!/bin/bash

 counter=0
 source domain.txt
 for i in "${domain_list[@]}"
 do
    echo "${domain_list[counter]}"
    let counter=counter+1
 done
 echo "$counter"

这篇关于将列表从文本文件加载到Bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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