Forum

Notifications
Clear all

Devops 26-Sept-2024

0
Topic starter

Variable types:
---------------
1. Local variables / user defined variables.
variables created by user

2. system / predefined variables.
variables created by system
env ==> command to check system defined variables

 

script3: write a script to create a variable (sportsman=viratkohli) & verify the variable creation using echo command:
----------------------------------------------------------------------------------------------------------------------
[ec2-user@ip-172-31-6-171 ~]$ cat vars_check.sh
#!/bin/bash
sportsman=viratkohli
echo "my fav sportsamen is $sportsman"

script4: write a script to have employee data coming as variables & verify the variable creation using echo command:
----------------------------------------------------------------------------------------------------------------------
[ec2-user@ip-172-31-93-35 ~]$ cat employee_data.sh

#!/bin/bash
EMP_NAME=alice
EMP_ID=54234
EMP_LOC=bangalore
EMP_STATE=karnataka
echo "Display employee name $EMP_NAME "
echo "Display emloyee id $EMP_ID "
echo "Display employee location $EMP_LOC "
echo "Display employee state $EMP_STATE "

 

script5: script to check harware resources:
-------------------------------------------
[ec2-user@ip-172-31-6-171 ~]$ cat ram_usage_check.sh

#!/bin/bash
echo "Current free ram available is..."

free -m | grep Mem | awk -F " " '{print $4}'
#free -m

echo "current disk usage is..."
df -kh .

echo "number of cpu...."
nproc

 

 

Grep
----
grep is used to search the pattern (keyword) in a file.
if pattern is found, it will print all matched lines /entire line

syntax: grep <pattern_to_search> fileName

Note: Pattern = name =string all are same

---------------------------------------------------
SI City_Name Population State
1 bengaluru 1.4 crore karnataka
2 mumbai 2.5 crore maharashtra
3 newdelhi 1 crore delhi
4 chennai 90 lakhs tn
5 kolkata 1.2 crore westbengal
6 ahmedabad 60 lakhs gujarat
7 hyderabad 70 lakhs andhrapradesh
8 pune 45 lakhs maharashtra
9 Hubli_dharwad 10 lakhs karnataka
10 gurgaon 10 lakhs haryana
---------------------------------------------------

Head
-----
head command is used to display top most part of a file
  syntax: head -5 <fileName> ==> it will show first 5 lines of a file
  
  
  i.print first 15 lines of my file
    head -15 <fileName>
  
  ii. print first 1st line of my file
     head -1 <fileName>
  
  Note:
  -----
  if you dont specify any number, head by default will print first 10 lines
  head <fileName>
  
  
Tail:
-----
tail command is used to display the bottom part of file.
  
  i. to display last 5 lines of a file
  syntax: tail -5 <fileName> 
  
  
  ii. to display very last line of a file
  syntax: tail -1 <fileName>
 
 
echo:
-----
echo is used to print a statement in terminal.
 
syntax: echo "<your_content>"
 
eg: echo "hello all, welcome to devops classes"
 
 
 
 
What is Piping?  
===============
    | 
   A way of connecting commands together. 
   A way of passing data from the output of one command as the input to another command.
 
usecase(i): to print only line number 99 from myFile
        
 
          [ec2-user@ip-172-31-16-223 ~]$ tail -2 numbers_file
          Line 99
          Line 100
          [ec2-user@ip-172-31-16-223 ~]$  
 
observation: tail -2 numbers_file will print last 2 lines of the file. but we need only first line (i.e line 99) from the printed ouput, here i can join 2 commands using piping | which will take input from previous command & generate new output.
 
      [ec2-user@ip-172-31-16-223 ~]$ tail -2 numbers_file | head -1
          Line 99
          [ec2-user@ip-172-31-16-223 ~]$
 
 
 
 
(ii) to see / display 97th line only
    tail -3 numbers_file | head -1
 
 
        (iii) to display 9th line of a file
     head -10 numbers_file | tail -1
 
 
 
 
Grep 
----
grep is used to search the pattern (keyword) in a file.
if pattern is found, it will print all matched lines /entire line
 
syntax: grep <pattern_to_search> fileName
 
Note: Pattern = name =string all are same    
 
 
 
---------------------------------------------------
SI  City_Name         Population    State
1   bengaluru          1.4 crore    karnataka
2   mumbai             2.5 crore    maharashtra
3   newdelhi            1  crore    delhi
4   chennai            90  lakhs    tn
5   kolkata            1.2 crore    westbengal
6   ahmedabad          60  lakhs    gujarat
7   hyderabad          70  lakhs    andhrapradesh
8   pune               45  lakhs    maharashtra
9   Hubli_dharwad      10  lakhs    karnataka
10  gurgaon            10  lakhs    haryana
---------------------------------------------------
 
 
 to search pattern ==> "karnataka" 
 
syntax: grep name/pattern fileName
        grep karnataka census_data
 
grep mumbai census_data
 
grep chennai census_data
 
 
 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
Next class Topics:
------------------
- word count (wc) 
- egrep , vgrep & rgrep
- links(Hard link and soft link)  
- File permissions
 
 
 
 
 
 
 
