在Shell脚本中,判断字符串是否以特定的字符或子串结尾,可以通过多种方法实现。以下是几种常见的方法,包括使用条件表达式、expr命令以及bash内置的字符串操作功能。 1. 使用条件表达式([[ ... ]])和通配符 这种方法利用了bash的条件表达式和通配符功能,非常直观且易于理解。 sh str="hello.txt" suffix=".txt" if...
shell判断字符串结尾 下面围绕“判断字符串是否以.txt结尾”展开。转变一下也同样适用于“判断字符串是否以.txt开头”。 通用的方法 # 方法一、使用grep命令 #!/bin/sh str="/path/to/foo.txt" # 使用if语句 if echo "$str" | grep -q -E '\.txt$' then echo "true" else echo "false" fi # ...
# 方法一、使用grep命令 #!/bin/sh str="/path/to/foo.txt" # 使用if语句 if echo "$str" | grep -q -E '\.txt$' then echo "true" else echo "false" fi # 写成一行 echo "$str" | grep -q -E '\.txt$' && echo true || echo false grep -q -E '\.txt$' <<< "$str" &&...
「Shell」- 判断字符串结尾 @20210222 下面围绕“判断字符串是否以.txt结尾”展开。转变一下也同样适用于“判断字符串是否以.txt开头”。 通用的方法 # 方法一、使用grep命令 #!/bin/sh str="/path/to/foo.txt" # 使用if语句 if echo "$str" | grep -q -E '\.txt$' then echo "true" else echo ...
「Shell」- 判断字符串结尾 @20210222 下面围绕“判断字符串是否以.txt结尾”展开。转变一下也同样适用于“判断字符串是否以.txt开头”。 通用的方法 # 方法一、使用grep命令 #!/bin/sh str="/path/to/foo.txt" # 使用if语句 if echo "$str" | grep -q -E '\.txt$'...