从每个参数中删除尾部斜杠的最简单方法是什么?

发布于 2024-12-29 06:23:50 字数 184 浏览 1 评论 0 原文

从 '$@' 数组中的每个参数中删除尾部斜杠,以便 rsync 按名称复制目录的最简单方法是什么?

rsync -a --exclude='*~' "$@" "$dir"

为了澄清起见,标题已更改。要了解有关多个尾部斜杠的评论和答案,请参阅编辑历史记录。

What is the simplest way to remove a trailing slash from each parameter in the '$@' array, so that rsync copies the directories by name?

rsync -a --exclude='*~' "$@" "$dir"

The title has been changed for clarification. To understand the comments and answer about multiple trailing slashes see the edit history.

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

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

发布评论

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

评论(10

静谧 2025-01-05 06:23:50

您可以使用 此处。这是一个演示该行为的简单测试脚本:

#!/bin/bash

# Call this as:
#   ./test.sh one/ two/ three/ 
#
# Output:
#  one two three

echo ${@%/}

You can use the ${parameter%word} expansion that is detailed here. Here is a simple test script that demonstrates the behavior:

#!/bin/bash

# Call this as:
#   ./test.sh one/ two/ three/ 
#
# Output:
#  one two three

echo ${@%/}
一桥轻雨一伞开 2025-01-05 06:23:50

接受的答案将修剪一个尾部斜杠。

修剪多个尾部斜杠的一种方法如下:

VALUE=/looks/like/a/path///

TRIMMED=$(echo "$VALUE" | sed 's:/*$::')

echo "$VALUE" "$TRIMMED"

哪个输出:

/looks/like/a/path/// /looks/like/a/path

The accepted answer will trim ONE trailing slash.

One way to trim multiple trailing slashes is like this:

VALUE=/looks/like/a/path///

TRIMMED=$(echo "$VALUE" | sed 's:/*$::')

echo "$VALUE" "$TRIMMED"

Which outputs:

/looks/like/a/path/// /looks/like/a/path
韶华倾负 2025-01-05 06:23:50

这对我有用: ${VAR%%+(/)}

如此处所述 http://wiki.bash-hackers.org/syntax/pattern

可能需要设置 shell 选项 extglob。我看不到它为我启用,但它仍然有效

This works for me: ${VAR%%+(/)}

As described here http://wiki.bash-hackers.org/syntax/pattern

May need to set the shell option extglob. I can't see it enabled for me but it still works

杯别 2025-01-05 06:23:50

realpath 解析给定的路径。除此之外,它还删除了尾部斜杠。使用 -s 防止跟随 simlinks

DIR=/tmp/a///
echo $(realpath -s $DIR)
# output: /tmp/a

realpath resolves given path. Among other things it also removes trailing slashes. Use -s to prevent following simlinks

DIR=/tmp/a///
echo $(realpath -s $DIR)
# output: /tmp/a
情魔剑神 2025-01-05 06:23:50

仅供参考,我根据在 SO 上找到的答案将这两个函数添加到我的 .bash_profile 中。正如 Chris Johnson 所说,所有使用 ${x%/} 的答案仅删除一个斜杠,这些函数将执行它们所说的操作,希望这有用。

rem_trailing_slash() {
    echo "$1" | sed 's/\/*$//g'
}

force_trailing_slash() {
    echo "$(rem_trailing_slash "$1")/"
}

FYI, I added these two functions to my .bash_profile based on the answers found on SO. As Chris Johnson said, all answers using ${x%/} remove only one slash, these functions will do what they say, hope this is useful.

rem_trailing_slash() {
    echo "$1" | sed 's/\/*$//g'
}

force_trailing_slash() {
    echo "$(rem_trailing_slash "$1")/"
}
—━☆沉默づ 2025-01-05 06:23:50

zsh 中,您可以使用 :a 修饰符。

export DIRECTORY='/some//path/name//'

echo "${DIRECTORY:a}"

=> /some/path/name

这与realpath类似,但不会因缺少文件/目录作为参数而失败。

In zsh you can use the :a modifier.

export DIRECTORY='/some//path/name//'

echo "${DIRECTORY:a}"

=> /some/path/name

This acts like realpath but doesn't fail with missing files/directories as argument.

白日梦 2025-01-05 06:23:50

我在修剪用于 rsync 的目录参数时使用的方法,这里使用 dirname 和 basename 来分割路径,然后重新组合各个部分没有尾部斜杠。

raw_dir=/a/b/c/
trim_dir=$(dirname "$raw_dir")"/"$(basename "$raw_dir")

Approach I have used, when trimming directory arguments that are intended for rsync, here using dirname and basename to split the path and then recombining the parts without the trailing slash.

raw_dir=/a/b/c/
trim_dir=$(dirname "$raw_dir")"/"$(basename "$raw_dir")
初懵 2025-01-05 06:23:50

完全符合 POSIX 标准

# recursively remove trailing slashes
remove_slashes() {
    res="${1%/}"
    if [ "$1" = "$res" ]
    then echo "$res"
    else remove_slashes "$res"
    fi
}

# test:
remove_slashes a/b/
remove_slashes a/b////
remove_slashes ""
remove_slashes ///
remove_slashes ///a

Completely POSIX compliant

# recursively remove trailing slashes
remove_slashes() {
    res="${1%/}"
    if [ "$1" = "$res" ]
    then echo "$res"
    else remove_slashes "$res"
    fi
}

# test:
remove_slashes a/b/
remove_slashes a/b////
remove_slashes ""
remove_slashes ///
remove_slashes ///a
寄与心 2025-01-05 06:23:50

记下已接受答案中的几条评论:

  1. 将所有重复的斜杠 //[...] 替换为单个斜杠 / (根据@Dave 评论)
  2. 删除尾部斜杠除非它也是前导斜杠(即根文件路径 /)(根据 @GordonDavisson 评论)
trimSlash() { for s; do sed -E 's://*:/:g; s:(^/)?/*$:\1:' <<< "${s}"; done; }

不像使用参数替换的答案那么简洁,但我认为它值得付出努力。

一些测试用例:

$ trimSlash "/" "///" "a/" "a/a/" "a///a/" "a/a" "a///a" "a///" "/a/a/" "///a///"
/
/
a
a/a
a/a
a/a
a/a
a
/a/a
/a

Taking note of a couple comments in the accepted answer:

  1. Replace all repeated slashes //[...] with a single slash / (per @Dave comment)
  2. Remove trailing slash unless it is also the leading slash (i.e., the root filepath /) (per @GordonDavisson comment)
trimSlash() { for s; do sed -E 's://*:/:g; s:(^/)?/*$:\1:' <<< "${s}"; done; }

Not as concise as the answers using parameter substitution, but I think its worth the diligence.

Some test cases:

$ trimSlash "/" "///" "a/" "a/a/" "a///a/" "a/a" "a///a" "a///" "/a/a/" "///a///"
/
/
a
a/a
a/a
a/a
a/a
a
/a/a
/a
只是我以为 2025-01-05 06:23:50

这不是最漂亮的方式,但又快又简单。

我只是添加一个斜杠并删除所有双打。假设在其他地方找不到这样的模式。

WORD="abc/"
WORD=$WORD'/'
WORD=`echo $WORD | sed s/'\/\/'/''/g`
echo $WORD

Not the most beautiful way, but quick and easy.

I just add a slash and remove all doubles. Assuming such a pattern will not be found elsewhere.

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