Append file based upon user input-- solved

Ok, I have a script with a commandline option that allows the user to add a custom function to the script file. I have tried everything in my limited knowledge of sed to get this to work and keep coming up short. I need sed to search for a line starting with a pattern, I've got that part so far, but now I need it to write to the line directly under it with an argument passed to the script. Right now i have several commandline switches being handled i want the -i switch to allow a user to add what comes next to the script. ex:

./myscript.sh -i test

should append 'test' to the file and save changes. Kind of like in javascript where you can .appendChild and such. There's a line that starts with foobar that i'd like sed to do this: find the line starting with foobar in myscript.sh and append test after that line. A subsequent 'cat' or 'vim' of that file should reflect test has been appended. I'll even use awk if that's a better answer. Find foobar in myscript.sh and add test after it and save the changes.

---------- Post updated at 12:43 AM ---------- Previous update was at 12:39 AM ----------

oh geez I should have just tried one more time.

sed -i blahblahbla

---------- Post updated at 03:41 AM ---------- Previous update was at 12:43 AM ----------

ok i've got sed working almost as needed. In my script i have two occurrences of "#CUSTOM", once in the sed command and once as a comment. I need sed to edit the script that its in and do this:

sed -i '/#CUSTOM/ a\ $aVariable' /path/to/sed/script

But not append it after where sed is locate. Let's say where I want it to append to is line 50, or even just say "the second occurrence".Basically I'm letting the user edit the calling script and add a directory I get from them which i want to become $aVariable. Please help I've been messing with sed all night now. It's either given me a line after both or when i tried some example i saw online it appended it after about 50 lines.

---------- Post updated at 03:54 AM ---------- Previous update was at 03:41 AM ----------

ok fixed the occurrence issue with

^

before #CUSTOM. I still can't get variables working. Anyone?

---------- Post updated at 04:07 AM ---------- Previous update was at 03:54 AM ----------

nvm fixed it on my own...lol...forgot about the stupid single quotes variable expansion. use " "