0

The shell script looks like this:

#!/bin/bash
while [ true ]; do
    clear
    date
    who
    sleep 5
done 

If I run int, I get the following:

yuikus@DESKTOP-VTJ0OG4:~$ cd /mnt/d/lab_oc_1/lab_oc_2/
yuikus@DESKTOP-VTJ0OG4:/mnt/d/lab_oc_1/lab_oc_2/$ sh bag.sh
bag.sh: 7: Syntax error: "done" unexpected (expecting "do")
yuikus@DESKTOP-VTJ0OG4:/mnt/d/lab_oc_1/lab_oc_2/$ bash bag.sh
bag.sh: line 7: syntax error near unexpected token `done'
bag.sh: line 7: `done '

Why doesn't it work? I run with Ubuntu, from Windows.

11
  • 1
    Also note that [ true ] is the same as [ false ], i.e. it checks that the string isn't empty.
    – choroba
    Sep 6, 2021 at 15:16
  • Try watch -n 5 who Sep 6, 2021 at 15:21
  • @StéphaneChazelas how do I write this down? what should be on which line? I'm really not good at this, I'm sorry
    – Y_sgard
    Sep 6, 2021 at 15:24
  • The watch -n 5 who command should do what your script does Sep 6, 2021 at 15:24
  • Please keep your question to one item at a time. I wrote my suggestion as an answer, in order to have the original problem "solved" (which you may want to accept if that actually was the solution). If you have a follow-up question, please don't modify the original question, but create a new onw. You can still add a link to this question to establish context.
    – AdminBee
    Sep 6, 2021 at 15:25

1 Answer 1

1

Since you are operating Ubuntu under Windows, you likely edited the script concurrently with Windows and Linux editors. However, Windows and Linux use different line-endings, which can confuse script interpreters.

Run dos2unix over your script file; this will replace any Windows (CR-LF) line endings with Unix line endings:

$ dos2unix bag.sh

Then, run your script as originally intended. Note that since you have a #!-line stating /bin/bash, it would make more sense to make the script executable and run it as

$ ./bag.sh

than running it under sh, which may use another shell than the one you want (Bash).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .