加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 来自文件行的回显

发布时间:2020-12-15 22:36:14 所属栏目:安全 来源:网络整理
导读:我有一个文件“myfile.txt”,其中包含下一个内容: hola mundohello word 我想要与每一行合作 for i in `cat myfile.txt`; do echo $i; done 我希望这能给我 hola mundohello word 先是一条线,然后另一条线,但是得到 holamundohelloword 因为我可以要求结果
我有一个文件“myfile.txt”,其中包含下一个内容:

hola mundo
hello word

我想要与每一行合作

for i in `cat myfile.txt`; do echo $i; done

我希望这能给我

hola mundo
hello word

先是一条线,然后另一条线,但是得到

hola
mundo
hello
word

因为我可以要求结果直到换行而不是每个空格?

所有

解决方法

那更好

cat myfile.txt | while read line; do
    echo "$line"
done

甚至更好(不启动其他过程,如子壳和猫):

while read line; do
    echo "$line"
done < myfile.txt

如果您更喜欢oneliners,那很明显

while read line; do echo "$line"; done < myfile.txt

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读