-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path105-2-Customize-or-write-simple-scripts
218 lines (167 loc) · 6.85 KB
/
105-2-Customize-or-write-simple-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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
################################################
105.2 Customize or write simple scripts
################################################
Description
Candidates should be able to customize existing scripts, or write simple new Bash scripts.
################################################
Key Knowledge Areas:
################################################
& Use standard sh syntax (loops, tests)
& Use command substitution
& Test return values for success or failure or other information provided by a command
& Perform conditional mailing to the superuser
& Correctly select the script interpreter through the shebang (#!) line
& Manage the location, ownership, execution and suid-rights of scripts
################################################
Terms and Utilities
################################################
& for
& while
& test
& if
& read
& seq
& exec
################################################
Shell Scripts
################################################
Are a way of automating tasks.
################################################
Shebang
################################################
If a line starts with #! it is called shebang and tells the shell what interpreter to use for running this script.
Note: Normally a # at the beginning of a script is for showing comments. Do not confuse it with Shebang (#!)
In many cases we run shells with #!/bin/bash or #!/bin/sh
We can use the command we already know in our shell scripts. A sample is:
#!/bin/bash
echo
echo "We are learning! Wowww..."
echo
################################################
Variables
################################################
Already seen in some parts. You can define variables like this VARNAME=VALUE. A sample:
#!/bin/bash
NAME=mojtaba
echo
echo "$NAME is learning! Wowww..."
echo
Note: you can also do NAME="mojtaba"
################################################
Command substitution
################################################
Sometimes you need to have a variable with the result of something to a variable. In this case you can use $() construct:
FILES=$(ls -1)
################################################
executing scripts
################################################
If the file is executable, we can run them using the ./script_name.sh if we are in the same directory, or give the complete path or include their directory ine $PATH variable. As you can see they are just normal programs.
Another way is giving our scripts name as a parameter to the bash or sh commands.
Note: you know that for making a file executable we can do chmod 755 filename or chmod +x fiename.
################################################
Conditions
################################################
Up to now, we were just running commands one by one. That is not very programmatic. If we are going to have some logic in our programs, we need conditions and loops. First we will cover conditions, using the if command. Its usage is like this:
if [condition]
then
do something
do another thing
else
do new things
even funnier things
fi
Note: else part is optional, if, then, fi is enough.
Conditions can be TRUE or FALSE. A very simple conditions is if [ "Linux" = "Linux" ]. Silly? I know but we will change it soon but for now, learn the syntax! Specially the spaces and = for checking if two strings are equal.
#!/bin/bash
kernel=$(uname -s)
if [ $kernel = "Linux" ]
then
echo YES. You are using a Linux
else
echo "Not a linux :("
fi
Note spaces and using doublequotes (") on seccond echo because it has ( character which will be interpreted by bash if we do not enclose the string in a doublequote.
The actual checking of the condition is done by test command which is writter as [ some test ]. There are the other options:
conditions what is means
"a" = "b" if two strings are equal (here it will return False)
"a" != "b" string a is not equal to string b
4 -lt 40 if 4 is lower than 40 (True)
5 -gt 15 if 5 is greater than 15 (False)
5 -ge 5 if 5 is greater or equal 5
5 -le 3 if 5 is lower or equal to 3
9 -ne 2 9 is not equal with 2 (True)
-f FILENAME if file FILENAME exists
-s FILENAME if file exists and its size is more than 0
-x FILENAME file exists and is executable
################################################
read
################################################
Using read we can read the user input. Look at this:
1 #!/bin/sh
2
3 echo "what is your name?"
4 read NAME
5
6 echo "Hello $NAME"
7
8 if [ $NAME = "mojtaba" ]
9 then
10 echo "Oh I know you!"
11 else
12 echo "I wish I knew you"
13 fi
14
15 echo "Bye"
################################################
for loop
################################################
Generaly loops are used to run a specific set of commands more than once. The syntax is like this:
for VAR in SOME_LIST;
do
some stuff with $VAR
some other stuff
done
Note the in, ;, do and done.
On each loop, the VAR will be equal to one of the SOME_LIST elements. SOME_LIST can be numbers, name of files, words, ...
for NUM in 1 2 3 4 5 6;
do
echo $NUM
done
But what if you needed to go 1 to 42? We have the seq command which can be used like seq 1 42 or a shorthand like {1..42}.
Good part is we can use non-numbers too!
for FILE in $(ls);
do
echo $FILE
wc $FILE
done
################################################
while loop
################################################
This is another kind of loop but loops while a conditions is TRUE. This is the syntax:
while [condition]
do
do something
do anohter thing
done
Note: If your condition will remains true all the time, the while loop will run forever. This is called an infinite loop
This is sample:
VAR=52
while [ $VAR -gt 42 ]
do
echo VAR is $VAR and it is still greater than 42
let VAR=VAR-1
done
we will have an infinite loop if we use let VAR=VAR+1. Ctrl+C will help us to break the loop.
Note the let usage! If you just just say VAR=1 and then VAR=$VAR+1, then VAR will be equal to 1+1 as an string!.
################################################
mailing the root user
################################################
For sending mail, you need to install mailutils. Then the mail command will send emails. You can send the mail to the root user by issuing this command:
mojtaba@funlife:~$ mail root
Cc:
Subject: Hi there root
hello there. This is my mail
And root will get this email. She can read it using mail command.
If you need to send emails in a script, just do:
$ echo "Body!" | mail -s "Subject" root
This can be easily embeded as poart of your scripts!