Nothing Special   »   [go: up one dir, main page]

SESSION5 ShellScripting

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

#SHELL SCRIPTING 10 REAL TIME

SCENARIOS

By Praveen Singampalli [09-07-2022 ]

-----FOLLOW ME FOR BOOTCAMP UPDATES----

INSTAGRAM/ TWITTER – SINGAM4DEVOPS

LINKEDIN – PRAVEEN SINGAMPALLI

TELEGRAM - SINGAM4DEVOPS_DISCUSS

Please do share my channel link with 10 of your


friends
https://www.youtube.com/channel/
UCBz4yaxNxfiz1XYh-07UfWQ
Shell Scripting commands
1) sed -n '1,3'p test.sh ------> prints 1 to 3 lines
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=20.1.2.3 -- Version Incerement
5) sed -i ‘s/tenth/eleven/g’ test.sh -> Replace a value in a file
6) myVar=`echo $hello | sed 's/=/ /g' | awk '{print $2}'`
AUTOMATION 1 - IF ELSE BLOCK WITH JFROG REST API
CICD=true
WORKSPACE=/apps/opt/users/
JOB_BASE_NAME=Test_demo
if [[ $CICD == true ]]
Then
echo "CI/CD pipe line check"
file="${WORKSPACE}/html/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”. -T-- Target
curl -H X-JFrog-Art-Api:Token -T $file https://oneartifactorycloud/artifactory/CICD/Reports/$REPORTNAME.html
curl -H X-JFrog-Art-Api:Token -T $file https://oneartifactorycloud/artifactory/CICD/Reports/$REPORTNAME.html
else
echo "testReport not found"

fi
fi
AUTOMATION 2 - For Loop With Jira Rest API

for ip in $(cat ip.txt) 10.118,10.119 vim forloop.sh


do for line in $(cat test.sh)

curl -u GITLAB:Token -X PUT --data do


'{"update":{"labels":[{"add":”’”$TEAMNAME- echo $line
$version"'"}]}}' --header "Content-Type: done
application/json"
https://jira.com/rest/api/2/issue/2341 sh forloop.sh
curl -u GITLAB:Token -X PUT --data '{"update": IN CLI
{"comment":[{"add":
{"body":"'"$version"'"}}]}}' --header "Content- $ip
Type: application/json" https://jira. Echo $ip
10.118
com/rest/api/2/issue/$line
echo "Name read from file - $line" 2341
3555
done 67778
AUTOMATION 3 - Array Declaration To check Jar Process
• #!/bin/bash
array=(helloservice hiservice nameservice managerservice teamservice)
for line in "${array[@]}" array=(helloservice hiservice nameservice managerservice
do teamservice)
COUNT=`ps -ef | grep helloservice | grep -v grep | wc -l`. RUNNING , 3 for line in "${array[@]}"
MAX=2
do
echo $line
echo $COUNT
echo $line
if [ $COUNT -gt $MAX ] done
then
echo $line
Testfilename - arrayloop.sh
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}'` IN CLI
echo $JAR
JAR_RUN=`echo $JAR | sed 's/ /,/g'`
echo $JAR_RUN
cd /apps/nnos/vzomega/scripts
./mail.sh $line $JAR_RUN $COUNT
fi
AUTOMATION 4-Archive The Data With
Find/Mtime/Tar/Name Command

cd /apps/logs/backup.txt
find . -type f -mtime +7-exec mv '{}' /apps/logs/Log_Backup \;
cd /apps/logs/Log_Backup
find /apps/logs/Log_Backup -type f -name '*log*' > include-file
tar -cvf $(hostname)_$(date +%Y%m%d%H%M%S).tar.gz -T include-file

find -type f -name '*log*' -exec rm -rf {} \;


