junk_script_to_run is a file that was created by the first script. It contains the actual script that would do the work. It's duct tape and baling wire in the linux world.
I could have just made the first script include the second like so:
BLINK--> cat sample_script
#this script takes 3 inputs
# first input string to replace
# second string replacement string
# third filename
script_location=/tmp/junk_script_to_run
echo Preparing sed script...
echo "sed 's/$1/$2/g' $3"
echo "sed 's/$1/$2/g' $3" > $script_location
chmod 755 $script_location
echo "Script in $script_location will replace $1 with $2 in file: $3"
echo Run $script_location to launch this script.
echo "running the script now:"
$script_location
my sample file:
BLINK --> cat junk2
red green blue
red green orange
blue green green
green orange purple
when running my script:
BLINK --> ./sample_script green FOO junk2
Preparing sed script...
sed 's/green/FOO/g' junk2
Script in /tmp/junk_script_to_run will replace green with FOO in file: junk2
Run /tmp/junk_script_to_run to launch this script.
running the script now:
red FOO blue
red FOO orange
blue FOO FOO
FOO orange purple
As far as redirecting it back to itself that's just 2 more lines you had in your original script.