2013-02-20 180 views
-2

因此,我有以下代码(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

虽然'[1]'碰巧你打算什么它,它出于错误的原因(它总是真的有括号之间的1个参数 - '[0]'也将始终为真)。您应该使用'while true'来代替。 – Kevin 2013-02-20 22:12:28

+0

这样可以解决问题吗? – user2084095 2013-02-20 22:22:59

+0

可能不是,但你应该牢记它。 – Kevin 2013-02-20 22:24:35

回答

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 
+0

非常感谢你,绝对的传奇!我会记得从现在开始正确缩进,以便轻松发现错误 – user2084095 2013-02-20 23:01:38

+0

@ user2084095我看到了您的建议编辑,您试图删除个人原因的答案。什么原因应该让你这样做? – AbZy 2013-03-14 23:53:42