Dont you mind
Dont you mind

Reputation: 43

How to loop case statement in a whiptail

#!/bin/bash
function advancedMenu() {
    ADVSEL=$(whiptail --title "Advanced Menu" --fb --menu "Choose an option" 15 60 4 \
    "1" "Delete group" \
    "2" "Create login name" \
    "3" "Exit" 3>&1 1>&2 2>&3)
    case "$ADVSEL" in
    1)
    echo "Option 1"
    whiptail --title "Option 1" --msgbox "You chose group deleting. Exit status $?" 8 45
    bash ./script3;;
    2)
    echo "Option 2"
    whiptail --title "Option 1" --msgbox "You chose login name creating. Exit status $?" 8 45
    bash ./script;;
    3)
    echo "Option 3"
    whiptail --title "Option 1" --msgbox "You chose exit. Exit status $?" 8 45
    ;;
    esac
}

advancedMenu

I don't know how to loop my case statement. I have user friendly interface here. Someone chooses 1\2 or exit. 1 and 2 runs another scripts. So when 1 is clicked and scripted is finished, I need the main menu back until case 3 is clicked. I will be grateful for your help!


#!/bin/bash
export NEWT_COLORS='
window=white,blue
border=white,green
textbox=white,green
button=black,white
'

{
    for ((i = 0 ; i <= 100 ; i += 1)); do
    sleep 1
    echo $i
    done

} | whiptail --gauge "Wait..." 6 60 0

touch 35_2.csv
while read line; do
IFS=","
set -- $line
group_name=$1
student_name=$2
student_name=`echo $student_name | tr -d '\r\n' `
login_name=`echo $student_name | sed 's/[b\`]//g;'`
login_name=`echo $login_name | sed 'y/абвгдеєзиіїйклмнопрстуфхь/abvgdeeziiijklmnoprstufh_/'`
login_name=`echo $login_name | sed 's/ /_/g; s/С†/ts/g; s/Р¶/zh/g; s/С‡/ch/g; s/С€/sh/g; s/С‰/sh/g; s/СЋ/yu/g; s/СЏ/ya/;'`
echo "$group_name, $student_name, $login_name" >> 35_2.csv
done < 35_2.csv

Don't look at wrong encoding. Your decision seemed to be right. The first script runs then menu goes back. However, when I start the second script the menu does not go back. Attached the 2nd's script code.

Upvotes: 1

Views: 1325

Answers (1)

Barmar
Barmar

Reputation: 781096

Simply put a while loop around it.

#!/bin/bash
function advancedMenu() {
    while :; do
        ADVSEL=$(whiptail --title "Advanced Menu" --fb --menu "Choose an option" 15 60 4 \

            "1" "Delete group" \
            "2" "Create login name" \
            "3" "Exit" 3>&1 1>&2 2>&3)
        case "$ADVSEL" in
        1)
            echo "Option 1"
            whiptail --title "Option 1" --msgbox "You chose group deleting. Exit status $?" 8 45
            bash ./script3;;
        2)
            echo "Option 2"
            whiptail --title "Option 1" --msgbox "You chose login name creating. Exit status $?" 8 45
            bash ./script;;
        3)
            echo "Option 3"
            whiptail --title "Option 1" --msgbox "You chose exit. Exit status $?" 8 45
            return
            ;;
        esac
    done
}
advancedMenu

I'm not sure what you expect Exit status $? to show. $? is the exit status of the echo statement on the previous line, so it will almost always just be 0.

Upvotes: 1

Related Questions