bash 第 1 行:?#!/usr/bin/sh:尝试执行 shell 脚本时未找到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28025514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:12:56  来源:igfitidea点击:

line 1: ?#!/usr/bin/sh: not found when trying to execute a shell script

bashshell

提问by Don

I have a script called autoinstall:

我有一个名为autoinstall的脚本:

#!/usr/bin/sh
echo "Installasi membutuhkan free space minimal 2MB, pastikan ada punya cukup space di router anda"
read -p "Anda yakin ingin melanjutkan installasi?(y/n) " -n 1 -r
echo ""
if [[ $REPLY = ^[Yy]$ ]]
then
    cd /
    cd /tmp/
    tar -xvf OpenWrt_Angel_Beats_Edition_v1.3.3.tar -C /
    chmod -R 744 /root/crt
    chmod 744 /www/wget/wget_download.sh
    chmod 744 /usr/bin/gsm
    chmod 744 /usr/bin/profile
    opkg update && opkg install elinks
    cp /etc/rc.local /etc/rc.local.backup
    cat > /etc/rc.local << END
    #!bin/sh
    # /etc/rc.local: Local system initialization script.
    #
    # Put any local startup commands in here. Also, if you have
    # anything that needs to be run at shutdown time you can
    # make an /etc/rc.d/rc.local_shutdown script and put those
    # commands in there.
    sh /www/wget/wget_download.sh > /dev/null 2>&1 &
    exit 0
END
    killall sh /www/wget/wget_download.sh
    sh /www/wget/wget_download.sh > /dev/null 2>&1 &
    echo "File backup /etc/rc.local.backup telah dibuat, gunakan file ini untuk mengembalikan konfigurasi rc.local anda yang dulu jika diperlukan"
    echo "Installasi selesai. Jangan lupa di akun openvpn yang digunakan (/root/crt/xxx.ovpn) tambahkan baris ini:
    script-security 2
    up client-connect.sh"
else
    echo ""
    echo "Installasi dibatalkan"
fi

Every command that I put in the first line always gets the error above (line 1:xxx not found) and I'm sure I've typed in the correct command, even echogives the error like that, how do I solve this?

我放在第一行的每个命令总是得到上面的错误 ( line 1:xxx not found) 并且我确定我输入了正确的命令,甚至echo给出了这样的错误,我该如何解决这个问题?

采纳答案by jlliagre

The file might have been edited with an editor that insert a Unicode BOM (Byte Order Mark).

该文件可能已使用插入 Unicode BOM(字节顺序标记)的编辑器进行了编辑。

Have a look to the first line contents with:

看看第一行的内容:

od -c autoinstall | head -1

or

或者

hd -n 16 autoinstall

If you see unexpected characters before #!/usr/bin/sh, you might try one of the methods described here Using awk to remove the Byte-order markto remove the BOM.

如果您之前看到意外字符#!/usr/bin/sh,您可以尝试使用此处描述的方法之一使用 awk 删除字节顺序标记以删除 BOM。

回答by Aaron Digulla

There can be two problems here:

这里可能有两个问题:

  1. The file doesn't exist. Usually, for sh, the path is /bin/sh, so it should be #!/bin/sh

  2. You're editing the file on Windows. Windows uses CR+LF as line ending. Unix (and Linux) uses just LF. So for Linux, the command reads "execute /bin/sh<CR>and sh<CR>doesn't exist.

  1. 该文件不存在。通常,对于sh,路径是/bin/sh,所以应该是#!/bin/sh

  2. 您正在 Windows 上编辑文件。Windows 使用 CR+LF 作为行尾。Unix(和 Linux)只使用 LF。因此,对于 Linux,该命令显示为“执行/bin/sh<CR>并且sh<CR>不存在。

Solution: When editing the file, make sure you use Unix line endings.

解决方案:编辑文件时,请确保使用 Unix 行结尾。