[BASH] Using a function to write data to a file

Hello,

I've created a shell script, which accepts information using an input from the console. Part of the script will write a file containing this information.

My code looks like (for the write)

function make_file
{
cat <<- _EOF_
The contents of my file are here
_EOF_
}

make_file >> /home/cpickering/$file.conf

However, when I run the script, the other sections work, just not the writing of the file.

The contents of $file are collected from my users input..

The script snippet you posted works just fine.

If you have older shell, then

make_file()
{
cat <<- _EOF_
The contents of my file are here
_EOF_
}

make_file >> /home/cpickering/$file.conf

Which shell you have used ? You said in subject that bash, but are you sure that you used bash ?

bash thisscript
ksh thisscript
dash thisscript
...
Insert the needed shell to first line, example

#!/bin/bash
...

I certainly am using bash, the first line reads

#!/bin/bash -x

---------- Post updated at 02:59 PM ---------- Previous update was at 02:54 PM ----------

Having changed my function to have the () in, I am now faced with the following error

It's look that you have not done cut&paste for us. Error is not in those lines ...

Maybe error is pairs " ", ' ', ``, ... end pair is missing ?

Line 224, is

# build vhost config file
make_vhost >> /home/cpickering/$dname.conf
echo "created $dname server config"
# Exit the script

Yes, we have tested it, that line is okay. Error is not in those lines, which you have posted.
My previous message means whole script.

As this looks like an unreproducible error, can you post the result of this command:

sed -n 224p createVhost.sh | od -c

I suspect an odd unprintable character to confuse the shell.

Looks like syntax on the "here" document.

cat <<- _EOF_

Should be

cat <<_EOF_

I'm with kshji the script most likely has an unmatched quoted pair. The easy way to find this is to download an editor that understands shell script. I use 'jedit' on my pc and copy any scripts I'm having issue with and open them in jedit. The editor will change all the text to PINK between quotes, so if your missing one it is obvious where you need to insert one to correct the issue.

Why ?
"<<-" is a valid syntax for a here-document although the "-" is unnecessary in that specific sample.

if I might add, both methyl and jlliagre are right-ish on the matter of the syntax...but kshji is most right in suggesting a bum char is gumming up the works...

The "<<- _EOF_" is very much usable (as well as not necessary...), so long as the shell supports it and the whitespace is in fact a space (hex:20). A tab char will not do, and actually causes the function to hang altogether, so an od -c would be most helpful in sorting out the root cause of the failure. Otherwise, manually re-create what you want to happen and then copy/paste it from whatever history file you're logging to...

My .02...

---------- Post updated at 20:39 ---------- Previous update was at 20:38 ----------

actually, it was jlliagre who'd suggested the octal dump...my bad!

---------- Post updated at 20:54 ---------- Previous update was at 20:39 ----------

can't leave well-enough alone...

$ tail -16 file_test.txt

cat <<- _EOF_
blah...[space]
_EOF_

cat <<- _EOF_
blah...[tab]-less
_EOF_

cat <<- _EOF_
blah...[tab]-more
        _EOF_

cat <<- _EOF_
blah...[space]-tab
        _EOF_

$ tail -16 file_test.txt |od -c
0000000  \r  \n   c   a   t       <   <   -       _   E   O   F   _  \r
0000020  \n   b   l   a   h   .   .   .   [   s   p   a   c   e   ]  \r
0000040  \n   _   E   O   F   _  \r  \n  \r  \n   c   a   t       <   <
0000060   -  \t   _   E   O   F   _  \r  \n   b   l   a   h   .   .   .
0000100   [   t   a   b   ]   -   l   e   s   s  \r  \n   _   E   O   F
0000120   _  \r  \n  \r  \n   c   a   t       <   <   -  \t   _   E   O
0000140   F   _  \r  \n   b   l   a   h   .   .   .   [   t   a   b   ]
0000160   -   m   o   r   e  \r  \n  \t   _   E   O   F   _  \r  \n  \r
0000200  \n   c   a   t       <   <   -       _   E   O   F   _  \r  \n
0000220   b   l   a   h   .   .   .   [   s   p   a   c   e   ]   -   t
0000240   a   b  \r  \n  \t   _   E   O   F   _  \r  \n
0000254

Add the following to your sh script debugging toolkit. It can be extremely useful.

set -x