【问题标题】:why is my .sh file not running in terminal? [duplicate]为什么我的 .sh 文件没有在终端中运行? [复制]
【发布时间】:2016-12-02 03:08:28
【问题描述】:

所以我格式化了“colors.sh”,它位于我桌面上一个名为记事本的文件中。尝试使用:

    ./colors.sh

然后它说这个......

    shell@shell:~$ cd Desktop/notepad/
    shell@shell:~/Desktop/notepad$ ll
    total 144
    drwx------  2 shell shell  4096 Apr  9  2015 ./
    drwxr-xr-x 11 shell shell  4096 Jul 27 19:42 ../
    -rw-r--r--  1 shell shell 35551 Oct  7  2014 colors background.PNG
    -rwxr-xr-x  1 shell shell  2534 Apr  9  2015 colors.bat~*
    -rw-r--r--  1 shell shell 31047 Oct  7  2014 colors.PNG
    -rwxr-xr-x  1 shell shell  2475 Apr  9  2015 colors.sh*
    -rwxr-xr-x  1 shell shell  2475 Apr  9  2015 colors.sh~*
    -rw-r--r--  1 shell shell  1142 Aug 22  2013 Command Prompt.lnk
    -rw-r--r--  1 shell shell    84 Nov 21  2014 desktop.ini
    -rwxr-xr-x  1 shell shell    92 Nov 21  2014 hello.bat*
    -rw-r--r--  1 shell shell  3755 Oct 16  2014 netstat.PNG
    -rw-r--r--  1 shell shell  1158 Aug 22  2013 Notepad.lnk
    -rw-r--r--  1 shell shell   970 Nov 18  2014 NSIS.lnk
    -rwxr-xr-x  1 shell shell   504 Nov 21  2014 rainbow.bat*
    -rwxr-xr-x  1 shell shell    31 Nov 21  2014 random.bat*
    -rwxr-xr-x  1 shell shell   561 Oct  7  2014 spanish.bat*
    -rw-r--r--  1 shell shell   687 Oct  6  2014 text to speech.vbs
    -rwxr-xr-x  1 shell shell    50 Oct 12  2014 timer.bat*
    -rw-r--r--  1 shell shell  2060 Feb 13  2013 Turbo C++.lnk
    -rw-r--r--  1 shell shell   182 Sep 23  2014 welcome dif.vbs
    -rw-r--r--  1 shell shell  2472 Sep 22  2014 welcome - Shortcut.lnk
    shell@shell:~/Desktop/notepad$ chmod +x colors.sh
    shell@shell:~/Desktop/notepad$ ./colors.sh
    bash: ./colors.sh: /bin/sh^M: bad interpreter: No such file or directory

【问题讨论】:

  • 可能问题出在 in colors.sh?
  • 使用不同的文本编辑器。在文件的第一行 (#!/bin/sh) 的行尾字符之前,您有一个杂散的回车 (^M)。可能文件的每一行都有同样的问题。
  • 哦。好点子!老实说,甚至没有想到这一点

标签: shell file execute


【解决方案1】:

错误消息中的^M 告诉大家:您的文本文件已存储为“DOS”文本格式,即以CR+LF 行结尾。 U*x 解释器仅将 LF (^J) 字符作为行终止符,因此多余的 CR (^M) 字符被附加到 /bin/sh shell 声明中,从而使其无效。

使用 Unix/Linux 换行符 (LF) 保存您的文件,或在使用前使用 dos-to-unix 转换器进行转换。

【讨论】:

    猜你喜欢
    • 2021-10-17
    • 2018-01-08
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    • 2022-08-24
    • 1970-01-01
    相关资源
    最近更新 更多