从bash中的ascii文件读取的字符串创建多个循环 [英] Creating multiple loops with string read from ascii files in bash
问题描述
我想在一个内部循环中创建一个内部循环,其中包含从bash脚本中的ascii文件读取的字符串.我有两个包含字符串列表的输入ascii文件:
I want to create an internal loop inside a loop with strings which are read from ascii files in a bash script. I have two input ascii files which contain a list of strings:
files.txt:
filename1
filename2
polls.txt:
CO
NOx
SOx
我使用了IFS,内容如下:
I used IFS and read as following :
while IFS= read -r file; do
printf '%s\n' "$file"
while IFS= read -r poll; do
printf '%s\n' "$poll"
ogrinfo $Database_maindir/$file".shp" -sql "ALTER TABLE $file ADD COLUMN $poll float"
done < "$Database_maindir/polls.txt"
done < "$Database_maindir/files.txt"
我得到以下打印和错误:
I get the following prints and errors:
CO
CO
FAILURE:
Unable to open datasource `/home/CO.shp' with the following drivers.
它仅读取"polls.txt"字符串.
It reads only the string of "polls.txt".
正确的输出应为:
filename1
CO
ogrinfo /home/"filename1.shp" -sql "ALTER TABLE filename1 ADD COLUMN CO float"
NOx
ogrinfo /home/"filename1.shp" -sql "ALTER TABLE filename1 ADD COLUMN NOx float"
Sox
ogrinfo /home/"filename1.shp" -sql "ALTER TABLE filename1 ADD COLUMN Sox float"
filename2
CO
ogrinfo /home/"filename2.shp" -sql "ALTER TABLE filename2 ADD COLUMN CO float"
NOx
ogrinfo /home/"filename2.shp" -sql "ALTER TABLE filename2 ADD COLUMN NOx float"
Sox
ogrinfo /home/"filename2.shp" -sql "ALTER TABLE filename2 ADD COLUMN Sox float"
因此,对于每个文件(例如filename1,filename2),我想执行COMMAND(例如ogrinfo命令,该命令将更新文件中的列,而这些列是polls.txt中列出的不同污染物.
So, for each file (e.g. filename1, filename2) I want to execute a COMMAND (e.g. an ogrinfo command which updates the columns of a file where the columns are different pollutants listed in the polls.txt.
在第一个循环中,我想从files.txt中读取字符串,并将字符串$ file保留为字符串的名称(例如filename1).在第二个循环中,我想从"polls.txt"中读取字符串,并保留字符串$ poll(例如CO).
In the first loop I want to read the strings from the files.txt and keep the string $file with the name of the string (e.g. filename1). In the second loop I want to read the strings from "polls.txt" and keep the string $poll (e.g.CO).
但是,似乎在第一个循环中,它从找到的第一个文件中读取了字符串,即"polls.txt",因此$ file给出了字符串"CO".
However, it seems that for the first loop it reads the string from the first file it finds , meaning the "polls.txt", so $file gives the string "CO".
我如何定义第一个循环应读取第二个txt文件?
How I could define that the first loop should read the second txt file?
推荐答案
- 使用专用文件描述符.这样,您可以100%确保每个
read
从正确的流中读取.而且循环内的命令也不会从流中读取.
- Use dedicated file descriptors. That way you are 100% sure each
read
reads from the proper stream. And the commands inside the loop also don't read from the stream.
while IFS= read -r -u 3 file; do
printf '%s\n' "$file"
while IFS= read -r -u 4 poll; do
printf '%s\n' "$poll"
echo ogrinfo $Database_maindir/$file".shp" -sql "ALTER TABLE $file ADD COLUMN $poll float"
done 4< "$Database_maindir/polls.txt"
done 3< "$Database_maindir/files.txt"
paste <(
awk -v nr=$(wc -l <polls.txt) \
'{for (i = 1; i <= nr; i++) print}' files.txt
) <(
seq $(wc -l <files.txt) |
xargs -n1 cat polls.txt
) |
xargs -n2 sh -c 'echo ogrinfo "$1".shp -sql "ALTER TANLE $1 ADD COLUMN $2 float"' --
这篇关于从bash中的ascii文件读取的字符串创建多个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!