繁体   English   中英

Bash错误:意外令牌'done'附近的语法错误

[英]Bash error: syntax error near unexpected token `done'

我用ls和shell脚本编写了一个非常简单的文件浏览器。 我使用了while循环使脚本永久运行(直到Ctrl + C为止),但是while循环似乎不起作用。 我得到这个错误

./fileexplorer: line 5: syntax error near unexpected token `done'
./fileexplorer: line 5: `done'`

我的代码是这样的:

#!/bin/bash
ls -l $1
while :
    browse()
done

function browse()
{
    read file;
    if [ -f $file ]
        if test -e $file
            echo "Starting $file with nano."
            echo "Press a key to open the file."
            pause
            nano $file
    if test -d $file
        ls -l $file
}

这不是正确的语法,您需要类似:

while CONDITION ; do
    ACTION
done

没有dodone确实是出乎意料的。 另外,您的if语句应采用以下形式:

if CONDITION ; then
    ACTION
fi

bash手册页更详细地显示了正确的形式:

if list; then list; [ elif list; then list; ] ... [ else list; ] fi
while list-1; do list-2; done

请记住,上面显示的是我的首选格式, do/thenwhile/if 您也可以离开; ,然后将do/then移至下一行,但我认为这不必要地浪费了屏幕空间。

下面的脚本应该工作..

#!/bin/bash

function browse()
{
    read file;
    if [ -f $file ]
    then
        if [ -e $file ]
        then
            echo "Starting $file with nano."
            echo "Press a key to open the file."
            sleep 2
            nano $file
        fi
    fi
    if [ -d $file ]
    then
        ls -l $file
     fi
}

ls -l $1

while true;do
browse
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM