将列表从文本文件加载到Bash脚本 [英] Load List From Text File To Bash Script
问题描述
我有一个.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屋!