Tuesday, November 16, 2021

[SOLVED] how to get jdk version from the download string in bash script

Issue

I have the below download sting

javaurl=http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x
64.tar.gz

I want to get jdk-8u131-linux-x64.tar.gz so that I can do tar -xvzf on it

so I tried

echo `expr match "$javaurl" '\(.[jdk]*.tar.gz\)'`

but I did not find it

I tried in ansible

name: Download Java to Latest Version
      shell: |
              mkdir /opt/java
              cd /opt/java
              wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie" {{javaurl}}
              tar -xzvf "{{{javaurl##*/}}}"

but it throwing error

"msg": "template error while templating string: unexpected char '#' at 130. String: mkdir /opt/java\ncd /opt/java\nwget -c --header \"Cookie: oraclelicense=accept-securebackup-cookie\" {{javaurl}}\ntar -xzvf \"{{{javaurl##*/}}}\"\n"}

Solution

echo "${javaurl##*/}" should work. It will remove everything upto the last slash and that points to your file name.



Answered By - git-bruh