Home > Net >  Jumping lines in bash script
Jumping lines in bash script

Time:10-27

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

CodePudding user response:

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

CodePudding user response:

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
  • Related