替换bash变量中的换行符?

发布于 2024-12-01 21:39:56 字数 1380 浏览 0 评论 0原文

我试图理解带有 cdargs 包的“cdargs-bash.sh”脚本。我对以下函数有一个疑问:

function _cdargs_get_dir ()
{
local bookmark extrapath
# if there is one exact match (possibly with extra path info after it),
# then just use that match without calling cdargs
if [ -e "$HOME/.cdargs" ]; then
    dir=`/bin/grep "^$1 " "$HOME/.cdargs"`
    if [ -z "$dir" ]; then
        bookmark="${1/\/*/}"
        if [ "$bookmark" != "$1" ]; then
            dir=`/bin/grep "^$bookmark " "$HOME/.cdargs"`
            extrapath=`echo "$1" | /bin/sed 's#^[^/]*/#/#'`
        fi
    fi
    [ -n "$dir" ] && dir=`echo "$dir" | /bin/sed 's/^[^ ]* //'`
fi
if [ -z "$dir" -o "$dir" != "${dir/
/}" ]; then
    # okay, we need cdargs to resolve this one.
    # note: intentionally retain any extra path to add back to selection.
    dir=
    if cdargs --noresolve "${1/\/*/}"; then
        dir=`cat "$HOME/.cdargsresult"`
        /bin/rm -f "$HOME/.cdargsresult";
    fi
fi
if [ -z "$dir" ]; then
    echo "Aborted: no directory selected" >&2
    return 1
fi
[ -n "$extrapath" ] && dir="$dir$extrapath"
if [ ! -d "$dir" ]; then
    echo "Failed: no such directory '$dir'" >&2
    return 2
fi

}

测试的目的是什么:

"$dir" != "${dir/
/}"

这里的测试跨越两行;它是否想删除 $dir 中的换行符或者可能出于其他原因?我刚刚开始学习 bash 脚本,我在 Google 上搜索了一段时间,但找不到任何这样的用法。

I am trying to understand the "cdargs-bash.sh" script with cdargs packages. And I have a question about in the following function:

function _cdargs_get_dir ()
{
local bookmark extrapath
# if there is one exact match (possibly with extra path info after it),
# then just use that match without calling cdargs
if [ -e "$HOME/.cdargs" ]; then
    dir=`/bin/grep "^$1 " "$HOME/.cdargs"`
    if [ -z "$dir" ]; then
        bookmark="${1/\/*/}"
        if [ "$bookmark" != "$1" ]; then
            dir=`/bin/grep "^$bookmark " "$HOME/.cdargs"`
            extrapath=`echo "$1" | /bin/sed 's#^[^/]*/#/#'`
        fi
    fi
    [ -n "$dir" ] && dir=`echo "$dir" | /bin/sed 's/^[^ ]* //'`
fi
if [ -z "$dir" -o "$dir" != "${dir/
/}" ]; then
    # okay, we need cdargs to resolve this one.
    # note: intentionally retain any extra path to add back to selection.
    dir=
    if cdargs --noresolve "${1/\/*/}"; then
        dir=`cat "$HOME/.cdargsresult"`
        /bin/rm -f "$HOME/.cdargsresult";
    fi
fi
if [ -z "$dir" ]; then
    echo "Aborted: no directory selected" >&2
    return 1
fi
[ -n "$extrapath" ] && dir="$dir$extrapath"
if [ ! -d "$dir" ]; then
    echo "Failed: no such directory '$dir'" >&2
    return 2
fi

}

What's the purpose of the testing:

"$dir" != "${dir/
/}"

Here the testing span over two lines; does it want to remove the newline character in $dir or maybe for some other reason? I am just starting to learn bash scripting and I have Googled some time but couldn't find any usage like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2024-12-08 21:39:56

是的,你是对的,它删除了换行符。我认为测试的目的是确保 $dir 不包含多行。

或者,您可以通过删除 \newline

${dir/

这不需要两行,所以我认为它看起来更好。

\n'/}

这不需要两行,所以我认为它看起来更好。

Yes you are right, it removes the newline character. I think the purpose of the test is to make sure $dir doesn't contain multiple lines.

Alternatively, you can remove \newline by

${dir/

This doesn't require two lines so I think it looks better.

\n'/}

This doesn't require two lines so I think it looks better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文