Shell脚本说没有找到命令。忽略工作?

时间:2021-10-11 14:38:49

It seems like my shebang is being ignored. When I try to execute a shell script I wrote, I get an error:

看来我的shebang被忽视了。当我尝试执行我编写的shell脚本时,我得到一个错误:

% ls -alh /usr/sh
-r-xr-xr-x  1 root  1011   139k Sep 16 01:29 sh
% ls -alh foo.sh
-rwxr-xr-x  1 george george    21B Sep 16 00:59 foo.sh
% cat ./foo.sh
#!/usr/sh
echo "lol"
% ./foo.sh
./foo.sh: Command not found.
% sh ./foo.sh
lol

Yes, it's at /usr/sh. I put it there.

是的,它是在/usr/sh.我把它放在那里。

But as you can see it works just fine when I instruct sh to execute it. What gives?

但是正如你所看到的,当我指示sh执行它时,它工作得很好。到底发生了什么事?

I don't think $PATH is applicable but in some search results trying to solve this problem, people have asked for it:

我不认为$PATH是适用的,但是在一些试图解决这个问题的搜索结果中,人们要求它:

% echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin

4 个解决方案

#1


2  

The reason for this can be one of several things. To list the most common:

这样做的原因可以是以下几个方面之一。列出最常见的:

  1. The file does not have executable permissions. Use chmod a+x foo.sh to give all users execute permissions.
  2. 该文件没有可执行的权限。使用chmod + x foo。sh给所有用户执行权限。
  3. Your shebang is incorrect, double-check the path. Use which sh and copy the result after a #!
  4. 您的shebang不正确,请再次检查路径。使用哪个sh并在#之后复制结果!
  5. Your lines are delimited with DOS feeds. Scripts need to be delimited with \n. Text editors such as nano allow you to convert by choosing Write-out then pressing Alt+D to toggle DOS format off. Utilities such as dos2unix can convert files directly on the command line (although it may need installing first).
  6. 您的行用DOS提要分隔。脚本需要用\n分隔。像nano这样的文本编辑器可以通过选择Write-out然后按Alt+D来切换DOS格式来进行转换。像dos2unix这样的实用工具可以直接在命令行上转换文件(尽管它可能需要先安装)。

#2


1  

You need to make your script file executable and try again.

您需要使您的脚本文件可执行,并再次尝试。

chmod +x foo.sh

#3


0  

You want something like this:

你想要这样的东西:

% cat ./foo.sh
#!/bin/sh
echo "lol"
% chmod +x ./foo.sh
% ls -l ./foo.sh
-rwxr-xr-x  1 deed  deed  0 16 sep 00:33 ./foo.sh

Note the path of /bin/sh and the executable bits.

注意/bin/sh的路径和可执行位。

#4


0  

Well, I do see your script is executable. It is the /usr/sh that bothers me... some UNIX/POSIX systems do not have it. Most of those that do are a link to a better shell.

我确实看到你的脚本是可执行的。困扰我的是/usr/sh…一些UNIX/POSIX系统没有这种功能。大多数这样做是为了找到更好的shell。

Try ls -l /usr/sh to see what that shows.

试试ls -l /usr/sh,看看会有什么结果。

You can also printenv SHELL and use the results as your script's shell. As this points to your login shell it is usually a good choice.

您还可以打印SHELL并将结果用作脚本的SHELL。因为这指向您的登录shell,所以通常是一个很好的选择。

#1


2  

The reason for this can be one of several things. To list the most common:

这样做的原因可以是以下几个方面之一。列出最常见的:

  1. The file does not have executable permissions. Use chmod a+x foo.sh to give all users execute permissions.
  2. 该文件没有可执行的权限。使用chmod + x foo。sh给所有用户执行权限。
  3. Your shebang is incorrect, double-check the path. Use which sh and copy the result after a #!
  4. 您的shebang不正确,请再次检查路径。使用哪个sh并在#之后复制结果!
  5. Your lines are delimited with DOS feeds. Scripts need to be delimited with \n. Text editors such as nano allow you to convert by choosing Write-out then pressing Alt+D to toggle DOS format off. Utilities such as dos2unix can convert files directly on the command line (although it may need installing first).
  6. 您的行用DOS提要分隔。脚本需要用\n分隔。像nano这样的文本编辑器可以通过选择Write-out然后按Alt+D来切换DOS格式来进行转换。像dos2unix这样的实用工具可以直接在命令行上转换文件(尽管它可能需要先安装)。

#2


1  

You need to make your script file executable and try again.

您需要使您的脚本文件可执行,并再次尝试。

chmod +x foo.sh

#3


0  

You want something like this:

你想要这样的东西:

% cat ./foo.sh
#!/bin/sh
echo "lol"
% chmod +x ./foo.sh
% ls -l ./foo.sh
-rwxr-xr-x  1 deed  deed  0 16 sep 00:33 ./foo.sh

Note the path of /bin/sh and the executable bits.

注意/bin/sh的路径和可执行位。

#4


0  

Well, I do see your script is executable. It is the /usr/sh that bothers me... some UNIX/POSIX systems do not have it. Most of those that do are a link to a better shell.

我确实看到你的脚本是可执行的。困扰我的是/usr/sh…一些UNIX/POSIX系统没有这种功能。大多数这样做是为了找到更好的shell。

Try ls -l /usr/sh to see what that shows.

试试ls -l /usr/sh,看看会有什么结果。

You can also printenv SHELL and use the results as your script's shell. As this points to your login shell it is usually a good choice.

您还可以打印SHELL并将结果用作脚本的SHELL。因为这指向您的登录shell,所以通常是一个很好的选择。