3

I'm trying to run my shell script on Linux (Ubuntu).
It's running correctly on MacOS, but on Ubuntu it doesn't.

#!/usr/bin/env bash
while true
do
   node Client/request -t 10.9.2.4 -p 4400 --flood
done

Ubuntu output this error for running: sh myScript.sh:

Syntax error: end of file unexpected (expecting "do")
  1. Why is there any difference between them, since both of them are running by Bash? How can I avoid future errors caused by their differences?
  2. I tried cat yourscript.sh | tr -d '\r' >> yournewscript.sh as related question was suggested to do, and also while [ true ]. The command hexdump -C util/runner.sh result is:

    00000000  23 21 2f 75 73 72 2f 62  69 6e 2f 65 6e 76 20 62  |#!/usr/bin/env b|
    00000010  61 73 68 0d 0a 0d 0a 77  68 69 6c 65 20 5b 20 74  |ash....while [ t|
    00000020  72 75 65 20 5d 0d 0a 64  6f 0d 0a 20 20 20 6e 6f  |rue ]..do..   no|
    00000030  64 65 20 43 6c 69 65 6e  74 2f 72 65 71 75 65 73  |de Client/reques|
    00000040  74 20 2d 74 20 31 39 32  2e 31 36 38 2e 30 2e 34  |t -t 192.168.0.4|
    00000050  31 20 2d 70 20 34 34 30  30 20 2d 2d 66 6c 6f 6f  |1 -p 4400 --floo|
    00000060  64 0d 0a 64 6f 6e 65 0d  0a                       |d..done..|
    00000069
    
5
  • 2
    Perhaps we could help you better if you told us what problem you have with the script? How doesn't it work on Ubuntu? And are you sure it's a problem with Bash and not Node or the application it tries to run? Commented Aug 23, 2018 at 8:22
  • @Someprogrammerdude you right, thanks! Edited. And I'm trying to run it by shell. Commented Aug 23, 2018 at 8:25
  • 1
    How do you transfer the script to the Ubuntu system, or what settings do you have in your editor when saving the file? I wonder because the script seems to have Windows line-endings (CR-LF, "\r\n"), and not POSIX line-endings (plain LF, "\n"). That might have something to do with your problem. Commented Aug 23, 2018 at 8:27
  • 1
    Oh and the hexdump you show doesn't match the script you show. There should be an empty line between the two first (the "she-bang" and the while). Commented Aug 23, 2018 at 8:33
  • tr -d '\r' < yourscript.sh > yournewscript.sh then bash yournewscript.sh Commented Aug 23, 2018 at 8:34

1 Answer 1

6

The shebang #! line at the top of your file tells that this is a bash script. But then you run your script with sh myScript.sh, therefore using the sh shell.

The sh shell is not the same as the bash shell in Ubuntu, as explained here.

To avoid this problem in the future, you should call shell scripts using the shebang line. And also make sure to prefer bash over sh, because the bash shell is more convenient and standardized (IMHO). In order for the script to be directly callable, you have to set the executable flag, like this:

chmod +x yournewscript.sh

This has to be done only once (it's not necessary to do this on every call.)

Then you can just call the script directly:

./yournewscript.sh

and it will be interpreted by whatever command is present in the first line of the script.

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

5 Comments

Naming bash scripts with a .sh suffix doesn't help either😄
The name is unimportant. It matters how you execute it.
I agree it only matters how you execute it, but I meant that the OP naming the script with .sh is misleading... why not name it spreadsheet.xls and confuse the heck out of everyone?
@MarkSetchell: I understand.. in the last company I worked the shell scripts all had a .sh suffix, although they were bash-scripts. It didn't turn out to be a problem, because it was always clear that we are using the bash, and not sh. So I thought that was normal. What would be a good extension? Or no extension, just ./yournewscript?
I mean, I always read .sh as "I am a shell script", and not as "I am an sh script".

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.