没有./的最小Bash脚本无法正常运行

时间:2015-08-01 05:11:44

标签: bash

在此控制台会话中说明

bir@N2C:~$ echo $PATH
.../home/bir/bin:...         # script folder is in PATH
bir@N2C:~$ ls -lh ~/bin/     # script permissions look ok
...
-rwxr-xr-x 1 bir bir   28 Jul 31 21:46 test
...
bir@N2C:~$ test              # no output
bir@N2C:~$ ~/bin/test
startend                     # anticipated output
bir@N2C:~$ cd ~/bin/
bir@N2C:~/bin$ ./test
startend                     # anticipated output
bir@N2C:~/bin$  cat test     
#!/bin/sh
echo "start$1end"

此外:

bir@N2C:~$ which test
/home/bir/bin/test
bir@N2C:~$ whereis test
test: /usr/bin/test /usr/bin/X11/test /usr/share/man/man1/test.1.gz

(我必须在这里添加一些内容,因为我的帖子主要是代码。)

1 个答案:

答案 0 :(得分:2)

/ usr / bin / test中通常会有一个测试,它位于路径中主目录中的测试之前。您可以输入命令which test

来解决这个问题 编辑:正如Alok Singhal在下面的评论中指出的那样,test也是bash shell(以及其他一些shell)中内置的命令之一,如果你使用的是bash,那么是另一个内置的shell命令type可以向您显示,不仅会执行哪个test(即使它是内置的shell而且在文件系统上不可用),而且,它隐藏的所有测试版本。例如,如果我在/ usr / bin之前有/ home / pat / bin:

$ type -a test
test is a shell builtin
test is /home/pat/bin/test
test is /usr/bin/test

因此,键入type -a <cmdname>非常有用,不仅要弄清楚将要执行的内容,还要确定执行的内容。通过显示完整路径,它还允许您使用剪切和粘贴(在大多数终端程序中)来选择和执行正确的程序,即使您决定不重命名它。 (其他shell也有类似的别名设施。)

顺便说一下,man type不会为您提供有关type bash shell命令的任何有用信息,因为它不是一个独立的程序。 man bash确实描述了它,因为它是内置于bash shell中的 - 但是字面上有很多用法&#34; type&#34;在该文档中,如果您要查找有关type命令的信息,则最快滚动到底部。

EDIT2:正如hek2mgl指出的那样,如果你在执行命令时遇到问题,hash命令也很有用。特别是,如果您创建的程序与其他程序具有相同的名称,并且您已经运行了其他程序,则您的脚本可能无法运行,即使它首先在路径中运行:

$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
$ echo echo Hi there! > bin/python
$ chmod 700 bin/python
$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
$ hash -r  # Clear the hash for this instance of bash
$ python
Hi there!
$ rm bin/python 
$ python
bash: /home/pat/bin/python: No such file or directory
$ hash -r
$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>