Isso usa apenas a expansão de parâmetros:
${var%:"${var#*:*:*:*:*:*:*:}"}
Exemplo :
$ var=client-ip:server-ip:gw-ip:netmask:hostname:device:autoconf:morefields:another:youwantanother:haveanother:
$ echo "${var%:"${var#*:*:*:*:*:*:*:}"}"
client-ip:server-ip:gw-ip:netmask:hostname:device:autoconf
Obrigado ilkkachu por encontrar uma correção para o :
!
${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted. If parameter is ‘@’ or ‘’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
Isso tentará corresponder ao início do seu parâmetro e, se isso acontecer, ele será removido.
Exemplo :
$ var=a:b:c:d:e:f:g:h:i
$ echo "${var#a}"
:b:c:d:e:f:g:h:i
$ echo "${var#a:b:}"
c:d:e:f:g:h:i
$ echo "${var#*:*:}"
c:d:e:f:g:h:i
$ echo "${var##*:}" # Two hashes make it greedy
i
${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted. If parameter is ‘@’ or ‘’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
Isso tentará corresponder ao fim do seu parâmetro e, se isso acontecer, ele será removido.
Exemplo :
$ var=a:b:c:d:e:f:g:h:i
$ echo "${var%i}"
a:b:c:d:e:f:g:h:
$ echo "${var%:h:i}"
a:b:c:d:e:f:g
$ echo "${var%:*:*}"
a:b:c:d:e:f:g
$ echo "${var%%:*}" # Two %s make it greedy
a
Então, na resposta:
${var%:"${var#*:*:*:*:*:*:*:}"}
(observe as aspas em torno de ${var#...}
para que seja tratado como uma string literal (não um padrão) a ser removida do final de $var
).
Quando aplicado a:
var=client-ip:server-ip:gw-ip:netmask:hostname:device:autoconf:morefields:another:youwantanother:haveanother:
${var#*:*:*:*:*:*:*:}
= morefields:another:youwantanother:haveanother:
Isso é expandido dentro de ${var%: ... }
da seguinte forma:
${var%:morefields:another:youwantanother:haveanother:}
Então você está dizendo para mim:
client-ip:server-ip:gw-ip:netmask:hostname:device:autoconf:morefields:another:youwantanother:haveanother:
Mas apare :morefields:another:youwantanother:haveanother:
do final.
O Manual de Referência do Bash ( 3.5.3 )