Bash 在MacOS和Linux上运行不同的Shell脚本
我正在尝试在Linux(Ubuntu)上运行shell脚本。Bash 在MacOS和Linux上运行不同的Shell脚本,bash,macos,ubuntu,Bash,Macos,Ubuntu,我正在尝试在Linux(Ubuntu)上运行shell脚本。 它在MacOS上运行正常,但在Ubuntu上运行不正常 #!/usr/bin/env bash while true do node Client/request -t 10.9.2.4 -p 4400 --flood done Ubuntu在运行时输出此错误:sh myScript.sh: Syntax error: end of file unexpected (expecting "do") 既然它们都是由Bash运行
它在MacOS上运行正常,但在Ubuntu上运行不正常
#!/usr/bin/env bash
while true
do
node Client/request -t 10.9.2.4 -p 4400 --flood
done
Ubuntu在运行时输出此错误:sh myScript.sh:
Syntax error: end of file unexpected (expecting "do")
cat yourscript.sh | tr-d'\r'>>yournewscript.sh
作为相关问题的建议,并且在[true]时也尝试了。
命令hextump-C util/runner.sh
的结果是:
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
#文件顶部的代码>行表明这是一个bash脚本。但是,然后使用shmyscript.sh
运行脚本,因此使用sh
shell
shell与Ubuntu中的bash
shell不同,正如。
为了避免将来出现这个问题,应该使用shebang行调用shell脚本。而且还要确保更喜欢bash而不是sh,因为bashshell更方便和标准化(IMHO)。为了使脚本可以直接调用,必须设置可执行标志,如下所示:
chmod +x yournewscript.sh
此操作只需执行一次(并非每次通话都必须执行此操作。)
然后您可以直接调用脚本:
./yournewscript.sh
它将由脚本第一行中出现的任何命令来解释。shebang#文件顶部的代码>行表明这是一个bash脚本。但是,然后使用shmyscript.sh
运行脚本,因此使用sh
shell
shell与Ubuntu中的bash
shell不同,正如。
为了避免将来出现这个问题,应该使用shebang行调用shell脚本。而且还要确保更喜欢bash而不是sh,因为bashshell更方便和标准化(IMHO)。为了使脚本可以直接调用,必须设置可执行标志,如下所示:
chmod +x yournewscript.sh
此操作只需执行一次(并非每次通话都必须执行此操作。)
然后您可以直接调用脚本:
./yournewscript.sh
它将由脚本第一行中的任何命令进行解释。如果您告诉我们脚本有什么问题,我们可能会更好地帮助您?它是如何在Ubuntu上工作的?你确定这是Bash的问题,而不是节点或它试图运行的应用程序的问题吗?@Someprogrammerdude你说得对,谢谢!编辑。我正在尝试用shell运行它。你如何将脚本传输到Ubuntu系统,或者在保存文件时在编辑器中有什么设置?我很奇怪,因为脚本似乎有Windows行结尾(CR-LF,“\r\n”
),而没有POSIX行结尾(普通LF,“\n”
)。这可能与您的问题有关。哦,您显示的hextump与您显示的脚本不匹配。前两个(she bang)和while
)之间应该有一条空行。tr-d'\r'
然后bash yoursnewscript.sh
如果你告诉我们你的脚本有什么问题,也许我们可以更好地帮助你?它是如何在Ubuntu上工作的?你确定这是Bash的问题,而不是节点或它试图运行的应用程序的问题吗?@Someprogrammerdude你说得对,谢谢!编辑。我正在尝试用shell运行它。你如何将脚本传输到Ubuntu系统,或者在保存文件时在编辑器中有什么设置?我很奇怪,因为脚本似乎有Windows行结尾(CR-LF,“\r\n”
),而没有POSIX行结尾(普通LF,“\n”
)。这可能与您的问题有关。哦,您显示的hextump与您显示的脚本不匹配。前两个(she bang)和while
)之间应该有一条空行。tr-d'\r'
然后bash yoursnewscript.sh
命名bash
带有.sh
后缀的脚本没有帮助,因为名称并不重要。它关系到你如何执行它。我同意它只关系到你如何执行它,但我的意思是,用.sh
命名脚本的操作是误导性的。。。为什么不把它命名为spreadsheet.xls
,把每个人都搞糊涂了呢?@MarkSetchell:我明白。。在上一家公司,我工作的shell脚本都有.sh
后缀,尽管它们是bash脚本。这并不是一个问题,因为很明显我们使用的是bash,而不是sh。所以我认为这是正常的。什么是好的扩展?或者没有扩展名,只是/yournewscript
?我的意思是,我总是把.sh
读作“我是一个shell脚本”,而不是“我是一个sh
脚本”。用.sh
后缀命名bash
脚本并没有任何帮助。名称并不重要。它关系到你如何执行它。我同意它只关系到你如何执行它,但我的意思是,用.sh
命名脚本的操作是误导性的。。。为什么不把它命名为spreadsheet.xls
,把每个人都搞糊涂了呢?@MarkSetchell:我明白。。在上一家公司,我工作的shell脚本都有.sh
后缀,尽管它们是bash脚本。这并不是一个问题,因为很明显我们使用的是bash,而不是sh。所以我认为这是正常的。什么是好的扩展?或者没有扩展名,只是/yournewscript
?我的意思是,我总是把.sh
读成“我是一个shell脚本”,而不是“我是一个sh
脚本”。