Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Jumping lines in bash script

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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 GNU/Linux bookworm/sid n l")    handleDebian() ;;
    "Ubuntu GNU/Linux bookworm/sid n l")    handleUbuntu() ;;
    "kali GNU/Linux bookworm/sid n l")      handleKali() ;;
    "Arch GNU/Linux bookworm/sid n l" )     handleArch() ;;
    *)  handleUnknown() ;;
esac
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading