Bash脚本和/bin/bash ^ M:错误的解释器:没有这样的文件或目录 [英] Bash script and /bin/bash^M: bad interpreter: No such file or directory
问题描述
我正在学习本教程,以学习 bash脚本为我自动化一些任务.我正在使用腻子连接到服务器.
I'm learning through this tutorial to learn bash scripts to automate a few tasks for me. I'm connecting to a server using putty.
位于.../Documents/LOG
中的脚本为:
#!/bin/bash
# My first script
echo "Hello World!"
然后我对read/write/execute permissions
chmod 755 my_script
然后,当我输入./my_script
时,出现标题中给出的错误.
Then, when I enter ./my_script
, I'm getting the error given in the title.
一些类似的问题想看看这些,所以我认为它们可能会有所帮助:
Some similar questions wanted to see these, so I think they might help:
which bash
/bin/bash
和
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/bin/mh
我尝试将当前目录添加到PATH
,但这不起作用.
I tried adding current directory to PATH
, but that doesn't work..
推荐答案
在Windows env中创建脚本,然后移植到Unix环境中运行时,我已经看到了这个问题.
I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.
尝试在脚本上运行dos2unix
:
http://dos2unix.sourceforge.net/
或者只是使用vi
并在测试中重写Unix环境中的脚本.
Or just rewrite the script in your Unix env using vi
and test.
Unix使用不同的行尾,因此无法读取您在Windows上创建的文件.因此,它会将^ M视为非法字符.
Unix uses different line endings so can't read the file you created on Windows. Hence it is seeing ^M as an illegal character.
如果要在Windows上写文件然后移植过来,请确保将编辑器设置为以UNIX格式创建文件.
If you want to write a file on Windows and then port over, make sure your editor is set to create files in UNIX format.
在屏幕右下角的notepad ++中,它告诉您文档格式.默认情况下,它将显示Dos\Windows
.要更改它,请转到
In notepad++ in the bottom right of the screen, it tells you the document format. By default, it will say Dos\Windows
. To change it go to
- 设置->首选项
- 新文档/默认目录标签
- 选择unix格式并关闭
- 创建一个新文档
这篇关于Bash脚本和/bin/bash ^ M:错误的解释器:没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!