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:
Code:
#!/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:
Code:
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.
Last edited by wisecracker; 09-19-2019 at 10:42 AM..
These 2 Users Gave Thanks to wisecracker For This Post:
Eventually swap the two lines in the loop, to omit the first line (then you do not need to prefix the lable with a nop).
Of course this is a hack. A function puts something on the "return" stack...
I will replace my version with your function and give it a whirl.
I had in fact thought about using #some_label: as both methods would not create an error, something like command not found under normal running but I see your point.
And of course it's a hack, as I always quote "if there is a back door I will find it" and hacks by their nature are back doors....
I have found many source files in the kernel using goto keyword instead of just doing the actual thing. For example:
if(blah)
goto x;
--
--
--
--
--
x: return blah-blah
Is there any specific reason for writing the code like this? The first thought that came to my mind is minimizing... (0 Replies)
#!/bin/sh
label:
echo sql poll
v=`sqlplus -s <<!
HR/HR
set pages 0 echo off feed off
select distinct status from
emp
where
id=5;
!
`
echo $v;
echo it comes here after false
if
then
echo if condition true
sqlplus -l scott/tiger <<EOF
select * from department;
EXIT (2 Replies)
Im trying to do something like this but I cant find any documentation.
read X
if
then goto ThisLine
fi
OTHER CODE
OTHER CODE
Label: ThisLine echo "You entered 1" (5 Replies)
Dear All,
Can any one pls let me the command for how to goback to previous visited directory from the current working directory in SunOS ?
In case of HP-UX; the same can be resolved through "cd -" command.
Thanks in advance!
Prasanth Babu. (6 Replies)
Hi!
I'm new in these forums and more or less new with Unix. So... here is the question:
does anyone know where is redirected the output of a command when you put >&- after it?
Does it means any standard file descriptor?
Thanks! (2 Replies)
uname -a returns: SMP Tue May 17 17:52:23 EDT 2005 i686 athlon i386 GNU/Linux
I have many aliases beginning with "goto" so...
if I type goto and then hit return (oops)
A goto prompt pops up and I cant exit from it(I tried MANY key seqs)
The only way to exit is to kill the term window... (2 Replies)
I have a test script for using goto statement but its not working. please help
i tried both in linux and hp-ux it's not working please help
#! /bin/ksh
t=`ps -ef|grep ti.sh|grep -v grep`
if ; then
goto start
else
goto stop
fi
start:
echo "start"
stop:
echo "stop" (5 Replies)
Hey Guys.. I just want to know how to use Goto statement in shell scripts.
I know the basic use of statement.
Goto Label
The above statement will search for some label which must be defined in the script itself as:
label:
I tried these combinations but I didn't work out for me and I'm... (7 Replies)
Hi,
I'm trying to use the goto in ksh but it does not appear to be a valid command. Is that only valid in csh? Anything similar in ksh that I can use?
Appreciate any help you can provide.
Thanks.
geraldine (4 Replies)