jackieeeeeeeeeee
jackieeeeeeeeeee

Reputation: 25

Bash if $? ignored

I'm making a postinstall script, but for some reason my menu doesnt work with if comands.

Its supposed to echo whatever editor i selected (Well, for now), but only echos emacs. Help?

(Code below)

#!/bin/bash
if [ $(whoami) != root ]
then
    echo "Sorry, this script must be run as root. Try sudo su, and then running it!"
    exit 1
fi

which dialog > /dev/null

if [ $? = 1 ]
then
    echo "Sorry, you need to install dialog first."
    exit 1
fi

choice=$(dialog --no-cancel --backtitle "Jacks Post Install Script" --title "Welcome" --menu "" 20 40 35 1 "Install crap already!" 2 "Enable ssh" 3 "Reboot" 4 "Exit" 3>&1 1>&2 2>&3 3>&-)

function insstp1 {
    dialog --cancel-label "No thanks" --extra-button --extra-label "Vim" --ok-label "Emacs" --backtitle "Jacks Post Install Script" --title "Pick an editor!" --yesno "I bet you will pick emacs. Seriusly." 5 40
    echo $? #emacs 0 vim 3 no 1
    if [ $? == 0 ]
    then
    echo "Emacs"
    fi

    if [ $? == 3 ]
    then
        echo "Vim"
    fi

    if [ $? == 1 ]
    then
    echo "nope"
    fi
}

case $choice in
    1) insstp1 ;;
    2) enablssh ;;
    3) reboot ;;
    4) clear; exit 0 ;;
esac

Upvotes: 0

Views: 108

Answers (2)

Walter A
Walter A

Reputation: 20022

Store $? und use a case construction. You are trying to store the result of your first dialog in a variable choice, but it will contain the stdout. This choice should be done with $? as well:

    function insstp1 {
        dialog --cancel-label "No thanks" --extra-button --extra-label "Vim" --ok-label "Emacs" --backtitle "Jacks Post Install Script" --title "Pick an editor!" --yesno "I bet you will pick emacs. Seriusly." 5 40
        editchoice=$? #emacs 0 vim 3 no 1
        case ${editchoice} in
           0) echo "Emacs" ;;
           3) echo "Vim" ;;
           1) echo "nope";;
           *) echo "Let me pick for you ...";;
        fi
    }

dialog --no-cancel --backtitle "Jacks Post Install Script" --title "Welcome" --menu "" 20 40 35 1 "Install crap already!" 2 "Enable ssh" 3 "Reboot" 4 "Exit" 3>&1 1>&2 2>&3 3>&-
choice=$?

case ${choice} in
    1) insstp1 ;;
    2) enablssh ;;
    3) reboot ;;
    4) clear; exit 0 ;;
esac

Upvotes: 0

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14955

Wrong syntax == should be -eq and of course avoid the echoes.

dialog --cancel-label "No thanks" --extra-button --extra-label "Vim" --ok-label "Emacs" --backtitle "Jacks Post Install Script" --title "Pick an editor!" --yesno "I bet you will pick emacs. Seriusly." 5 40
choice2=$?
        
if [ $choice2 -eq 0 ]; then
   echo "Emacs"
fi

From the docs:

arg1 OP arg2

OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.

Upvotes: 2

Related Questions