-1

what would goto, translate to in bash script


This is my code:

#!/bin/bash
read distro </etc/issue
echo $distro
if [[ "$distro" = "Debian GNU/Linux bookworm/sid n l" ]];
then
    goto Debian:
elif [[ "$distro" = "Ubuntu GNU/Linux bookworm/sid n l" ]];
then
    goto Ubuntu:
elif [[ "$distro" = "kali GNU/Linux bookworm/sid n l" ]];
then
    goto Kali:
elif [[ "$distro" = "Arch GNU/Linux bookworm/sid n l" ]];
then
    goto Arch:
else
    echo No suported OS detected some thing may not work
fi

Debian:
echo using Debian
sleep 5
exit

Ubuntu:
echo using Ubuntu
sleep 5
exit

Kali:
echo using kali
sleep 5
exit

Arch:
echo using Arch
sleep 5
exit

Its a really simple code and I don't even know if the way I'm checking the Linux distro will work

I have tried with the Goto function from a batch from windows but it wont work on Linux, how would i jump form line to line

  • 1
    You could define functions and call those if needed. Also use `elif` so you don't need the 'goto check4' – 0stone0 Oct 26 '22 at 15:41

2 Answers2

0

I'd recommend using a Switch-Case statement with fall-through.

All the 'handlers' can be defined in a function that will be called from the switch:

#!/bin/bash

handleDebian() {
    echo "Using Debian"
}

handleUbuntu() {
    echo "Using Ubuntu"
}

handleKali() {
    echo "Using Kali"
}

handleArch() {
    echo "Using Arch"
}

handleUnknown() {
   echo "No suported OS detected some thing may not work"
}

read distro </etc/issue
case "${distro}" in
    *Debian*)   handleDebian() ;;
    *Ubuntu*)   handleUbuntu() ;;
    *kali*)     handleKali() ;;
    *Arch*)     handleArch() ;;
    *)          handleUnkown() ;;
esac
0stone0
  • 34,288
  • 4
  • 39
  • 64
0

In this case I'd opt for a more compact case statement, eg:

read distro </etc/issue
echo $distro

case "${distro}" in
    *Debian*)    echo "Debian" ;;
    *Ubuntu*)    echo "Ubuntu" ;;
    *kali*)      echo "kali"   ;;
    *Arch*)      echo "Arch"   ;;
    *)           echo "No suported OS detected some thing may not work";;
esac

sleep 5
markp-fuso
  • 28,790
  • 4
  • 16
  • 36