在从文件运行时,命令行中的同一bash脚本不工作 [英] the same bash script in command line doesn't work when run from a file
问题描述
我试图从cygwin中删除Bash脚本。当我在命令提示符中手动键入以下行时,一切正常,我看到从1到10的数字打印在屏幕上。
for i in {1..10}; do
echo $ i;但是,如果我将脚本保存到一个文件(test.sh)中,并且将脚本保存到一个文件(test.sh)中,运行./test.sh,我看到以下错误 ./ test.sh:line 1:syntax error接近意外的令牌`$'do \r''
'/test.sh:line 1:`for in in {1..10}; do
这看起来很奇怪。
谢谢你,
Derek
我想知道如何使用相同的脚本在命令行中运行而不是从文件运行。 div class =h2_lin>解决方案
\r
是一个提示。你的文件可能有CRLF( \r\\\
)行结束,bash解释器不能正常处理。将编辑器设置更改为使用Unix风格(
\\\
)行尾,或运行
/ usr / bin / dos2unix< file& code>在您的文件上重置行结束。
I am trying to lean Bash scripting from cygwin. When i manually type the following lines in command prompt, everything works and I see that the number from 1 to 10 are printed on the screen.
for i in {1..10}; do
echo $i;
done
However, if I save the script into a file (test.sh) and run "./test.sh", I was shown the following errors
./test.sh: line 1: syntax error near unexpected token `$'do\r''
'/test.sh: line 1: `for i in {1..10}; do
This seems very strange to me. i am wondering how come the same script runs fine in command line but not from a file.
Thanks, Derek
The \r
is a hint. Your file probably has CRLF (\r\n
) line endings, which the bash interpreter does not handle gracefully. Change your editor settings to use Unix-style (\n
) line endings, or run /usr/bin/dos2unix <file>
on your file to reset the line endings.
这篇关于在从文件运行时,命令行中的同一bash脚本不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!