不影响UNIX中的shell脚本的错误行

时间:2014-11-29 11:37:28

标签: shell unix

./ menu:第12行:[1:命令未找到

即使不影响脚本的功能,这件事仍然会出现。

脚本:

#!/bin/bash/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/rvs130@studin.aubgin.$

function press_enter
{
    echo ""
    echo -n "Press Enter to continue"
    read
    clear
}

choice=
until ["$choice" = "0"]; do
        echo ''
        echo '          SMILEY SERVER MENU'
        echo '1 - To view the available smilies'
        echo '2 - Search for a smiley'
        echo '3 - Add a new smiley'
        echo '4 - Delete a smiley'
        echo '5 - Edit smiley or description'
        echo ''
        echo '0 - Exit'
        echo 'Enter your choice: '
        read choice

        case "$choice" in
                1)
                cat smilies.txt;;
                2)
                echo 'Please enter the name of the smiley you want to see: '
                read name
                echo 'Match found: '
                grep $name smilies.txt ;;
                3)
                echo 'Enter the smiley you want to add followed'
                echo 'by a space and description: '
                read newsmiley
                echo "$newsmiley" >> smilies.txt
                echo 'New smiley added.' ;;
                4)
                echo 'Enter a smiley or its description you want to delete: '
                read delsmiley
                sed -i /"$delsmiley"/d smilies.txt ;;
                5)
                echo 'Enter a word you want to edit, space'
                echo 'and then desired replacement word: '
                read word1 word2
                replace "$word1" "$word2" -- smilies.txt ;;
                0) exit ;;
                *)
                echo 'Please enter a valid choice.'; press_enter
        esac
done

1 个答案:

答案 0 :(得分:1)

必须[]周围留出空格。您的脚本中发生的情况是第一次,$choice为空,因此该行评估为[ = "0"]导致bash: [: missing `]'之类的错误第一轮(除非你set -o errexit,否则不会影响执行)。选择1并再次回到测试行后,它将计算为[1 = "0"],其中[1被解释为您要运行的命令。你想要的是:

until [ "$choice" -eq 0 ]

此外,这不是有效的shebang line