Bash 脚本和/bin/bash^M: bad interpreter: No such file or directory [英] Bash script and /bin/bash^M: bad interpreter: No such file or directory

查看:28
本文介绍了Bash 脚本和/bin/bash^M: bad interpreter: No such file or directory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过本教程学习 bash 脚本 来为我自动化一些任务.我正在使用腻子连接到服务器.

位于 .../Documents/LOG 中的脚本是:

#!/bin/bash# 我的第一个脚本回声世界你好!"

我为读/写/执行权限

执行了以下操作

chmod 755 my_script

然后,当我输入 ./my_script 时,我收到了标题中给出的错误.

一些类似的问题想看看这些,所以我认为它们可能会有所帮助:

哪个 bash

<块引用>

/bin/bash

echo $PATH

<块引用>

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/bin/mh

我尝试将当前目录添加到 PATH,但这不起作用..

解决方案

我在 Windows env 中创建脚本然后移植到 Unix 环境中运行时遇到过这个问题.

尝试在脚本上运行 dos2unix:

http://dos2unix.sourceforge.net/

或者只是使用 vi 和测试在您的 Unix 环境中重写脚本.

Unix 使用不同的行结尾,因此无法读取您在 Windows 上创建的文件.因此它将 ^M 视为非法字符.

如果您想在 Windows 上编写文件然后移植,请确保您的编辑器设置为以 UNIX 格式创建文件.

在屏幕右下角的记事本++中,它会告诉您文档格式.默认情况下,它会显示 DosWindows.要更改它,请转到

  • 设置->首选项
  • 新文档/默认目录标签
  • 选择格式为 unix 并关闭
  • 创建一个新文档

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.

The script, located in .../Documents/LOG, is:

#!/bin/bash
# My first script
echo "Hello World!"

And I executed the following for read/write/execute permissions

chmod 755 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

and

echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/bin/mh

I tried adding current directory to PATH, but that doesn't work..

解决方案

I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.

Try running dos2unix on the script:

http://dos2unix.sourceforge.net/

Or just rewrite the script in your Unix env using vi and test.

Unix uses different line endings so can't read the file you created on Windows. Hence it is seeing ^M as an illegal character.

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.

In notepad++ in the bottom right of the screen, it tells you the document format. By default, it will say DosWindows. To change it go to

  • settings->preferences
  • new document / default directory tab
  • select the format as unix and close
  • create a new document

这篇关于Bash 脚本和/bin/bash^M: bad interpreter: No such file or directory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