终端打开然后立即关闭

时间:2013-02-20 22:08:22

标签: bash

所以我有以下代码(W.I.P),当我尝试执行它时,终端立即打开并再次关闭。我不太确定是什么导致它所以如果有人能帮助我,我会很感激。代码如下:

#!/bin/bash

#displays a menu with options for user to select
showMenu () 

{

zenity --list \
--title="Menu - Choose an option" \
--column="Option No." --column="Option" \
--height="300" --width="475" \
1 "Install Gnome Disk Utility & gParted" \
2 "Create File - Demo.txt" \
3 "Remove File - Demo.txt" \
4 "Search for "BASH" in the .profile file (Case insensitive)" \
5 "Exit" 

}

while [ 1 ]
do

CHOICE="$(showMenu)"
case $CHOICE in
"1")
    #gets and installs gnome disk utility and gparted
    sudo apt-get install gnome-disk-utility
    sudo apt-get install gparted
    ;;
"2")
    #creates a blank text file on the desktop called            Demo.txt
    touch /home/admin/Desktop/Demo.txt
    zenity --info \
    --text="File Demo.txt created on Desktop" \
    ;;
"3")
    zenity --question \
    --text="Are you sure you want to remove Demo.txt?" \
    if ["$?" = 0]
    then
    #removes the Demo.txt file from the desktop
    rm /home/admin/Desktop/Demo.txt

    zenity --info \
    --text="File has been removed" \
    ;;
"4")
    #searches the .profile file for the word 'BASH' (Not case           sensitive)
    grep -i "BASH" /home/mintuser/.profile
    ;;
"5")
    echo "Are you sure you want to exit? Press y/n"
    read YN
    case "$YN" in
"y")
    exit
    ;;
"n")
    #command for 'ESC' in BASH. Clears the screen
    printf "\ec"
    ;;
*)
    echo "Invalid option"
    ;;
esac
done

我有一个脚本的命令行版本,但是当我使用Zenity小部件创建一个问题已经发生的GUI时。感谢阅读和我可能获得的任何帮助。

1 个答案:

答案 0 :(得分:3)

你有一些错误,我在下面指出:

#!/bin/bash

#...

# [ is a command, just like any other. It just happens to be commonly used on if/while.
#while [ 1 ]
while true
do
    CHOICE="$(showMenu)"
    case $CHOICE in
    # ... 
    "3")
        zenity --question \
        --text="Are you sure you want to remove Demo.txt?" #\
        # Note the removal of the line continuation --------^
        # As I said, [ is a program. It therefore requires all arguments be separated by whitespace.
        # Also, = is for string comparison; use -eq for numeric.
        #if ["$?" = 0]
        if [ "$?" -eq 0 ]
        then
            # Indent properly so you see the missing terminators
            #removes the Demo.txt file from the desktop
            rm /home/admin/Desktop/Demo.txt

            zenity --info \
            --text="File has been removed" #\
            # Another line continuation we  ^ don't want
            # With the indentation, we can see you were missing a fi.
        fi
        ;;
    "4")
        #searches the .profile file for the word 'BASH' (Not case           sensitive)
        grep -i "BASH" /home/mintuser/.profile
        ;;
    "5")
        echo "Are you sure you want to exit? Press y/n"
        read YN
        case "$YN" in
        "y")
            exit
            ;;
        "n")
            #command for 'ESC' in BASH. Clears the screen
            printf "\ec"
            ;;
        *)
            echo "Invalid option"
            ;;
        esac
    # Missing a second esac
    esac
done