find -type f -mtime +3 -name '*.tar.gz' -exec rm '{}' \;
exit
AUTOMATION 5 - Playing With Dates And While loop
>dataset_date=`date`
Fourteendaysold=`date -d
"$dataset_date - $ONE days" + #!/bin/sh
%F` a=0
echo $Fourteendaysold while [ $a -lt 10 ]
TWO=7 do
dataset_date=`date` echo $a
sevendaysold=`date -d a=`expr $a + 1`
"$dataset_date - $TWO days" + done
%F` IN TERMINAL
echo $sevendaysold
date1=`echo $sevendaysold | sed
's/-//g’`
date2=`echo $Fourteendaysold |
sed 's/-//g'`
echo $date1 with $date2
AUTOMATION 6 - SendEmail To Team
Vim hello.sh
/usr/sbin/sendmail -i -t <<
Subject: $1 server process status echo $1 service in TEST server which has $3 $2
From: Email1
To: Email2 sh hello.sh linkedin place good
Hi Team, IN CLI
Please check for $1 service in TEST server which
has $3
process running with below list of KIT IDs
$2
Regards,
ShineInCareer
MESSAGE_END
Vim hello.sh
Special Variables in Shell sh hello.sh linkedin place good
echo $# - 3
echo $?  Exit value of last command
1) $# Stores the number of command- echo $0. - Stores the first input [linkedin]
line arguments that were passed to the
echo $0 service in TEST server which has $3 $2
shell program.
2) $? Stores the exit value of the last CASE 1 :
echo "With *:”
command that was executed. for arg in "$*"; do echo "<$arg>"; done
3)$0 Stores the first word of the CASE 2:
echo "With @:”
entered command (the name of the for arg in "$@"; do echo "<$arg>"; done
shell program).
With *:
4) $* Stores all the arguments that <linkedin place good>
were entered on the command line ($1
$2 ...).
With @:
5) "$@" Stores all the arguments that <linkedin>
were entered on the command line, <place>
<good>
individually quoted ("$1" "$2" ...).
cat > html <<'EOF'
<html> AUTOMATION 8 - Creating HTML File
<body>
<p><strong>MODULE_VERSION -
{{module_version}}</strong></p>
<table border="1" cellspacing="0" cellpadding="2.5"
valign="top"
width="100%" align="justify"
style="width: 100%; max-width: 1200px; background-color:
#ffffff">
<tr>
<th>Key</th>
</tr>
EOF
Paste test.sh test2.txt tesxt100.txt | while read key dog
zebra; do
cat >> jira.html <<EOF
<tr>
<td>$ key </td>
</tr>
EOF
done
AUTOMATION 9 - Sonar/GitLab Rest
API {
curl -u "GITLAB:$gitlab”
"https://sonar/api/qualitygates/project_status? "employee": {
projectKey=${sonarProjectKey}&branch=$
{PIPELINE_GIT_BRANCH}" >> status.json
"name": "sonoo",
cat status.json | jq '.projectStatus .status' > status.txt
sed 's/"//g' status.txt >> QG.txt "ID": 56000,
echo "status=$(sed -n '1p' QG.txt)"
"Status": true
if [ $(sed -n '1p' QG.txt) == 'ERROR' ]
then }
echo -e "*****$RED QUALITY GATE ERROR $NC*****"
echo "Check https://sonar/dashboard?id=$ }
{sonarProjectKey}"
exit 1 cat status.json| jq '.employee’
cat status.json| jq '.employee.name'
else
echo "*****$GREEN QUALITY GATE OKAY $NC******"
curl --request POST --header "PRIVATE-TOKEN: $TOKEN"
echo "To validate check https://sonar/dashboard?id=$ "https://gitlab/api/v4/projects/$PROJECT_ID/protected_branche
{sonarProjectKey}"
s?
fi name=$MS_TARGET_BRANCH&push_access_level=40&merge_a
ccess_level=40" >> protectedbranches.txt
AUTOMATION 10 - 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
#DevOps Help
#LiveSessions
#Referral
#OneToOne Sessions (320+ calls
in which 200+ were free)
#Guided more than 2500+
DevOps Engineers
#JobHelp
#ResumeBuilding
#InterviewExperience
#Many Successful Stories and
testimonials 
#Thank you for being the part of my family and let’s grow
together

You might also like