如何在bash中检查字符串中的最后一个字符?

时间:2022-08-30 00:15:27

I need to ensure that the last character in a string is a /

我需要确保字符串中的最后一个字符是/

x="test.com/"

if [[ $x =~ //$/ ]] ; then
        x=$x"extention"
else
        x=$x"/extention"
fi

at the moment, false always fires.

此刻,虚假总是火上浇油。

4 个解决方案

#1


8  

Like this, for example:

像这样,例如:

$ x="test.com/"
$ [[ "$x" == */ ]] && echo "yes"
yes

$ x="test.com"
$ [[ "$x" == */ ]] && echo "yes"
$ 

$ x="test.c/om"
$ [[ "$x" == */ ]] && echo "yes"
$ 

$ x="test.c/om/"
$ [[ "$x" == */ ]] && echo "yes"
yes

$ x="test.c//om/"
$ [[ "$x" == */ ]] && echo "yes"
yes

#2


3  

Your condition was slightly incorrect. When using =~, the rhs is considered a pattern, so you'd say pattern and not /pattern/.

你的病情有点不正确。当使用=〜时,rhs被认为是一种模式,所以你会说模式而不是/ pattern /。

You'd have got expected results if you said

如果你说的话,你会得到预期的结果

if [[ $x =~ /$ ]] ; then

instead of

代替

if [[ $x =~ //$/ ]] ; then

#3


3  

You can index strings in Bash using ${var:index} and ${#var} to get the length of the string. Negative indices means the moving from the end to the start of the string so that -1 is index of the last character:

您可以使用$ {var:index}和$ {#var}在Bash中索引字符串以获取字符串的长度。负索引表示从字符串的结尾移动到开头,因此-1是最后一个字符的索引:

if [[ "${x:${#x}-1}" == "/" ]]; then
    # last character of x is /
fi

#4


0  

You can do this generically using bash substrings $(string:offset:length} - length is optional

你可以使用bash子串$ $(string:offset:length)来执行此操作 - length是可选的

#x is the length of x

#x是x的长度

Therefore

因此

$n = 1       # 1 character
last_char = ${x:${#x} - $n}

For future references,

供将来参考

$ man bash

has all the magic

拥有所有的魔力

${parameter:offset:length}

$ {参数:偏移量:长度}

Substring Expansion. Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset. length and offset are arithmetic expressions ...

子串扩展。从offset指定的字符开始扩展为参数的最大长度字符。如果省略length,则扩展为参数的子字符串,从offset指定的字符开始。长度和偏移量是算术表达式......

#1


8  

Like this, for example:

像这样,例如:

$ x="test.com/"
$ [[ "$x" == */ ]] && echo "yes"
yes

$ x="test.com"
$ [[ "$x" == */ ]] && echo "yes"
$ 

$ x="test.c/om"
$ [[ "$x" == */ ]] && echo "yes"
$ 

$ x="test.c/om/"
$ [[ "$x" == */ ]] && echo "yes"
yes

$ x="test.c//om/"
$ [[ "$x" == */ ]] && echo "yes"
yes

#2


3  

Your condition was slightly incorrect. When using =~, the rhs is considered a pattern, so you'd say pattern and not /pattern/.

你的病情有点不正确。当使用=〜时,rhs被认为是一种模式,所以你会说模式而不是/ pattern /。

You'd have got expected results if you said

如果你说的话,你会得到预期的结果

if [[ $x =~ /$ ]] ; then

instead of

代替

if [[ $x =~ //$/ ]] ; then

#3


3  

You can index strings in Bash using ${var:index} and ${#var} to get the length of the string. Negative indices means the moving from the end to the start of the string so that -1 is index of the last character:

您可以使用$ {var:index}和$ {#var}在Bash中索引字符串以获取字符串的长度。负索引表示从字符串的结尾移动到开头,因此-1是最后一个字符的索引:

if [[ "${x:${#x}-1}" == "/" ]]; then
    # last character of x is /
fi

#4


0  

You can do this generically using bash substrings $(string:offset:length} - length is optional

你可以使用bash子串$ $(string:offset:length)来执行此操作 - length是可选的

#x is the length of x

#x是x的长度

Therefore

因此

$n = 1       # 1 character
last_char = ${x:${#x} - $n}

For future references,

供将来参考

$ man bash

has all the magic

拥有所有的魔力

${parameter:offset:length}

$ {参数:偏移量:长度}

Substring Expansion. Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset. length and offset are arithmetic expressions ...

子串扩展。从offset指定的字符开始扩展为参数的最大长度字符。如果省略length,则扩展为参数的子字符串,从offset指定的字符开始。长度和偏移量是算术表达式......