为什么空行没有存储在变量中 [英] why empty line is not getting stored in variable
本文介绍了为什么空行没有存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码
#! /bin/bash
for host in $(cat ./server.txt)
do
echo "$host"
done
server.txt包含:
server.txt contains :
server1.com
server2.com
server3.com
server4.com
但是上面的代码给出了以下输出:
But the above code is giving below output:
server1.com
server2.com
server3.com
server4.com
即它没有考虑空行.我该如何解决?
i.e its not taking account of empty lines.How can I fix that?
推荐答案
while read host; do echo "$host"; done < server.txt
使用您提供的代码,将文件作为单个字符串作为for
命令的第4个参数给出.然后,shell使用任意空格作为分隔符来分隔字符串 (假设您尚未更改IFS
变量).
With the code you provided, the file as a single string is given as the 4th argument to the for
command. The shell then splits the string using arbitrary whitespace as a delimiter (assuming you have not altered the IFS
variable).
这篇关于为什么空行没有存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文