-2

したがって、次のコード(WIP)があり、それを実行しようとすると、ターミナルは即座に開閉します。何が原因なのかよくわかりませんので、誰か助けていただければ幸いです。コードは以下のとおりです。

#!/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を作成したときに、問題が発生しました。読んでくれてありがとう、そして私が受けるかもしれないどんな助けでも。

4

1 に答える 1

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
于 2013-02-20T22:41:35.863 に答える