0

I wrote a really simple file explorer using ls with a shell script. I used a while loop to make the script run forever (until Ctrl+C), but the while loop doesn't seem to work. I get this error

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

My code is this:

#!/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
}
1
  • 2
    paste your code in shellcheck.net Commented Aug 6, 2015 at 12:21

2 Answers 2

1

That's not the correct syntax, you need something like:

while CONDITION ; do
    ACTION
done

Without the do, the done is indeed unexpected. In addition, your if statements should be of the form:

if CONDITION ; then
    ACTION
fi

The bash man page shows the correct forms in more detail:

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

Keep in mind those shown above are my preferred form, with the do/then on the same line as the while/if. You can also leave off the ; and move the do/then on to the next line but I consider that unnecessarily wasteful of screen real estate.

Sign up to request clarification or add additional context in comments.

1 Comment

I fixed it but it still gives the same error. I changed it to "while True; do".
0

Below script should work ..

#!/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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.