1

I would like to use this json file to get jenkins version and java version numbers to include my script.sh file. How do we do that?

I tried {{user `java_version`}} but it did not work.

  • variable.json file
    {
        "region": "us-east-1",
        "jenkins_version": "2.263.4",
        "java_version": "1.8.0"
    
    }
    
  • script.sh file
    #!/bin/bash
    sudo yum update -y
    sudo yum install wget -y
    sudo yum install   java-{{user `java_version`}}-openjdk-devel -y
    sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
    sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
    sudo yum install -y jenkins-{{user `java_version`}}
    sudo systemctl start jenkins
    sudo systemctl status jenkins
    #Get auth password from jenkins master
    echo "authpwd="$(sudo cat /var/lib/jenkins/secrets/initialAdminPassword)
    
AdminBee
  • 21,637
  • 21
  • 47
  • 71
M.Turan
  • 13
  • 5

1 Answers1

2

Take a look at the jq command it is a command line json parser. You can pull any field you would like.

https://www.systutorials.com/docs/linux/man/1-jq/

For example this will return the java_version number.

cat variable.json | jq .java_version

If you prefer using core commands so you don't have to install anything you can use this command.

cat input | grep java_version | awk '{print $NF}'

Shorter version of the above command before someone says you don't have to cat into grep.

grep java_version variable.json | awk '{print $NF}'

EDIT: Another option would be.

sudo yum install -y jenkins-$(grep java_version variable.json | awk '{print $NF}' | sed 's/"//g')-openjdk
muru
  • 69,900
  • 13
  • 192
  • 292
Jason Croyle
  • 773
  • 2
  • 13
  • Hey jason, thanks for the commands. What i am really looking for to embed it into this line sudo yum install -y jenkins-{{user `java_version`}} how am i supposed to that? – M.Turan Feb 26 '21 at 02:12
  • `sudo yum install -y jenkins-$(cat input | jq .java_version | sed 's/"//g')` This will return something like `jenkins-1.8.0` I am not familiar with how the jenkins packages are named you may have to add `-openjdk` or something like that to the end.i – Jason Croyle Feb 26 '21 at 02:19
  • I added another example to the answer at the bottom it shows how you would add something to the end of the version number if you need to. – Jason Croyle Feb 26 '21 at 02:27
  • Some people might look at commands like this an say there too long or oh I can do that with just awk yes it is possible to do this with just awk but the syntax is much harder to remember and this way I thinks show just why Linux/Unix shine commands that do one thing and do it well. – Jason Croyle Feb 26 '21 at 02:30
  • 1
    You can use `jq`'s `-r` switch to output raw values (without quotes) ex. `jenkins-$(jq -r .java_version variable.json)` – steeldriver Feb 26 '21 at 02:51