How to execute a command, if an option is selected in Bash

Viewed 21

I am learning bash script and made this script :

#!/bin/bash


PS3='What is your distro: '
options=("Fedora" "Ubuntu" "Arch" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            echo "you chose Fedora"
            ;;
        "Option 2")
            echo "you chose Ubuntu"
            ;;
        "Option 3")
            echo "you chose Arch $REPLY which is $opt"
            ;;
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

I want to execute a command if I select an option for example Fedora ==> Fedora.sh

I hope someone helps me.

1 Answers

It is a simple task.

Suppose you placed your scripts inside the same folder of the main script:

#!/bin/bash

SCRIPT_PATH=$(readlink -f $(dirname $0))

PS3='What is your distro: '
options=("Fedora" "Ubuntu" "Arch" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            echo "you chose Fedora"
            ${SCRIPT_PATH}/fedora.sh
            ;;
        "Option 2")
            echo "you chose Ubuntu"
            ${SCRIPT_PATH}/ubuntu.sh
            ;;
        "Option 3")
            echo "you chose Arch $REPLY which is $opt"
            ${SCRIPT_PATH}/arch.sh
            ;;
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

Here you can get a wonderfull BASH guide.

Related