[SH] Problem reading input in script

Alright, so the goal of my script is to read text from standard input and store it into a file using the ex-editor:

so far i've got this, but it doesn't work.

#!/bin/s
read text
ex $1 >> HERE
text
HERE

I don't get any errors either, so i don't know what i'm doing wrong.

I think you've got your brackets backwards. >> tells it to append to the HERE file, not read from a here document. And since it never reads the here document, it just hangs reading on STDIN instead. (For future reference, "hangs" is a much more useful description than "doesn't work", which could mean nearly anything...)

If it ever got beyond that it'd consider 'text' and 'HERE' on subsequent lines to be syntax errors.

So, nearly right. Try:

#!/bin/s
read text
ex $1 <<HERE
text
HERE

nope, it says: text is not an editor commando

btw now i got this:

#!/bin/sh
read text
ex $1 <<HERE
$text
HERE

Well, what are you typing into the script? How are you running it? Maybe it isn't an editor command.

alright, so i've come already pretty far.

So far i've got this

#!/bin/sh
read text
ex $1 <<'HERE'
a
text
.
wq
'HERE'

The only thing that has to be done now is that the text given by the users should be put in the file. But ex just puts the word 'text' in it instead of the text given by the user.

This should do it:

#!/bin/sh
read text
ex $1 <<'HERE'
a
$text
.
wq
'HERE'

You could get the same results with:

read "text"
echo "$text" > file

using read won't work if there are newlines since read quits when one is entered. A "here" document is better for working with existing files.

Bill

OP wanted to append to existing file so:

read "text"
echo "$text" >> file

is probably closer, however the original post was also clear about using ex editor to write to the file.