使用 Bash 打开并将数据写入文本文件? [英] Open and write data to text file using Bash?

查看:31
本文介绍了使用 Bash 打开并将数据写入文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Linux 中通过 shell 脚本自动将数据写入文本文件?

How can I write data to a text file automatically by shell scripting in Linux?

我能够打开文件.但是,我不知道如何向其写入数据.

I was able to open the file. However, I don't know how to write data to it.

推荐答案

简短的回答:

echo "some data for the file" >> fileName

但是,echo 在理想情况下不会处理行尾字符 (EOF)方式.所以,如果你要追加不止一行,用 printf 来做:

However, echo doesn't deal with end of line characters (EOFs) in an ideal way. So, if you're gonna append more than one line, do it with printf:

printf "some data for the file
And a new line" >> fileName

>>> 运算符对于重定向命令的输出非常有用,它们可以与多个其他 bash 命令一起使用.

The >> and > operators are very useful for redirecting output of commands, they work with multiple other bash commands.

这篇关于使用 Bash 打开并将数据写入文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