I am working on a bash script that needs to take a single line and add it to the end of a file if it exists, and if it does not exist create the file with the line.
I have so far:
if [ ! -e /path/to/file ]; then
echo $some_line > /path/to/file
else
???
fi
How do I perform the operation that should go in the else (adding the line of text to the existing file)?
Use two angles: echo $some_line >> /path/to/file
> creates the file if it doesn't exist; if it exists, overwrites it.
>> creates the file if it doesn't exist; if it exists, appends to it.
if [ ! -e /path/to/file ]; then
echo $some_line > /path/to/file
else
echo $some_line >> /path/to/file
fi
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