Search code examples
bashshelldialogforkinteraction

shell/dialog - Close dialog without user interaction


Simply put, I am using Dialog in BASH to generate messages and have an interactive menu. For a step though, I need to wait for the user to plug in a hardware device, so I run a "tailbox" with a function echoing into it.

Right now I have it just echo out "Please exit dialog box" but for streamlining purposes, I'd like to close the dialog box on its own. I've added a timeout already, but it's to make sure users don't get stuck on that screen, so it's very long.

Is there a way to make it so when the script ends the dialog box sleeps for 2-3 seconds then closes?

Any help is greatly appreciated!

function initRPI { # Wait for carrier Board to be plugged in, then initialize CM Unit
    echo -e "Plug in carrier board with compute module attached.\n"
    pkill "rpiboot"
    sleep 0.5
    $FILE # Run rpiboot from where it's installed
    echo -e "\nCompute Module Initialized - Exit Now."
}

function writeImage { # Find proper image and write it to device
    if [[ " ${boxTypes[*]} " =~ "$boxType" ]]; then
        initRPI > _temp &
        dialog --backtitle "$backTitle" --fb --title "Image Writer" --timeout 60 --tailbox _temp 15 70
        if pgrep -f rpiboot &> /dev/null 2>&1; then
            pkill "rpiboot"
            dialogMsg FAILED "Compute Module was not initialized."
        else
            devCM="/dev/sda"
            if [ ! -d "/mnt/firmware" ]; then
                mkdir /mnt/firmware
            fi
            mount -t nfs $nasIP/firmware /mnt/firmware
            (pv -n "/mnt/firmware/${unitSerial:2:4}.img" | dd of="$devCM" bs=4M conv=notrunc,noerror) 2>&1 | dialog --gauge "Running cloning $imageName to device $1, please wait..." 10 70 0
            sleep 0.5
            if kill -0 "$pid" ; then
                dialogMsg SUCCESS "Image $imageName written to device $1."
            else
                dialogMsg FAILED "Image $imageName failed to write to device $1."
            fi
        fi
    else
        dialogMsg ERROR "Can't find $boxType in the model list."
    fi
}

I have it forking the rpiboot process so the tail just reads a temp variable, but I have to wait for user input to exit when the rpiboot completes.

EDIT: I realized I put the --timeout inside the --tailbox arguments, so just moved it real quick.


Solution

  • I figured it out. I can basically just remove the EXIT button and force kill the dialog window with pkill "dialog".

    Hope this helps someone!