将文件内容附加到 Bash 中现有文件的底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13181725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:54:11  来源:igfitidea点击:

Append file contents to the bottom of existing file in Bash

bashsedawkappend

提问by Grimlockz

Possible Duplicate:
Shell script to append text to each file?
How to append output to the end of text file in SHELL Script?

可能的重复:
将文本附加到每个文件的 Shell 脚本?
如何在 SHELL Script 中将输出附加到文本文件的末尾?

I'm trying to work out the best way to insert api details into a pre-existing config. I thought about using sedto insert the contents of the api text file to the bottom of the config.inc file. I've started the script but it doesn't work and it wipes the file.

我正在尝试找出将 api 详细信息插入到预先存在的配置中的最佳方法。我想过使用sed将api文本文件的内容插入到config.inc文件的底部。我已经启动了脚本,但它不起作用,它会擦除​​文件。

#!/bin/bash

CONFIG=/home/user/config.inc
API=/home/user/api.txt

sed -e "$a $API" > $CONFIG

What am I doing wrong?

我究竟做错了什么?

回答by William Pursell

This should work:

这应该有效:

 cat "$API" >> "$CONFIG"

You need to use the >>operator to append to a file. Redirecting with >causes the file to be overwritten. (truncated).

您需要使用>>运算符附加到文件。重定向>会导致文件被覆盖。(截断)。