I want to append things to a file in a way that uses sudo
if needed, here's what I have so far :
getAndAppend(){
# create file if doesn't exist, with right permission
[[ ! -s $2 ]] && touch "$2" || [[ ! -s $2 ]] && sudo touch "$2" # line 1
# append stuff to it
[[ -w $2 ]] && curl -sSL $1 >> $2 || sudo bash -c "curl -sSL $1 >> $2"
[[ -w $2 ]] && echo -e "\n" >> $2 || sudo bash -c "echo -e \"\n\""
}
file="~/.wot"
url="https://raw.github.com/n-marshall/system-setup/master/common/configs/.gitignore_global"
getAndAppend $url $file
However line 1 doesn't work in that the output will be something like ~/.wot: No such file or directory
Then of course the file won't exist and the following lines cannot work properly.
How could I fix that ? Thank you ! Any other comment or approach is welcome of course !