脚本全名和路径 $0 调用时不可见
我有一个脚本“task.sh”,其内容如下:
#!/bin/bash
CUR_DIR=`pwd`
SCRIPTPATH="${CUR_DIR}/`dirname $0`"
当我用“bash task.sh”调用它时,它按预期工作,但是当用“.task.sh”调用它时
$ . log/task.sh
dirname: invalid option -- b
Try `dirname --help' for more information.
,当脚本在 crontab 中计划时,它会按预期工作。效果不太好。 有人可以告诉我我做错了什么或以不同的方式获取不是当前目录的脚本的目录 ?
I have a script "task.sh" with the following content:
#!/bin/bash
CUR_DIR=`pwd`
SCRIPTPATH="${CUR_DIR}/`dirname $0`"
when I call it with "bash task.sh" it works as expected but when it is called with ". task.sh"
$ . log/task.sh
dirname: invalid option -- b
Try `dirname --help' for more information.
When the script is being scheduled in crontab it is not working as well.
Can someone tell me what am I doing wrong or a different way in order to get the directory of a script that is not the current directory
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您将其作为
bash task.sh
调用时,bash 会将“task.sh”分配给 $0(来自 bash 手册:“如果使用命令文件调用 Bash [...] $0 将设置为该文件的名称。”)。当您获取文件时,bash 不会更改 $0,它只是在当前环境中执行脚本。在您当前的环境中 0 美元有什么?
前导破折号将被
dirname
解释为一个选项。如果它是在 cron 作业中,你为什么要采购它?
如果您需要获取脚本的来源,如果您的 shell 是 bash,这将起作用:
但是,我相信 cron 的 shell 是 /bin/sh。即使 /bin/sh 是 bash 的符号链接,当 bash 作为 sh 调用时,它也会尝试按 POSIX 方式运行:BASH_ARGV 数组可能对您不可用。
When you invoke it as
bash task.sh
, bash assigns "task.sh" to $0 (from the bash manual: "If Bash is invoked with a file of commands [...] $0 is set to the name of that file.").When you source the file, bash does not alter $0, it just executes the script in the current environment. What's in $0 in your current enviroment?
The leading dash will be interpreted by
dirname
as an option.If it's in a cron job, why are you sourcing it?
If you need to source your script, this will work if your shell is bash:
However, cron's shell is, I believe, /bin/sh. Even if /bin/sh is a symlink to bash, when bash is invoked as sh it will try to behave POSIXly: the BASH_ARGV array probably won't be available to you.
我已经使用这个很长一段时间了,没有任何问题。
--
禁止进一步处理参数。I have used this for a long time without issues.
The
--
disable further processing of parameters.使用 bash 时没有理由调用外部二进制文件,例如
pwd
和dirname
。这两个二进制文件的功能可以使用纯 shell 语法来复制。请尝试以下操作:
There is no reason to call external binaries such as
pwd
anddirname
when using bash. The functionality of these two binaries can be replicated with pure shell syntax.Try the following:
当您键入时,
您正在执行脚本 foo.sh,并且 bash 将输入参数 $0 设置为正在运行的脚本的名称。
当您键入时,
您正在获取脚本并且未设置输入参数 $0。
在这种情况下,您可以使用自动变量 $_,它包含最后执行的命令的参数。
您可以在脚本中键入,
以获取 foo.sh 的路径。
请注意,要使其正常工作,这必须是文件中执行的第一个命令。
否则 $_ 将不包含源脚本的路径。
感谢丹尼斯·威廉姆森为类似的问题。
When you type,
you are executing script foo.sh, and bash sets the input argument $0 to the name of the script which is being run.
When you type,
you are sourcing the script and the input argument $0 is not set.
In this situation you can use the automatic variable $_ which contains the argument of the last executed command.
In your script you could type,
to get the path of foo.sh.
Notice that, for this to work, this has to be the first command executed in the file.
Otherwise $_ will not contain the path of the sourced script.
Kudos to Dennis Williamson for providing this answer to a similar question.