Update: As it's a bash question, you should try this first. ;)
cat <<':q' >> test.file
To understand what is going on, read about bash's IO redirection, heredoc syntax and the cat command
As you see above, there are many ways to do it. To explain some more bash commands I've prepared the function also in the way you've requested it:
#!/bin/bash
write_to_file()
{
# initialize a local var
local file="test.file"
# check if file exists. this is not required as echo >> would
# would create it any way. but for this example I've added it for you
# -f checks if a file exists. The ! operator negates the result
if [ ! -f "$file" ] ; then
# if not create the file
touch "$file"
fi
# "open the file to edit" ... not required. echo will do
# go in a while loop
while true ; do
# ask input from user. read will store the
# line buffered user input in the var $user_input
# line buffered means that read returns if the user
# presses return
read user_input
# until user types ":q" ... using the == operator
if [ "$user_input" = ":q" ] ; then
return # return from function
fi
# write to the end of the file. if the file
# not already exists it will be created
echo "$user_input" >> "$file"
done
}
# execute it
write_to_file
man bash
. – paddy Jan 31 '13 at 23:50man bash
is more of a reference manual, and a bit challenging for beginners. I think Fraz needs a tutorial. Try tldp.org/LDP/abs/html. – Randall Cook Jan 31 '13 at 23:52!abs
factoid in Freenode's#bash
channel: The infamous "Advanced" Bash Scripting Guide should be avoided unless you know how to filter out the junk. It will teach you to write bugs, not scripts. In that light, the BashGuide was written: mywiki.wooledge.org/BashGuide – Charles Duffy Jan 31 '13 at 23:59