summaryrefslogtreecommitdiff
path: root/linux-basics/sh-programming/pres_sh-programming_en.tex
blob: 1e6202f9112691073ccc6f3ea288dbb7a7fcc5ed (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
\input{configpres}

\title{Introduction to Shell Programming}
\maketitle

\begin{frame}
\frametitle{Components of a Script}
\begin{itemize}
\item syntax: "\#!/some/interpreter"
\pause
\item any program can be specified as the interpreter
\pause
\item example: "\#!/bin/sh"
\pause
\item followed by shell commands (for a shell interpreter)
\end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{Hello World as Shell Script}
\begin{lstlisting}
#!/bin/sh

echo 'Hello, world!'
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Variables}
\begin{lstlisting}
#!/bin/sh

# VAR1 defined for this process
VAR1=abc

# set export property of VAR1 for this process
# (now VAR1 is defined for future children processes)
export VAR1

# VAR2 defined only for /bin/sh child process
# (with export property set)
VAR2=def /bin/sh

# clear export property of VAR1 for this process
export -n VAR1

# undefine VAR1 for this process
unset VAR1
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Variables}
\begin{lstlisting}
#!/bin/sh

VAR1=abc
VAR1="a b c"

echo $VAR1
echo ${VAR1}
echo "${VAR1}"
echo '${VAR1}'

VAR1=`date`
VAR1=$(date)
\end{lstlisting}
\end{frame}

\begin{frame}
\frametitle{Special Variables}
\begin{description}
\item[\$0] argv[0]
\item[\$1] argv[1]
\item[\$2] argv[2]
\item[\$\#] argc
\item[\$@] all arguments together as a string
\item[\$\$] my PID
\item[\$?] return value of most recent executed command
\end{description}
\end{frame}

\begin{frame}[fragile]
\frametitle{Example Script}
\begin{lstlisting}
#!/bin/sh

MY_VAR=17
PAR_1=$1
now=`date`

echo variable is ${MYVAR}, parameter is ${PAR_1}
echo date is $now
echo hostname of this machine is $(hostname)
echo 
\end{lstlisting}

\end{frame}

\begin{frame}[fragile]
\frametitle{Output Redirection}
\begin{lstlisting}
#!/bin/sh

# How many lines in addr.txt contain "Jones" ?
# Store the result in "jones_count"
grep Jones addr.txt | wc -l > jones_count

# Append a message to a log file
echo "My message" >> log_file
\end{lstlisting}

\end{frame}

\begin{frame}[fragile]
\frametitle{Conditional Branching}
\begin{lstlisting}
#!/bin/sh

if [ condition ]
then
    ...
else
    ...
fi

# in one line...

if [ condition]; then ...; fi
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Conditional Branching}
\begin{lstlisting}
#!/bin/sh

# number tests: -eq -ne -lt -gt -le -ge
num=15
if [ $num -lt 20 ]; then ...; fi

# string tests: = != < > -n -z
string="Hello"
if [ $string != "hello" ]; then ...; fi
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Conditional Branching}
\begin{lstlisting}
#!/bin/sh

# file tests:
# -f is a normal file
# -d is a directory
# -e exists
# -s exists and is not empty
# -r is readable
# -w is writable
# -x is executable
# -b is a block device
# -c is a character device
filename="myfile"
if [ -f $filename ]; then ...; fi
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Conditional Branching}
\begin{lstlisting}
#!/bin/sh

# all tests can be negated with '!'
filename="myfile"
if [ ! -f $filename ]; then ...; fi

# tests can be combined with a logical and '-a' and or '-o'
filename="myfile"
num=15
string="Hello"
if [ -f $filename -o ! $num -lt 20 -a $string != "hello" ]; then ...; fi
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Conditional Branching}
\begin{lstlisting}
#!/bin/sh

# Does addr.txt contain "Jones" ?
if grep Jones addr.txt; then ...

# Return value 0 means OK
exit 0

\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Conditional Branching}
\begin{lstlisting}
#!/bin/sh

VAR=abcdef

case $VAR in
abc)
    echo "exactly abc"
    ;;
abcdef)
    echo "exactly abcdef"
    ;;
abc*)
    echo "begins with abc"
    ;;
*)
    echo "match everything"
    ;;
esac

\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Conditional Branching}
\begin{lstlisting}
#!/bin/sh

# short circuit tests:

make && make install

grep Jones addr.txt || echo "No Jones found!"

\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Loops}
\begin{lstlisting}
#!/bin/sh

for filename in file1 file2 file3
do
    cp $filename $filename.bak
done

for filename in *
do
    ...
done
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Loops}
\begin{lstlisting}
#!/bin/sh

while [ condition ]
do
    ...
done

while [ condition ]; do ...; done

# "while conditions" are the same as "if conditions"
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Functions}
\begin{lstlisting}
#!/bin/sh

error_log() {
    echo "Error: $1" >> my_log_file
}

...

error_log "file not found"
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Using External Scripts}
\begin{lstlisting}
#!/bin/sh

# include an external script
. /path/to/my_other_script

# run an external script
/path/to/my_other_script
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{Accessing Files}
\begin{lstlisting}
#!/bin/sh

ls -1 > filelist.txt

while read filename
do
    if [ -d $filename ]
    then
        echo "$filename is a directory"
    fi
done < filelist.txt
\end{lstlisting}
\end{frame}

\input{tailpres}