从数组中的每个文件读取行——条件永远不会成功
Reading lines from each file in an array -- condition never succeeds
我正在尝试将 cat 命令集成到 for 循环中,其中 cat 读取元素“$currentccoutput”,但似乎(我认为)cat 是按字面意思读取该行而不是理解它是一个数组元素使用 txt 文件的名称。
#create an array of text files
currentccoutputs=($currentccfolder'/'*.txt*)
#basic for loop until I can get my cat command working
for currentccoutput in "${currentccoutputs[@]}"; do
cat "$currentccoutput" | while read LINE; do
# I have .txt files with three numbers per line
# that I would like to read / use
IFS=' ' read C1 C2 threshold
if [ $C1 != $C2 ] && [ $threshold \> 0.2 ]; then
echo "Huzzah!!!! Progress at last"
fi
done < "$currrentccoutput" # I don't know what
# this backwards chevron
# does but other people
# have used it...
done
我毫不怀疑此代码段还有其他不完善之处,但我对创建脚本完全陌生,所以我试图将事情保持在我现在所知道的范围内,希望以后会出现复杂的解决方案. (现在,我正在尝试从 A 岛到 B 岛,在那里,一些木头和一些麻绳将被理解和复制。虽然我很感激你的建议 - 并希望有一天建造 - 一艘体面的护卫舰,现在它可能让我有些困惑)。
我连'while''read'或者'LINE'都没用过,都是从别人的解决方案里捏出来的
我已经使用 echo 命令来确保不是我的路径错误,只是我没有正确使用 cat。
您如何使用 cat
的唯一问题是您使用(更好的)shell-内置重定向覆盖它。很好——事实上,更可取;你不应该使用 cat
除非你绝对必须。[1]
是的一个问题是你是运行read LINE
然后是read C1 C2 threshold
,两者来自同一个来源。
这意味着您将每个文件的 first 行读入变量 LINE
(您的代码再也不会查看),并且 second 行进入变量 C1
、C2
和 threshold
。如果有更多行,则将第三行读入LINE
,将第四行读入C1
/C2
/threshold
,依此类推
如果您不想跳过每一行(从第一行开始),只需完全删除 read LINE
,使您的代码类似于:
#!/usr/bin/env bash
case $BASH_VERSION in '') echo "ERROR: This script must be run with bash" >&2; exit 1;; esac
currentccoutputs=( "$currentccfolder"/*.txt )
for currentccoutput in "${currentccoutputs[@]}"; do
while IFS=$' \t\r' read -r c1 c2 threshold; do
if [ "$c1" != "$c2" ] && [ "$(bc -l <<<"$threshold > 0.2")" = 1 ]; then
echo "Huzzah!!!! Progress at last: c1=$c1; c2=$c2; threshold=$threshold"
fi
done < "$currentccoutput"
done
参见:
- BashFAQ #1 - 如何逐行(and/or逐字段)读取文件(数据流、变量)?
- BashFAQ #22 - 如何使用浮点数而不是整数进行计算?(描述上面使用的
bc
习语)
- BashFAQ #24 - 我在管道中的循环中设置变量。为什么它们在循环终止后就消失了?或者,为什么我不能通过管道读取数据?(描述为什么
cat | while read
是个坏主意)
[1] - 是的,这意味着您应该忽略很多(如果不是大部分的话)您在网上找到的 bash 代码示例。 Sturgeon's Law 适用。
我正在尝试将 cat 命令集成到 for 循环中,其中 cat 读取元素“$currentccoutput”,但似乎(我认为)cat 是按字面意思读取该行而不是理解它是一个数组元素使用 txt 文件的名称。
#create an array of text files
currentccoutputs=($currentccfolder'/'*.txt*)
#basic for loop until I can get my cat command working
for currentccoutput in "${currentccoutputs[@]}"; do
cat "$currentccoutput" | while read LINE; do
# I have .txt files with three numbers per line
# that I would like to read / use
IFS=' ' read C1 C2 threshold
if [ $C1 != $C2 ] && [ $threshold \> 0.2 ]; then
echo "Huzzah!!!! Progress at last"
fi
done < "$currrentccoutput" # I don't know what
# this backwards chevron
# does but other people
# have used it...
done
我毫不怀疑此代码段还有其他不完善之处,但我对创建脚本完全陌生,所以我试图将事情保持在我现在所知道的范围内,希望以后会出现复杂的解决方案. (现在,我正在尝试从 A 岛到 B 岛,在那里,一些木头和一些麻绳将被理解和复制。虽然我很感激你的建议 - 并希望有一天建造 - 一艘体面的护卫舰,现在它可能让我有些困惑)。
我连'while''read'或者'LINE'都没用过,都是从别人的解决方案里捏出来的
我已经使用 echo 命令来确保不是我的路径错误,只是我没有正确使用 cat。
您如何使用 cat
的唯一问题是您使用(更好的)shell-内置重定向覆盖它。很好——事实上,更可取;你不应该使用 cat
除非你绝对必须。[1]
是的一个问题是你是运行read LINE
然后是read C1 C2 threshold
,两者来自同一个来源。
这意味着您将每个文件的 first 行读入变量 LINE
(您的代码再也不会查看),并且 second 行进入变量 C1
、C2
和 threshold
。如果有更多行,则将第三行读入LINE
,将第四行读入C1
/C2
/threshold
,依此类推
如果您不想跳过每一行(从第一行开始),只需完全删除 read LINE
,使您的代码类似于:
#!/usr/bin/env bash
case $BASH_VERSION in '') echo "ERROR: This script must be run with bash" >&2; exit 1;; esac
currentccoutputs=( "$currentccfolder"/*.txt )
for currentccoutput in "${currentccoutputs[@]}"; do
while IFS=$' \t\r' read -r c1 c2 threshold; do
if [ "$c1" != "$c2" ] && [ "$(bc -l <<<"$threshold > 0.2")" = 1 ]; then
echo "Huzzah!!!! Progress at last: c1=$c1; c2=$c2; threshold=$threshold"
fi
done < "$currentccoutput"
done
参见:
- BashFAQ #1 - 如何逐行(and/or逐字段)读取文件(数据流、变量)?
- BashFAQ #22 - 如何使用浮点数而不是整数进行计算?(描述上面使用的
bc
习语) - BashFAQ #24 - 我在管道中的循环中设置变量。为什么它们在循环终止后就消失了?或者,为什么我不能通过管道读取数据?(描述为什么
cat | while read
是个坏主意)
[1] - 是的,这意味着您应该忽略很多(如果不是大部分的话)您在网上找到的 bash 代码示例。 Sturgeon's Law 适用。