将文件内容附加到 Bash 中现有文件的底部 [英] Append file contents to the bottom of existing file in Bash

查看:19
本文介绍了将文件内容附加到 Bash 中现有文件的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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.

#!/bin/bash

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

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

我做错了什么?

推荐答案

这应该有效:

 cat "$API" >> "$CONFIG"

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

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

这篇关于将文件内容附加到 Bash 中现有文件的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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