bash 脚本全名和路径 $0 在调用时不可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5708339/
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
script full name and path $0 not visible when called
提问by Hugo
I have a script "task.sh" with the following content:
我有一个包含以下内容的脚本“task.sh”:
#!/bin/bash
CUR_DIR=`pwd`
SCRIPTPATH="${CUR_DIR}/`dirname $ . log/task.sh
dirname: invalid option -- b
Try `dirname --help' for more information.
`"
when I call it with "bash task.sh" it works as expected but when it is called with ". task.sh"
当我用“bash task.sh”调用它时它按预期工作但是当它用“.task.sh”调用时
$ echo "SCRIPTPATH="${CUR_DIR}/${BASH_ARGV[0]}"
"
-bash
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 ?
在 crontab 中调度脚本时,它无法正常工作。有人可以告诉我我做错了什么或以不同的方式获取不是当前目录的脚本目录吗?
回答by glenn Hymanman
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.").
当您调用它时bash task.sh,bash 将“task.sh”分配给 $0(来自bash 手册:“如果使用命令文件调用 Bash [...] $0 被设置为该文件的名称。”)。
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?
当您获取文件时,bash 不会更改 $0,它只是在当前环境中执行脚本。在您当前的环境中,0 美元是多少?
#!/bin/bash
CUR_DIR="$PWD"
SCRIPTPATH="${CUR_DIR}/${0#*/}"
The leading dash will be interpreted by dirnameas an option.
前导破折号将被解释dirname为一个选项。
If it's in a cron job, why are you sourcing it?
如果它是一个 cron 工作,你为什么要采购它?
If you need to source your script, this will work if your shell is bash:
如果您需要获取脚本的源代码,如果您的 shell 是 bash,这将起作用:
bash foo.sh
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.
但是,我相信 cron 的 shell 是 /bin/sh。即使 /bin/sh 是 bash 的符号链接,当 bash 被调用为 sh 时,它也会尝试以 POSIXly 方式运行:您可能无法使用 BASH_ARGV 数组。
回答by SiegeX
There is no reason to call external binaries such as pwdand dirnamewhen using bash. The functionality of these two binaries can be replicated with pure shell syntax.
没有任何理由来调用外部的二进制文件,如pwd和dirname当使用bash。这两个二进制文件的功能可以用纯 shell 语法复制。
Try the following:
请尝试以下操作:
. foo.sh
回答by Pedro Inácio
When you type,
打字时,
SCRIPTPATH=$(dirname "$_")
you are executing script foo.sh, and bash sets the input argument $0 to the name of the script which is being run.
您正在执行脚本 foo.sh,bash 将输入参数 $0 设置为正在运行的脚本的名称。
When you type,
打字时,
SCRIPTPATH=$(cd `dirname -- ##代码##` && pwd)
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,
您正在获取脚本并且未设置输入参数 $0。在这种情况下,您可以使用包含上次执行命令的参数的自动变量 $_。在你的脚本中,你可以输入,
##代码##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.
获取 foo.sh 的路径。请注意,要使其工作,这必须是文件中执行的第一个命令。否则 $_ 将不包含源脚本的路径。
Kudos to Dennis Williamsonfor providing this answer to a similar question.
回答by Leo
I have used this for a long time without issues.
我已经使用了很长时间没有问题。
##代码##The --disable further processing of parameters.
所述--的参数禁止进一步处理。

