0

I'm trying to modify someones script, and there's a certain part of it that's really confusing to me. It looks like below.

if [[ ! -f "${0%/*}/somefile" ]]; then
    echo "Cannot find somefile, quiting..." >&2
    exit 1
else
    source "${0%/*}/somefile"
fi

I know it's checking for the existence of a file, and using this code ${0%/*} to try to get the current directory, however it doesn't run for me. My way of fixing it was to replace it with pwd which works much better.

I'm just wondering if anyone could elaborate to me as to what that portion of the code ${0%/*} was trying to do?

4

1 回答 1

2

它不是试图获取当前目录,而是试图获取包含脚本的目录。$0是脚本的名称,%/*修饰符会删除其中最后一个/的所有内容。

此脚本somefile应存在于安装脚本的同一目录中,而不是您的当前目录中。

于 2013-12-28T02:32:21.223 回答