基本上我需要运行与shell脚本文件位置相关的路径脚本,我如何将当前目录更改为脚本文件所在的相同目录?
当前回答
让我们把它变成一个POSIX在线程序:
a="/$0"; a="${a%/*}"; a="${a:-.}"; a="${a##/}/"; BINDIR=$(cd "$a"; pwd)
在包括BSD在内的许多兼容bourne的shell上进行了测试。
据我所知,我是作者,我把它放到了公共领域。更多信息见: https://www.bublina.eu.org/posts/2017-05-11-posix_shell_dirname_replacement/
其他回答
这一行代码告诉shell脚本在哪里,与您是否运行它或是否获取它无关。此外,它还会解析所涉及的任何符号链接,如果是这样的话:
dir=$(dirname $(test -L "$BASH_SOURCE" && readlink -f "$BASH_SOURCE" || echo "$BASH_SOURCE"))
顺便说一下,我认为您正在使用/bin/bash.
假设您正在使用bash
#!/bin/bash
current_dir=$(pwd)
script_dir=$(dirname "$0")
echo $current_dir
echo $script_dir
这个脚本应该打印您所在的目录,然后是脚本所在的目录。例如,当使用/home/mez/中的脚本从/调用它时,它输出
/
/home/mez
请记住,当从命令的输出为变量赋值时,请将命令包装在$(and)中—否则将得不到所需的输出。
之前对一个答案的评论说过,但在所有其他答案中很容易被忽略。
使用bash时:
echo this file: "$BASH_SOURCE"
echo this dir: "$(dirname "$BASH_SOURCE")"
Bash参考手册,5.2 Bash变量
So many answers, all plausible, each with pro's and con's & slightly differeing objectives (which should probably be stated for each). Here's another solution that meets a primary objective of both being clear and working across all systems, on all bash (no assumptions about bash versions, or readlink or pwd options), and reasonably does what you'd expect to happen (eg, resolving symlinks is an interesting problem, but isn't usually what you actually want), handle edge cases like spaces in paths, etc., ignores any errors and uses a sane default if there are any issues.
每个组件都存储在一个单独的变量中,您可以单独使用:
# script path, filename, directory
PROG_PATH=${BASH_SOURCE[0]} # this script's name
PROG_NAME=${PROG_PATH##*/} # basename of script (strip path)
PROG_DIR="$(cd "$(dirname "${PROG_PATH:-$PWD}")" 2>/dev/null 1>&2 && pwd)"
基本版:
dir=$(dirname $0)
如果脚本可以通过$PATH调用,那么:
dir=$(dirname $(which $0))
如果脚本可能像这样调用:bash script.sh,那么:
dir=$(dirname $(which $0 2>/dev/null || realpath ./$0))
如果你感到极度不安全,那么:
dir="$(dirname -- "$(which -- "$0" 2>/dev/null || realpath -- "./$0")")"
推荐文章
- 查看PS命令的全部输出
- 确保一次只运行一个shell脚本实例的快速方法
- 如何从命令行通过mysql运行一个查询?
- 在创建守护进程时执行双fork的原因是什么?
- 如何在Makefile中设置子进程的环境变量
- 如何让“wc -l”打印没有文件名的行数?
- 有效地测试Linux上的端口是否打开?
- 如何从另一个文件A中删除文件B中出现的行?
- 对以制表符分隔的文件进行排序
- Shell脚本删除超过n天的目录
- 如何检查shell脚本中是否存在命令?
- 如何使用查找命令从列表中查找所有具有扩展名的文件?
- 如何打破一个循环在Bash?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- 如何合并2 JSON对象从2个文件使用jq?