bash goto code example
Example 1: bash jump to
#!/bin/bash
function jumpto
{
label=$1
cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$')
eval "$cmd"
exit
}
start=${1:-"start"}
jumpto $start
start:
x=100
jumpto foo
mid:
x=101
echo "This is not printed!"
foo:
x=${x:-10}
echo x is $x
Example 2: bash jump to
#!/bin/bash
read -p 'Input something > ' one_thing
while true; do
read -p 'Input something else > ' another_thing
if [[ "${thing_to_work}" == 'abcde' ]]; then
break
else
echo 'The requirements were not met, so the loop will start again'
fi
done
Example 3: bash jump to
while :
do
clear
echo -n 'Questions? '
read x
switch $x in
a)
echo a
;;
b|c)
echo b or c
;;
q)
break
;;
*)
echo Invalid command \"$x\"
;;
esac
done