Home > Software design >  bash | variable in variable
bash | variable in variable

Time:04-06

I want to get version number of tomcat.tar.gz file like this

read -p echo " Enter Version (8 or 9)" version

version8=$(printf -- '%s\n' * | grep -oP 'apache-tomcat-$version\K.*(?=\.tar\.gz)')
version9=$(printf -- '%s\n' * | grep -oP 'apache-tomcat-$version\K.*(?=\.tar\.gz)')

echo $version8 #or better $version${version}, but that doesn't work, too

depending on which version the user entered, I will receive the version number from the gz-file in the current folder.

Example: in my folder are two tar.gz

  • apache-tomcat-8.5.78.tar.gz
  • apache-tomcat-9.0.56.tar.gz

Starting the script:

Enter Version (8 or 9): 8

output should be: 8.5.78

With the above code I am getting nothing. What's wrong with it? I suspect it is due to the variable (version) within a variable (version8). How is it syntactically correct?

CodePudding user response:

I have this working with awk if you are interested, as follows:

read -p "Enter Version (8 or 9) " version

ls *.gz | awk "{split(\$0,a,\"-\"); split(a[3],b,\".\"); split(a[3],c,\".tar\"); if (b[1] == \"$version\") {print c[1]}}"
  • Related