Bash_scripting Cheatsheet==================https://devhints.io/bash https://dheeraj3choudhary.com/linux-for-devops-or-shell-script-for-operators-decision-making-statements https://github.com/jidibinlin/Free-DevOps-Books-1/blob/master/book/Linux%20Shell%20Scripting%20Essentials.pdf
============================================ echo "enter your name" read NAME echo "Your name is ${NAME}"
WD=$(pwd) echo "current working dir is ${WD}"
WD=$(pwd) echo "enter the name of new directory" read new_dir_name NEW_DIR_PATH="${WD}/${new_dir_name}" mkdir $NEW_DIR_NAME echo "New dir is created at $NEW_DIR_NAME"
TO CHECK THE LENGTH OF STRING OR THE CONTENTET ASSIGNED UNDER VARIABLE NEW_VARIABLE="this is just a script" echo ${#NEW_VARIABLE}
USE OF FOR LOOP for i in {1..20};do echo "current index is ${i}" done
echo "enter the file you want to read" read file_name cat $file_name | while read line; do echo $line done
USE OF FUNCTION myfunc(){ echo "Good morning .. $1" } myfunc $1
PASSING MULTIPLR VALUES myfunc(){ echo "Good morning .. $1 and $2" } myfunc $1 $2
GET FUNCTION USING LOCAL VARIABLE
getfunction(){
local expectedResult= "some input"
read
WE CANNOT PASS INPUT PARAMETER INSIDE FUNCTION , WE NEED TO USE IT OUTSIDE OF FUNCTION
getfunction(){
local expectedResult= "some input"
read
echo "Enter the name of person" read NAME if [[ NAME != "vijay" ]]; then echo "You are not vijay" fi
echo "Enter the name of person"
read NAME
if [[
Output: Enter the name of person vijay you are vijay
SAMPLE_VARIABLE=$1 if [[ -z $SAMPLE_VARIABLE ]]; then echo "Pass some value in the paramter" elif [[ $SAMPLE_VARIABLE == "na" ]]; then echo "Pass some meaningful value" else echo "Thank you" fi
for HOST in host1 host2 host3; do echo $HOST done
#! /bin/bash for HOST in host{1..20}; do echo $HOST done
#!/bin/bash for file in 'ls /Downloads' ; do echo $file done
Use of $0, $1, $@, $ and $#*
#! /bin/bash
echo "file name: $0"
echo "first parameter: $1"
echo "second parameter:
execute file by giving input as below
Output: file name: ./ega.sh first parameter: vijay second parameter: gawate total values: vijay gawate total values: vijay gawate Total numbers of parameters: 2
*For loop using $ **
#! /bin/bash for TOKEN in $* do echo $TOKEN done
Output: it will print all the content we supply while executing script ./ega.sh vijay is smart
Use of $?
#echo $? it will give status of privious commands or scripts upon its completion if exit status is 0 : then the cmd or script is successful if exit status is 1 : then the cmd or script is unsuccessful
Use of array in scripting
#! /bin/bash
NAME[0]="Vijay"
NAME[1]="Kapil"
NAME[2]="Vishal"
NAME[3]="Suyog"
NAME[4]="Vaibhav"
echo "First index is :
#! /bin/bash
NAME[0]="Vijay"
NAME[1]="Kapil"
NAME[2]="Vishal"
NAME[3]="Suyog"
NAME[4]="Vaibhav"
echo "First index is :
Output: First index is : Vijay Kapil Vishal Suyog Vaibhav Second indes is : Vijay Kapil Vishal Suyog Vaibhav
**Mathematical operators in Shell scripting **
#/bin/bash
val=expr 2 + 2
echo "Total is : $val"
#/bin/bash
a=10
b=30
val=expr $a + $b
echo "Total is : $val"
#/bin/bash a=10 b=30 if [ $a != $b ] then echo "a is not equal to b" fi
#/bin/bash a=10 b=30 if [ $a -lt $b ] then echo "a is not less than b" fi
#!/bin/sh
a=10 b=20
if [ $a -eq $b ] then echo "$a -eq $b : a is equal to b" else echo "$a -eq $b: a is not equal to b" fi
if [ $a -ne $b ] then echo "$a -ne $b: a is not equal to b" else echo "$a -ne $b : a is equal to b" fi
if [ $a -gt $b ] then echo "$a -gt $b: a is greater than b" else echo "$a -gt $b: a is not greater than b" fi
if [ $a -lt $b ] then echo "$a -lt $b: a is less than b" else echo "$a -lt $b: a is not less than b" fi
if [ $a -ge $b ] then echo "$a -ge $b: a is greater or equal to b" else echo "$a -ge $b: a is not greater or equal to b" fi
if [ $a -le $b ] then echo "$a -le $b: a is less or equal to b" else echo "$a -le $b: a is not less or equal to b" fi
Output: 10 -eq 20: a is not equal to b 10 -ne 20: a is not equal to b 10 -gt 20: a is not greater than b 10 -lt 20: a is less than b 10 -ge 20: a is not greater or equal to b 10 -le 20: a is less or equal to b
Boolen operators: #!/bin/sh
a=10 b=20
if [ $a != $b ] then echo "$a != $b : a is not equal to b" else echo "$a != $b: a is equal to b" fi
if [ $a -lt 100 -a $b -gt 15 ] then echo "$a -lt 100 -a $b -gt 15 : returns true" else echo "$a -lt 100 -a $b -gt 15 : returns false" fi
if [ $a -lt 100 -o $b -gt 100 ] then echo "$a -lt 100 -o $b -gt 100 : returns true" else echo "$a -lt 100 -o $b -gt 100 : returns false" fi
if [ $a -lt 5 -o $b -gt 100 ] then echo "$a -lt 100 -o $b -gt 100 : returns true" else echo "$a -lt 100 -o $b -gt 100 : returns false" fi