用另一个字符串外壳剥离字符串

时间:2019-04-15 18:39:24

标签: shell sh

我正在运行Shell脚本,并且具有以下字符串:

keystore_location="/mnt/blumeta0/db2/keystore/keystore.p12"如何获取keystore之前的字符串:即/mnt/blumeta0/db2。我知道如何剥离单个字符定界符以及更改密钥库之前的路径。我尝试过:

arrIN=(${keystore_location//\"keystore\"/ })

2 个答案:

答案 0 :(得分:2)

$ keystore_location="/mnt/blumeta0/db2/keystore/keystore.p12"
$ echo "${keystore_location%%/keystore*}"
/mnt/blumeta0/db2

%%/keystore*/keystore*中删除最长匹配的后缀$keystore_location,这是一个全局模式。

答案 1 :(得分:2)

你想要

arrIN=${keystore_location%%keystore*}
echo $arrIn
/mnt/blumeta0/db2/

%%运算符删除最长的匹配项,从字符串的右侧读取

请注意,还有运算符

%   --- remove first match from the right side of the string
#   --- remove first match starting from the left side of the string
##  --- remove longest match starting for the left side of the string.

IHTH