Home > Mobile >  How do I get the VersionName from this string?
How do I get the VersionName from this string?

Time:10-14

I have this string:

package: name='my.package.name versionCode='221013140' versionName='00.00.05' platformBuildVersionName='12' platformBuildVersionCode='32' compileSdkVersion='32' compileSdkVersionCodename='12'

Using bash, how can I get this value?

00.00.05

CodePudding user response:

Use parameter expansion:

#!/bin/bash
string="package: name='my.package.name versionCode='221013140' versionName='00.00.05' platformBuildVersionName='12' platformBuildVersionCode='32' compileSdkVersion='32' compileSdkVersionCodename='12'"

version=${string#* versionName=\'}  # Remove everything up to the version name.
version=${version%%\'*}             # Remove everything after the first quote.

echo "$version"

CodePudding user response:

You can use bash regex matching operator (=~):

[[ $string =~ versionName=\'([^\']*)\' ]] && echo "${BASH_REMATCH[1]}"
  •  Tags:  
  • bash
  • Related