Well, guys I saw a question about GOTO for Python.
So this gave me the inspiration to attempt a GOTO function for ' dash
', ( bash and ksh
too).
Machine: MBP OSX 10.14.3, default bash terminal, calling ' #!/usr/local/bin/dash
'...
This is purely a fun project to see if it is possible in PURE shell scripting, and so it is...
I now consider myself competent at UNIX shell scripting...
Enjoy ripping it apart:
#!/usr/local/bin/dash
# goto_demo.sh
# goto ': some_label:'
goto()
{
line_number=1
start_line=1
while IFS='' read -r line
do
if [ "${line}" = "${1}" ]
then
: > /tmp/goto.tmp
start_line=${line_number}
fi
if [ ${line_number} -ge ${start_line} ]
then
echo "${line}" >> /tmp/goto.tmp
fi
line_number=$(( line_number + 1 ))
done
. /tmp/goto.tmp
} < ${0}
goto ': second:'
echo 'This will not be printed!'
: first:
echo '**************************************'
echo 'You are at first label!'
# cat /tmp/goto.tmp
echo '**************************************'
goto ': third:'
echo 'This will not be printed either!'
: second:
echo '**************************************'
echo 'You are at the second label!'
# cat /tmp/goto.tmp
echo '**************************************'
goto ': first:'
echo 'And finally, this will not be printed either!'
: third:
echo '**************************************'
echo 'You are at the third label!'
echo '######## cat /tmp/goto.tmp... ########'
cat /tmp/goto.tmp
echo '###### END cat /tmp/goto.tmp... ######'
echo '**************************************'
: end:
echo 'Exiting...'
echo '**************************************'
exit
Results using above:
Last login: Wed Sep 18 15:55:17 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./goto_demo.sh
**************************************
You are at the second label!
**************************************
**************************************
You are at first label!
**************************************
**************************************
You are at the third label!
######## cat /tmp/goto.tmp... ########
: third:
echo '**************************************'
echo 'You are at the third label!'
echo '######## cat /tmp/goto.tmp... ########'
cat /tmp/goto.tmp
echo '###### END cat /tmp/goto.tmp... ######'
echo '**************************************'
: end:
echo 'Exiting...'
echo '**************************************'
exit
###### END cat /tmp/goto.tmp... ######
**************************************
Exiting...
**************************************
AMIGA:amiga~/Desktop/Code/Shell> _
EDIT:
Tested on current Linux Mint 19, using the shebang #!/bin/sh
and working as per the image.