将文件内容附加到 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
Append file contents to the bottom of existing file in Bash
提问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?
I'm trying to work out the best way to insert api details into a pre-existing config. I thought about using sed
to 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).
您需要使用>>
运算符附加到文件。重定向>
会导致文件被覆盖。(截断)。