Note:
------
content for head & tail with pipeing options
 
 
This is Line number 1
This is Line number 2
This is Line number 3
This is Line number 4
This is Line number 5
This is Line number 6
This is Line number 7
This is Line number 8
This is Line number 9
This is Line number 10
This is Line number 11
This is Line number 12
This is Line number 13
This is Line number 14
This is Line number 15
This is Line number 16
This is Line number 17
This is Line number 18
This is Line number 19
This is Line number 20
This is Line number 21
This is Line number 22
This is Line number 23
This is Line number 24
This is Line number 25
This is Line number 26
This is Line number 27
This is Line number 28
This is Line number 29
This is Line number 30
This is Line number 31
This is Line number 32
This is Line number 33
This is Line number 34
This is Line number 35
This is Line number 36
This is Line number 37
This is Line number 38
This is Line number 39
This is Line number 40
This is Line number 41
This is Line number 42
This is Line number 43
This is Line number 44
This is Line number 45
This is Line number 46
This is Line number 47
This is Line number 48
This is Line number 49
This is Line number 50
This is Line number 51
This is Line number 52
This is Line number 53
This is Line number 54
This is Line number 55
This is Line number 56
This is Line number 57
This is Line number 58
This is Line number 59
This is Line number 60
This is Line number 61
This is Line number 62
This is Line number 63
This is Line number 64
This is Line number 65
This is Line number 66
This is Line number 67
This is Line number 68
This is Line number 69
This is Line number 70
This is Line number 71
This is Line number 72
This is Line number 73
This is Line number 74
This is Line number 75
This is Line number 76
This is Line number 77
This is Line number 78
This is Line number 79
This is Line number 80
This is Line number 81
This is Line number 82
This is Line number 83
This is Line number 84
This is Line number 85
This is Line number 86
This is Line number 87
This is Line number 88
This is Line number 89
This is Line number 90
This is Line number 91
This is Line number 92
This is Line number 93
This is Line number 94
This is Line number 95
This is Line number 96
This is Line number 97
This is Line number 98
This is Line number 99
This is Line number 100
 
arithamatic expressions
----------------------------
we need to use expr keyword to do any arithamatic operations in linux
 
syntax: expr <value1> <arithamatic_operator> <value2>
 
arithamatic operators are
    + addition
    - substarction
    / division
    * multiplication
 
Note:
 \ (backward slash) is called as escape charecter in linux : it is used to bypass some functionalities( its used in multiplying with *)
  expr $value1 \* $value2
 
arithamatic_operators
------------------------
 
[ec2-user@ip-172-31-93-35 ~]$ value1=20
[ec2-user@ip-172-31-93-35 ~]$ value2=5
[ec2-user@ip-172-31-93-35 ~]$
[ec2-user@ip-172-31-93-35 ~]$ expr $value1 + $value2
25
[ec2-user@ip-172-31-93-35 ~]$ expr $value1 - $value2
15
[ec2-user@ip-172-31-93-35 ~]$ expr $value1 * $value2
expr: syntax error
[ec2-user@ip-172-31-93-35 ~]$ expr $value1 \* $value2
100
[ec2-user@ip-172-31-93-35 ~]$ expr $value1 / $value2
4
[ec2-user@ip-172-31-93-35 ~]$ expr $value1 % $value2
0
[ec2-user@ip-172-31-93-35 ~]$
 
 
Note:
----------
 
value1=25
value2=15
   to find sum of value1 & value2
     SUM=value1+value2
SUM=25+15
SUM=40
 
In all programming language / scripting first execution will always be R.H.S(right hand side) like how it has been taught in our school
 
 
 
Note:
-----
 
 1. ` ` --> back quoutes, these are used for mathematical opertions 
          eg: SUM=`expr $value1 + $value2`
 
 
 
----------------------------
Hardcoded format 
---------------------
[ec2-user@ip-172-31-93-35 ~]$ cat arithmatic_op.sh
#!/bin/bash
#Author= bharath
value1=25
value2=15
#to find sum of value1 & value2
SUM=`expr $value1 + $value2`
echo " sum of value1 + value2 is $SUM "
 
 
 Dynamic parsing / Command line arguments parsing
 -------------------------------------------------------------
 [ec2-user@ip-172-31-93-35 ~]$ cat arithmatic_op_2.sh
#!/bin/bash
#Author= vijay
#to find sum of value1 & value2
SUM=`expr $1 + $2`
echo " sum of argument_value1 + argument_value2 is $SUM "
[ec2-user@ip-172-31-93-35 ~]$
 
 
[ec2-user@ip-172-31-93-35 ~]$ sh arithmatic_op_2.sh 70 20
echo " sum of argument_value1 + argument_value2 is 90 "
----------------------------------------------------------------------------
 
     
Other important dollar notations used in shell scripts:
-------------------------------------------------------
 
 $# --> To know number of arguments passed to script
 $$ --> To know PID(processID) of the script 
 $* --> To print all arguments passed to script
 $0 --> To Print the name of script which i am execut
6 Answers
0

Task completed.

0

Task Completed.

0

Task completed

0

Task completed.

This post was modified 1 year ago 3 times by RajeevRF
0

Assignment Completed

This post was modified 1 year ago by Karthik
0

Data Organiser

Answer
© Copyright 2024, All rights reserved by HeyCloud Innovations LLP | designed by ColorWhistle | Privacy Policy | Terms and Conditions