-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2 Create Simple Shell Scripts
123 lines (81 loc) · 1.87 KB
/
2 Create Simple Shell Scripts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
2.1 : Use scripting to automate system maintenance
------------
bash is a command interpreter
Remove .sh to scripts if you want other programs to execute them
#!/bin/bash - shebang on first line
sample.sh
#!/bin/bash
if test -f /tmp/archive.tar.gz;then
mv /tmp/archive.tar.gz /tmp/archive.tar.gz.OLD
tar acf /tmp/archive.tar.gz /etc/dnf
else
tar acf /tmp/archive.tar.gz /etc/dnf
fi
--
2.2 : Conditional Logic
------------------------
if [ $rocket_status = "failed"]
then
rocker_debug $mission_name
elif [ $rocket_Status = "success" ]
then
echo "This is successful"
else
echo "The state is not failed no success"
fi
Conditional Operators
[ STRING1 = STRING2 ]
= is only used to compare strings
[ "abc" = "abc" ]
!= is not equal
[ "abc" != "abc" ]
numbers equals
[ 5 -eq 5 ]
[ 5 -ne 5 ]
[ 5 -gt 5 ]
[ 5 -lt 5 ]
Enhanced patterns
[[ STRING1 = STRING2 ]]
[[ "abcd" = *bc* ]]
[[ "abc" = ab[cd] ]]
[[ "abe"= "ab[cd]" ]]
[[ "abc" > "bcd" ]]
[[ "abc" < "bcd" ]]
[COND1] && [COND2] - OR OPERATORS
[[ COND1 && COND2 ]]
[ COND1 ] || [ COND2]
[[ COND1 || COND2 ]]
[ -e FILE ] - if file exists
[ -d FILE ] - if directory exist
[ -s FILE ] - if file exist and size > 0
[ -x FILE ] - if the file is executable
[ -w FILE ] - if the file is writeable
2.3 : Processing shell exit codes
----------------------------------
EXIT STATUS = 0 --> SUCCESS
EXIT STATUS > 0 --> FAILURE
$? stores the exit status
2.4: Loops
-----------
for loop:
for mission in <list of missions>
do
create-and-launch-rocket lunar_mission
done
for mission in 'cat mission.txt'
do
create_and_launch_rocket $mission
done
while loop :
while [ $rocket_status = "launching" ]
do
sleep 2
rocket_status=rocket-status $mission_name
done
2.5: Process script inputs
--------------------------
$1, $2
#!/bin/bash
read -p "Enter a mission name:" mission_name
#!/bin/bash
mission_name=$1