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.
#!/bin/bash CONFIG=/home/user/config.inc API=/home/user/api.txt sed -e "\$a $API" > $CONFIG
What am I doing wrong?
To make a new file in Bash, you normally use > for redirection, but to append to an existing file, you would use >> . Take a look at the examples below to see how it works. To append some text to the end of a file, you can use echo and redirect the output to be appended to a file.
You can use cat with redirection to append a file to another file. You do this by using the append redirection symbol, ``>>''. To append one file to the end of another, type cat, the file you want to append, then >>, then the file you want to append to, and press <Enter>.
The “>>” operator is used to append text to the end of a file that already has content.
Append Text Using >> Operator The >> operator redirects output to a file, if the file doesn't exist, it is created but if it exists, the output will be appended at the end of the file. For example, you can use the echo command to append the text to the end of the file as shown.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With