As far as your script goes, you can use >
redirection to file. You're asking if the file doesn't exist (which should better be expressed as if ! [ -e ~/.conf/file/path ];
), therefore there is no need to use append
operator in this particular case (even though it works alright).
As for printing long multiline configuration into the file, Panther already hinted at use of here docs in his comment under Thomas's answer. This is a frequent technique I personally use for writing "help message" or options information in my scripts
You could easily do something like this:
bash-4.3$ cat <<EOF > out.txt
parameter1="value 1"
parameter2="value 2"
EOF
bash-4.3$ cat out.txt
parameter1="value 1"
parameter2="value 2"
bash-4.3$
Or alternatively create a specific function, write_config()
to make your script more idiomatic, so to speak:
#!/usr/bin/env bash
# note usage of $HOME variable instead of ~
my_config="$HOME/my_conf_file.txt"
# declare all functions at the top
config_exists()[[ -e "$my_config" ]]
write_config(){
cat > "$my_config" <<EOF
parameter1="value 1"
parameter2="varlue 2"
EOF
}
if ! config_exists;
then
echo "config doesn't exist"
write_config
fi