String 需要操作字符串变量的帮助吗

String 需要操作字符串变量的帮助吗,string,shell,unix,String,Shell,Unix,我已经编写了一个shell脚本来进行一些处理,并且必须操作一个变量。基本上,变量是这样的-- 我的目的是根据“:”的位置将其分为两个变量。为了得到正确的结果,我正在这样做-- 但是,我无法获取字符串“set policy”的左侧部分。我试过这么做-- 但它不起作用,我得到了整个字符串--“set policy:set cli”。关于如何得到左边的部分有什么想法吗?试试这个 vaa2=${vaa%:*} echo ${vaa2} 你需要改变你的模式 echo ${vaa#*:} # from

我已经编写了一个shell脚本来进行一些处理,并且必须操作一个变量。基本上,变量是这样的--

我的目的是根据“:”的位置将其分为两个变量。为了得到正确的结果,我正在这样做--

但是,我无法获取字符串“set policy”的左侧部分。我试过这么做--

但它不起作用,我得到了整个字符串--“set policy:set cli”。关于如何得到左边的部分有什么想法吗?

试试这个

vaa2=${vaa%:*}
echo ${vaa2}

你需要改变你的模式

echo ${vaa#*:}  
# from the beginning of the string, 
# delete anything up to and including the first :

echo ${vaa%:*}  
# from the end of the string, 
# delete the last : and anything after it
这是如何做到的(bash)

或者读入数组

$ IFS=":"
$ read -a array <<< "$vaa"
$ echo "${array[0]}"
set policy
$ echo "${array[1]}"
set cli
$IFS=“:”

$read-bourne类型Shell中的数组,在“=”周围不能有空格
vaa2=${vaa%:*}
echo ${vaa2}
echo ${vaa#*:}  
# from the beginning of the string, 
# delete anything up to and including the first :

echo ${vaa%:*}  
# from the end of the string, 
# delete the last : and anything after it
$ vaa="set policy:set cli"
$ IFS=":"
$ set -- $vaa
$ echo $1
set policy
$ echo $2
set cli
$ IFS=":"
$ read -a array <<< "$vaa"
$ echo "${array[0]}"
set policy
$ echo "${array[1]}"
set cli