Please share your review for course under
INSTAGRAM/ TWITTER – SINGAM4DEVOPS
LINKEDIN – PRAVEEN SINGAMPALLI
Please do share my channel link with your
friends
https://www.youtube.com/praveensingampalli
#By Praveen Singampalli
Shell Scripting commands
1) sed -n '1,3'p test.sh ------> prints 1 to 3 lines [p -> print /n-> number ]
2) awk '{print}' test.sh -------> prints the whole file
3) awk /manager/'{print}' test.sh prints lines with "manager" word
4) awk -F'[.]' '/^version/{print $1"."$2"."$3"."$4+1}' test.sh
version=1.2.3.4 --🡪 Version Increment
5) sed 's/tenth/eleven/g' test.sh -> Replace a value in a file
hello=DevOps=learners
6) echo $hello | sed 's/=/ /g' | awk '{print $2}'
AUTOMATION 1 - IF ELSE BLOCK WITH JFROG REST API
#!/bin/sh
CICD=true
WORKSPACE=/opt/
JOB_BASE_NAME=Test_demo
BUILD_NUMBER=10
if [ $CICD = true ]
then
echo "CI/CD pipe line check"
file="${WORKSPACE}/basic_report.html"
REPORTNAME=${JOB_BASE_NAME}_${BUILD_NUMBER}.Test_demo_10
echo "CICD Check starting"
if [ -f "$file" ]; then
echo "testReport file found sending to artifactory"
curl -H X-JFrog-Art-Api:Token -T $file
https://oneartifactorycloud/artifactory/CICD/Reports/$REPORTNAME.html
else
echo "testReport file found sending to artifactory"
fi
fi
Create a Jira Account
https://www.atlassian.com/software/jira/pricing?&aceid=
&adposition=&adgroup=142327015872&campaign=18442
427682&creative=639487383070&device=c&keyword=jira
%20install&matchtype=p&network=g&placement=&ds_ki
ds=p73343394274&ds_e=GOOGLE&ds_eid=70000000155
8501&ds_e1=GOOGLE&gclid=Cj0KCQiA4uCcBhDdARIsAH5
jyUmXbB8_uFmHJdtBduFO3YqzGJzL6yXFCeBLaAVMe0KD0
BbxJQwkhUUaAl1dEALw_wcB&gclsrc=aw.ds
Get the token:
1) Log in
to https://id.atlassian.com/manage-profile/security/api-to
kens.
2) Click Create API token.
3) From the dialog that appears, enter a memorable and
concise Label for your token and click Create.
4) Login back to https://singam.atlassian.net/ and create a
PROJECT and JIRA
AUTOMATION 2 - For Loop With Jira Rest API
#!/bin/bash
version="1.2.3.4"
for line in $(cat jiraid.txt)
do PREREQUISITE
curl -X PUT -u STEP 1 – create jiraid.txt
"your_atlassian_jira_username:yo
ur_atlassian_jira_token" --data
'{"update":{"labels":[{"add":"DEMO STEP 2 - Add your ticket id
_NEW"}]}}' -H "Content-Type:
application/json" STEP 3 – Create jira.sh and add beside code
https://singam.atlassian.net/rest/ap
i/3/issue/SA-1/comment STEP 4 – Replace the USERNAME and
done TOKEN from your Atlassian account
STEP 5 - chmod +x jira.sh
STEP 6 - Then hit the ./jira.sh script
AUTOMATION 3 - Array Declaration To check Jar
Process
#!/bin/bash
Hands On Practice
array=(helloservice hiservice nameservice managerservice teamservice) STEP 1 – ADD BELOW SCRIPT IN automation3.sh
for line in "${array[@]}"
do
COUNT=`ps -ef | grep helloservice | grep -v grep | wc -l`. RUNNING , 3 array=(helloservice hiservice nameservice managerservice
MAX=2
teamservice)
echo $line for line in "${array[@]}"
echo $COUNT
do
if [ $COUNT -gt $MAX ]
then echo $line
echo $line Done
PROCS=`ps -ef| grep $line | grep -v grep | awk '{print $2, $11, $12, $13}' | sort -k 4`
JAR=`echo "${PROCS}" | awk -F"-Djar_name=| " '{print $5}'`
echo $JAR STEP 2 – chmod +x automation3.sh
JAR_RUN=`echo $JAR | sed 's/ /,/g'`
echo $JAR_RUN
cd /apps/nnos/vzomega/scripts
STEP 4 - ./ automation3.sh
./mail.sh $line $JAR_RUN $COUNT
fi
done
AUTOMATION 4-Archive The Data With
Find/Mtime/Tar/Name CommandPrerequisite
STEP 1 – Create two folders
#!/bin/bash
cd /opt/logs/ mkdir –p /opt/logs
find . -type f -name '*log*' -exec cp '{}' /opt/logs/Log_Backup \; mkdir –p /opt/logs/ Log_Backup
cd /opt/logs/Log_Backup
STEP 2 - Create a log file test.log in folder
find /opt/logs/Log_Backup -type f -name '*log*' > include-file /opt/logs/
tar -cvf $(hostname)_$(date +%Y-%m-%d).tar.gz -T include-file
exit STEP 3 – Copy the beside code in automation4.sh
STEP. 5 – chmod +x automation4.sh
STEP 6 - ./ automation4.sh
AUTOMATION 5 - Playing With Dates And While loop
(FOR MAC)
#!/bin/bash
ONE=14d
Fourteendaysold=`date -v -${ONE}`
echo $Fourteendaysold
TWO=7d
sevendaysold=`date -v -${TWO}`
echo $sevendaysold
echo $Fourteendaysold and $sevendaysold are obtained
AUTOMATION5 - For Linux System
#!/bin/bash
may_date=`date -d '2023-04-01 +30 days' '+%Y-%m-%d'`
echo $may_date
TWO=2
april_date=`date -d '2023-04-03 -2 days' '+%Y-%m-%d'`
echo $april_date
AUTOMATION 6 - SendEmail To Team
/usr/sbin/sendmail -i -t <<
Subject: $1 server process status
From: Email1
To: Email2
Hi Team,
Please check for $1 service in TEST server
which has $3
process running with below list of KIT IDs
$2
Regards,
ShineInCareer
MESSAGE_END
Special Variables in Shell Vim hello.sh
sh hello.sh linkedin place good
echo $# -🡪 3
echo $? 🡪 Exit value of last command
1) $# Stores the number of echo $0. -🡪 Stores the first input [linkedin]
command-line arguments that were
passed to the shell program. echo $0 service in TEST server which has $3 $2
2) $? Stores the exit value of the last CASE 1 :
command that was executed. echo "With *:”
for arg in "$*"; do echo "<$arg>"; done
3)$0 Stores the first word of the CASE 2:
entered command (the name of the echo "With @:”
shell program). for arg in "$@"; do echo "<$arg>"; done
4) $* Stores all the arguments that With *:
were entered on the command line ($1 <linkedin place good>
$2 ...).
5) "$@" Stores all the arguments that
With @:
were entered on the command line, <linkedin>
individually quoted ("$1" "$2" ...). <place>
<good>
AUTOMATION 7 - Creating HTML File
cat > jira.html <<'EOF'
<html>
<body>
<p><strong>MODULE_VERSION - {{module_version}}</strong></p>
STEP 1 – Create jiraid.txt and version.txt
<table border="1" cellspacing="0" cellpadding="2.5" valign="top"
width="100%" align="justify"
STEP 2 – Add the beside shell script to automation7.sh
style="width: 100%; max-width: 1200px; background-color: #ffffff">
<tr> STEP 3 – chmod +x automation7.sh
<th>Key</th>
</tr> STEP 4 - ./automation7.sh
EOF
# MULTIPLE FILES READ
paste jiraid.txt version.txt | while read take; do
cat >> jira.html <<EOF
<tr>
<td>$read</td>
<td>$take</td>
</tr>
EOF
done
AUTOMATION 8 - Sonar/GitLab Rest API
curl -u "GITLAB:$gitlab”
"https://sonar/api/qualitygates/project_status?projectKey=${sonarProjectKe {
y}&branch=${PIPELINE_GIT_BRANCH}" >> status.json
"employee": {
cat status.json | jq '.projectStatus .status' > status.txt
sed 's/"//g' status.txt >> QG.txt "name": "sonoo",
echo "status=$(sed -n '1p' QG.txt)"
"ID": 56000,
if [ $(sed -n '1p' QG.txt) == 'ERROR' ]
then "Status": true
echo -e "*****$RED QUALITY GATE ERROR $NC*****" }
echo "Check https://sonar/dashboard?id=${sonarProjectKey}" }
cat status.json| jq '.employee’
exit 1FAILURE
cat status.json| jq '.employee.name'
else
echo "*****$GREEN QUALITY GATE OKAY $NC******"
echo "To validate check https://sonar/dashboard?id=${sonarProjectKey}"
fi
curl --request POST --header "PRIVATE-TOKEN: $TOKEN"
"https://gitlab/api/v4/projects/$PROJECT_ID/protected_branch
es?name=$MS_TARGET_BRANCH&push_access_level=40&merg
e_access_level=40" >> protectedbranches.txt
AUTOMATION 9 - To Check the process of a
Jar
(java –jar helloworld). Id runtime cpu mem RAM App -🡪 CPU RAM
MEM HARDWARE. (AWK, SED, GREP,PIPE)
• **hello=`ps -ef | grep -i helloworld- 20.1.2.3.jar | awk '{print $13}’`
• sed -i 's/mdreject.html/index.html/g' web.xml
• **sed -i 's/'"$version"'/'"$newversionCode"'/’ test.sh
• date1=`echo $date| sed 's/-//g’`
• **cat branch.json | jq '.branches[0].name' | tr -d '"' >> branch.txt
• ls -ltr | head -10
#Thank you for being the part of my family and let’s grow together