我正在致力于加强Debian系统。我有一个带有多行的文本文件。每行以一些选项卡结束,下一行从新行开始。我想阅读每一行,直到文件的末尾。但是卡在while循环中,它会一次循环文件,也循环一行。我想在每行之后停止循环,这样我就可以处理该单行并在得到该行的结果后转到下一行。
我要处理的示例合规性文件
07:45:55 PM Compliance status:Yes 2.1 /tmp partition
07:45:55 PM Compliance status:No 2.5 /var partition
07:45:55 PM Compliance status:No 2.7 /var/log partition
07:45:55 PM Compliance status:Yes 2.9 /home partition
07:45:55 PM Compliance status:Yes 3.1 /boot/grub/grub.cfg
07:45:55 PM Compliance status:No 3.2 /boot/grub/grub.cfg
07:45:55 PM Compliance status:Yes 3.4 etc/shadow
示例代码:
count = 1
while IFS='' read -r line || [[ -n "$line" ]]
do
#echo "Text read from file $count: $line"
some_var=`cut -f4 -d" " <<< $line | cut -d":" -f2` #cuts Yes/No from every line of compliance file
if [ "$some_var" == "No" ]
then
echo "Yes apply remedy"
else
echo "No need to apply remedy"
fi
count=$((count+1))
done < "$1"
答案 0 :(得分:1)
Read几乎可以为您完成所有工作:
while read time ampm comp status ver partition; do
echo at $time, $partition had status $comp $status
done < input-file
它将进入输入每一行的循环,将IFS第一个字符上的行拆分为命名变量。您所需要做的就是使用变量。