might help to understand what it is you really mean by "what is not right"? A better question would be .... After I run the script my text file is empty or it gives me such and such results and I am looking for .... or it does nothing .... help us understand what your real problem is
I see several different potential problems .....
how do you call this script? (it is expecting a file name ... but the "cat text" always displays the file "text" )
the $1 does not necessarly have to be the file text
if you were to call this script without a file name (or with the file name of text ... you would indeed end up with an empty "text" file.
here is what I would do
change all references of "text" to "$1"
change the sed command to
cat $1 | sed s/'$old'/'$new'/g > $1
The way you wrote the sed empties the text file first then tries to put the now empty file back into itself. The cat/sed combination will empty the text file after it has done the cat.
cat $1 | sed s/'$old'/'$new'/g > $1
Need to direct to a tmp file and then move it back (is there a better way?)
cat $1 | sed s/'$old'/'$new'/g > $1.$$tmp
mv $1.$$tmp $1
If I remember correctly older versions of bash doesnt support reading a file and redirecting output to the same on the same command line. I use version 2.xxx at work and it doesnt work there.
On ksh, I seem to remember that it also depends on your version- what version are you using?
I suggest you go with Klashxx's perl pie solution, but adapted so that you use a separator char which isnt so usual in filenames:
I agree the perl route is better ... it also works from within a shell as well. I was just trying to answer the original question, there may be other reasons as to why they need it to be part of a script. (like a class assignment)