tags:

views:

98

answers:

2

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)?

+6  A: 

Use two angles: echo $some_line >> /path/to/file

John Millikin
Classic - always good to remember this!
Preet Sangha
A: 

with > this creates file if it doesn't exist. If it exits, overwrites

with >> this creates file if it doesn't exist. If it exits, appends to the file

if [ ! -e /path/to/file ]; then
   echo $some_line > /path/to/file
else
   echo $some_line >> /path/to/file
fi
Firstthumb
use just echo $some_line >> /path/to/file will be sufficient since >> creates the file if it doesn't exist
ghostdog74
Yes you are right. I just gave the sample to Mark Roddy as he did.
Firstthumb