From: Sidney Lambe on
Ever want to try a little script to see if it will work
but don't want to go through the hassle of creating a file
with #!/bin/bash at the top and making it executable and
running it as ./script-name and then deleting it or moving
it?

You can use the fc command in bash, but it runs in the
current shell, which can cause real problems, and once
you've done it, if it is more than one line, or you do
anything else in between at the prompt, it's gone. fc
also echos the script, which is really ugly and confusing.

So I wrote this little script which has been tremendously
useful. It's aliased to "ws" in my system-wide bashrc file
in /etc.

alias ws='/usr/local/bin/write-script.sh'


#!/bin/bash

# /usr/local/bin/write-script.sh

while : ;
do

/bin/nvi /tmp/f-2ckdh93-z34444X

/bin/bash -l /tmp/f-2ckdh93-z34444X

echo
echo "[a] bring up script again"
echo "[b] delete script and exit"
echo "[c] don't delete script and exit"
echo "[d] name script and mv to new location (deleted in /tmp)"
read -s -n1 var
echo
case "$var" in
b) /usr/bin/rm -f /tmp/f-2ckdh93-z34444X ; exit 0 ;;
c) exit 0 ;;
d)
echo
echo "enter full path:"
read np
echo
echo -e "#!/bin/bash\n" >> "$np"
cat /tmp/f-2ckdh93-z34444X >> "$np"
chmod +rx "$np"
rm /tmp/f-2ckdh93-z34444X
exit 0
;;
*) continue ;;
esac

done


You enter "ws" and up comes a blank file in your editor.
(Unless you've left a script there deliberately.)
Write the script and write the file and exit the
editor and the script runs in a subshell. Then you see this;

[a] bring up script again
[b] delete script and exit
[c] don't delete script and exit
[d] name script and mv to new location (deleted in /tmp)

If you choose "d" you see this:

enter full path:
/usr/local/bin/write-script.sh # example

The script moves the file to your chosen location and names it as
specified and puts the #!/bin/bash at the top followed by
a blank line, adds your script below, and makes it executable.

Simple and very useful.

Sid