Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Thursday, 24 December 2015

Automation: Create New Jenkins Job from template config xml file

Below Shell script helps to automatically create a new Jenkins Job from template config XML file.



Createjob.sh


#!/bin/bash
set +x

# To create a job
function create_job {
 mkdir $1
 cp template.xml $1
 mv ./$1/template.xml ./$1/config.xml
 echo Job $1 is created. 
}

#Modify TEMPLATE_SERVER, TEMPLATE_WORKSPACE keywords from template.xml are replaced from 
test.properties
function modify_job {
 . ./test.properties
 echo TEMPLATE_SERVER is replaced with $servername
 echo TEMPLATE_WORKSPACE is replaced with $workspace
 sed -i -- "s/TEMPLATE_SERVER/$servername/g" ./$1/*
 sed -i -- "s/TEMPLATE_WORKSPACE/$workspace/g" ./$1/*
}

# Need to reload disk for new job to be visible in the Jenkins.
function reload_disk {
 $JAVA_HOME/bin/java -jar jenkins-cli.jar -s http://testjenkins.com/ reload-configuration --username testuser --password-file myPwd.txt
}

usage(){
 echo "Usage: $0 filename"
 exit 1
}

[[ $# -eq 0 ]] && usage

# Just to check whether the job exists or not.
if [ -d "$1" ]; then
 echo "job $1 already exists"
 exit
fi
create_job $1
modify_job $1
reload_disk

We can store all properties into a file. Below are the example properties stored in the properties file.

test.properties
servername=testserver20
workspace=testworkspace20

Tuesday, 12 August 2014

Linux and shell script interview questions


  1. Write a script to print the first 10 elements of Fibonacci series?
  2. I want to create a directory such that anyone in the group can create a file and access any person's file in it but none should be able to delete a file other than the one created by himself.
  3. In my bash shell I want my I want my prompt to be of format '$" Present working directory" : "hostname" > and  load a file containing a list of user defined functions as soon as I log in, how will you automate this?
  4. Print the 10th line without using tail and head command.
  5. Given a file, write a command sequence to find the count of each word.
  6. Given a file, replace all occurrence of word "ABC" with "DEF" from 5th line till the end in only those lines that contains word "MNO".
  7. Use sed command to replace the content of the file [if cat file1 ABCD EFGH then output should be EFGH ABCD].
  8. How will you find the 99th line of a file using only tail and head command?
  9. I have 2 files and  I want to print the records which are common to both. Which command should I use?
  10. How will you connect to a database server from linux ?


NOTE:For 3 + years experience.