Skip to content

Commit faba086

Browse files
committed
ifs
1 parent 6ef6bba commit faba086

6 files changed

Lines changed: 78 additions & 34 deletions

File tree

DevOpsTOC.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3829,7 +3829,7 @@ Special variables
38293829
less than or equal to the value of right operand; if yes, then the condition becomes true. [ $a -le $b ] is true.
38303830

38313831
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3832-
o LogicalOperators
3832+
o LogicalOperators
38333833
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38343834
https://opensource.com/article/19/10/programming-bash-logical-operators-shell-expansions
38353835
a AND b
@@ -3883,6 +3883,7 @@ Special variables
38833883
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38843884
o if statement
38853885
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3886+
continue from here
38863887
https://acloudguru.com/blog/engineering/conditions-in-bash-scripting-if-statements
38873888
https://www.tutorialspoint.com/unix/if-elif-statement.htm
38883889
if-statement.sh
@@ -3905,6 +3906,7 @@ Special variables
39053906
o Case statement
39063907
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39073908
https://www.tutorialspoint.com/unix/case-esac-statement.htm
3909+
similar to switch case in java.
39083910
casestatement.sh
39093911
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39103912
o For Loop
@@ -3967,6 +3969,8 @@ done
39673969
https://www.baeldung.com/linux/ifs-shell-variable
39683970
https://bash.cyberciti.biz/guide/$IFS
39693971

3972+
ifs.sh
3973+
39703974
Internal Field Separator (IFS)
39713975

39723976
The special shell variable IFS determines
@@ -3990,10 +3994,17 @@ The special shell variable IFS determines
39903994
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39913995
o Writing Disk Utilization script
39923996
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3997+
https://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html
39933998
https://www.2daygeek.com/linux-shell-script-to-monitor-disk-space-usage-and-send-email/
39943999
https://linoxide.com/check-disk-usage-is-out-of-space/
39954000
https://gist.github.com/M1ke/5300297
4001+
4002+
/usr/bin/cat /proc/loadavg | awk '{print $1}' | awk '{ if($1 > 80) printf("Current CPU Utilization is: %.2f%\n"), $0;}' | mail -s "High CPU Alert" example@gmail.com
4003+
4004+
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }'
4005+
39964006
https://kedar.nitty-witty.com/blog/simple-shell-script-to-monitoring-disk-space-on-a-linux-machine
4007+
diskspace1.sh
39974008
cpumemdisk.sh
39984009
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39994010
o Trouble shooting debugging shell scripts
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
3+
do
4+
echo $output
5+
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
6+
partition=$(echo $output | awk '{ print $2 }' )
7+
if [ $usep -ge 10 ]; then
8+
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
9+
mail -s "Alert: Almost out of disk space $usep%" you@somewhere.com
10+
fi
11+
done

shellScripting/exercises/if-statement.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ fi
3232
#Please refer below url for an explanation on the below examples
3333
# https://acloudguru.com/blog/engineering/conditions-in-bash-scripting-if-statements
3434

35+
regularfile="nestedif.sh"
36+
readablefile="nestedif.sh"
3537
if [ -f regularfile ]; then #-f
3638
echo "Regular file"
3739
fi

shellScripting/exercises/ifs.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,33 @@
66
#WebSite: WIP
77
#Author: Vilas Varghese
88
# START #
9+
10+
#!/bin/bash
11+
12+
string="foo bar foobar"
13+
for i in $string
14+
do
15+
echo "'$i' is the substring"
16+
done
17+
#space is one of the IFS
18+
19+
string="foo\tbar\tfoobar"
20+
21+
for i in $string
22+
do
23+
echo "'$i' is the substring"
24+
done
25+
26+
old_ifs="$IFS"
27+
IFS="\t"
28+
for i in $string
29+
do
30+
echo "'$i' is the substring"
31+
done
32+
33+
934
LINE=`cat /etc/passwd |grep $1`
35+
echo "$LINE"
1036
IFS=:
1137
set $LINE
1238
echo "User Name = $1"
@@ -17,4 +43,6 @@ echo "Description = $5"
1743
echo "Home Directory = $6 "
1844
echo " Current Shell = $7"
1945

46+
47+
2048
# END #

shellScripting/exercises/math-bc.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ echo "a=1; b=2; b>a || a==2;" | bc
66
echo "10.5 + 5" | bc
77
echo "10.5 - 5" | bc
88
echo "10.5 * 5" | bc
9-
echo "10.5 / 5" | bc #result will not reutl precision
9+
echo "10.5 / 5" | bc #result will not result in precision
1010
echo "10.5 % 5" | bc
1111
echo "scale=2;10.5 / 5" | bc #now prevision can be seen
1212

@@ -15,5 +15,5 @@ echo "scale=2;10.5 / 5" | bc #now prevision can be seen
1515
#In order to use bc advanced math libraries (mathlib) you need to use the -l option
1616
#bc -l
1717
num=50
18-
echo "scale=2;sqrt($num)" | bc -l
18+
echo "scale=2;sqrt($num)" | bc -l #precision till second digit scale - 2
1919
echo "scale=2;$num^2" | bc -l
Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
#!/bin/bash
2-
#Purpose: Validate and report Student subject marks
3-
#Version:1.0
4-
#Created Date: Wed May 16 19:00:52 IST 2018
5-
#Modified Date:
6-
#Author: Vilas Varghese
7-
# START #
8-
echo -e "Please Enter Maths Marks: \c"
9-
read -r m
10-
echo -e "Please Enter Physics Marks: \c"
11-
read -r p
12-
echo -e "Please Enter Chemistry Marks: \c"
13-
read -r c
14-
15-
if [ $m -ge 35 -a $p -ge 35 -a $c -ge 35 ]; then
16-
total=`expr $m + $p + $c`
17-
avg=`expr $total / 3`
18-
echo "Total Marks = $total"
19-
echo "Average Marks = $avg"
20-
if [ $avg -ge 75 ]; then
21-
echo "Congrats you got Distinction"
22-
elif [ $avg -ge 60 -a $avg -lt 75 ]; then
23-
echo "Congrats you got First Class"
24-
elif [ $avg -ge 50 -a $avg -lt 60 ]; then
25-
echo "You got second class"
26-
elif [ $avg -ge 35 -a $avg -lt 50 ]; then
27-
echo "You Got Third Class"
28-
fi
1+
echo "Name"
2+
3+
read name
4+
5+
if [ "$name" == "abcd" ]; then
6+
7+
echo "Password"
8+
9+
read password
10+
11+
if [ "$password" == "pwd" ]; then
12+
13+
echo "Hello"
14+
15+
else
16+
17+
echo "Wrong password"
18+
19+
fi
20+
2921
else
30-
echo "Sorry You Failed"
31-
fi
3222

33-
# END #
23+
echo "wrong username"
24+
25+
fi

0 commit comments

Comments
 (0)