如何在shell脚本/ bash中将输出追加到文本文件的末尾?
如何将命令的输出追加到文本文件的末尾。
将输出引导到文件时使用>>而不是> 
 your_command >> file_to_append_to 
 如果file_to_append_to不存在,它将被创build。 
例:
 $ echo "hello" > file $ echo "world" >> file $ cat file hello world 
您可以使用>>运算符。 这会将数据从命令附加到文本文件的末尾。
要testing这个尝试运行:
 echo "Hi this is a test" >> textfile.txt 
做这个几次,然后运行:
 cat textfile.txt 
你会看到你的文本已经附加了几次textfile.txt文件。
 使用>>操作符将文本附加到文件。 
对于整个问题:
 cmd >> o.txt && [[ $(wc -l <o.txt) -eq 720 ]] && mv o.txt $(date +%F).o.txt 
这会将720行(30 * 24)添加到o.txt中,之后将根据当前date重命名该文件。
用cron每小时运行一次,或者
 while : do cmd >> o.txt && [[ $(wc -l <o.txt) -eq 720 ]] && mv o.txt $(date +%F).o.txt sleep 3600 done 
例如你的文件包含:
  1. mangesh@001:~$ cat output.txt 1 2 EOF 
如果你想在文件末尾追加—->记住'text'>>'filename'之间的空格
  2. mangesh@001:~$ echo somthing to append >> output.txt|cat output.txt 1 2 EOF somthing to append 
并覆盖文件的内容:
  3. mangesh@001:~$ echo 'somthing new to write' > output.tx|cat output.tx somthing new to write 
 使用command >> file_to_append_to追加到文件。 
 例如echo "Hello" >> testFile.txt 
  小心:如果您只使用单个文件,您将完全覆盖文件的内容。 为了确保不会发生,您可以将set -o noclobber添加到您的.bashrc 。 
 这可以确保,如果您不小心将command > file_to_append_to键入到现有文件,它会提醒您文件已经存在。 示例错误消息: file exists: testFile.txt 
 因此,当您使用>它将只允许您创build一个新的文件,而不是覆盖现有的文件。 
我build议你做两件事情:
-  在shell脚本中使用>>将内容追加到特定文件。 文件名可以是固定的或使用某种模式。
- 设置一个小时的cronjob触发shell脚本