如果没有./,最小的Bash脚本将无法正常运行 [英] Minimal Bash script doesn't run properly without ./

查看:65
本文介绍了如果没有./,最小的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

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

(And I have to add something here because my post is mostly code.)

推荐答案

在/usr/bin/test中通常有一个测试,它比您的主目录中的路径领先.您可以通过输入命令which test

There is usually a test in /usr/bin/test, which would be ahead of the one in your home directory in your path. You can figure this out by entering the command which test

正如Alok Singhal在下面的评论中指出的那样,test也是内置在bash shell(和其他一些shell)中的命令之一,如果您使用的是bash,则另一个内置shell命令type不仅可以显示将执行哪个test(即使它是内置的Shell,并且在文件系统上不可用),而且还可以显示所有版本测试它正在隐藏.例如,如果我在/usr/bin之前有/home/pat/bin:

As Alok Singhal points out in a comment below, test is also one of the commands built into the bash shell (and some other shells), and if you are using bash, then another built-in shell command, type, can show you, not only which test will be executed (even if it is a shell built-in and not available on the file system), but also, all the versions of test it is hiding. For example, if I have /home/pat/bin ahead of /usr/bin:

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

因此,键入type -a <cmdname>对于弄清将要执行的内容以及将执行的内容非常有用.通过显示完整路径,即使您决定不重命名,它也允许您使用剪切和粘贴(在大多数终端程序中)选择并执行适当的程序. (其他shell也具有类似的别名功能.)

So, typing type -a <cmdname> is very useful to figure out, not only what will be executed, but also what will not be executed. By showing full paths, it also lets you use cut and paste (in most terminal programs) to choose and execute the proper program, even if you decide not to rename it. (Other shells also have similar alias facilities.)

顺便说一句,man type不会为您提供有关type bash shell命令的任何有用信息,因为它不是独立程序. man bash确实描述了它,因为它是内置在bash shell中的-但是该文档中的文字词"type"有很多用法,因此,如果您要查找信息,最快的方法是滚动到底部在type命令上.

As an aside, man type won't give you any useful information about the type bash shell command, because it's not a standalone program. man bash does describe it, since it is built in to the bash shell -- but there are a lot of uses of the literal word "type" in that document, so it's quickest to scroll to the bottom if you are looking for information on the type command.

正如hek2mgl指出的那样,如果您在执行命令时遇到困难,则hash命令也很有用.特别是,如果您创建的程序名称与其他名称相同,并且已经运行了其他名称,则脚本可能无法运行,即使该脚本位于路径的首位.

As hek2mgl points out, the hash command also be useful if you are having trouble getting your command to execute. In particular, if you have created a program that has the same name as something else, and you have already run that something else, your script may not run, even though it's first in the path:

$ 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.
>>> 

这篇关于如果没有./,最小的Bash脚本将无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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