-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathShell-Scripting-1.tex
More file actions
executable file
·2181 lines (2103 loc) · 79.5 KB
/
Shell-Scripting-1.tex
File metadata and controls
executable file
·2181 lines (2103 loc) · 79.5 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[10pt,t]{beamer}
\input{mypreamble}
\beamertemplateballitem
\usetikzlibrary{mindmap,trees}
\usetikzlibrary{decorations.text,calc,arrows.meta}
\usepackage{tabu}
\newcolumntype{a}{>{\columncolor{lulime}}c}
\newcolumntype{b}{>{\columncolor{lulime!50}}c}
\newcolumntype{d}{>{\columncolor{lulime!40}}c}
\newcolumntype{e}{>{\columncolor{lulime}}l}
\newcolumntype{f}{>{\columncolor{lulime!50}}l}
\title{Shell Scripting}
%\subtitle{Variables, Arrays \& Control Constructs}
\author{Alexander B. Pacheco}
\institute{\href{http://researchcomputing.lehigh.edu}{LTS Research Computing}}%\\[2pt] \href{http://www.lehigh.edu}{Lehigh University}}
\date{ }
%\date{September 29 2015}%\today}
% Delete this, if you do not want the table of contents to pop up at
% the beginning of each subsection:
\AtBeginSection[]
{
\begingroup
\setbeamertemplate{background canvas}[vertical shading][bottom=lubrown,top=lubrown]
\setbeamertemplate{footline}[myfootline]
\setbeamertemplate{section page}[mysection]
\frame[c]{
\sectionpage
}
\endgroup
}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\footnotesize
\begin{frame}{Outline}
\tableofcontents
\end{frame}
\section{Introduction}
\begin{frame}[label=day1]
\frametitle{Introduction}
\begin{exampleblock}{What is a SHELL}
\begin{itemize}
\item The command line interface is the primary interface to Linux/Unix operating systems.
\item Shells are how command-line interfaces are implemented in Linux/Unix.
\item Each shell has varying capabilities and features and the user should choose the shell that best suits their needs.
\item The shell is simply an application running on top of the kernel and provides a powerful interface to the system.
\end{itemize}
\end{exampleblock}
\begin{center}
\begin{tikzpicture}[scale=0.65]
\coordinate (O) at (0,0);
\draw[fill=blue!10,blue!10] (O) circle (3.4);
%\onslide<4>{\draw[fill=blue,blue] (O) circle (3.4);}
\draw[fill=blue,blue] (O) circle (2.6);
%\onslide<3>{\draw[fill=blue,blue] (O) circle (2.6);}
\draw[fill=blue!30,blue!30] (O) circle (1.8);
%\onslide<2>{\draw[fill=blue,blue] (O) circle (1.8);}
\draw[fill=blue!40,blue!40] (O) circle (1.0);
\draw[decoration={text along path,reverse path,text
align={align=center},text={Other Software}},decorate] (2.9,0) arc (0:180:2.9);
\draw[decoration={text along path,reverse path,text
align={align=center},text={Shell}},decorate] (2.1,0) arc (0:180:2.1);
\draw[decoration={text along path,reverse path,text
align={align=center},text={Kernel}},decorate] (1.3,0) arc (0:180:1.3);
\draw[decoration={text along path,reverse path,text
align={align=center},text={Hardware}},decorate] (0.6,0) arc (0:180:0.6);
\end{tikzpicture}
\end{center}
\end{frame}
\subsection{Types of Shell}
\begin{frame}
\frametitle{Types of Shell}
\begin{itemize}
\item[\texttt{sh}]: Bourne Shell
\begin{enumerate}
\item[$\vardiamond$] Developed by Stephen Bourne at AT\&T Bell Labs
\end{enumerate}
\item[\texttt{csh}]: C Shell
\begin{enumerate}
\item[$\vardiamond$] Developed by Bill Joy at University of California, Berkeley
\end{enumerate}
\item[\texttt{ksh}]: Korn Shell
\begin{enumerate}
\item[$\vardiamond$] Developed by David Korn at AT\&T Bell Labs
\item[$\vardiamond$] backward-compatible with the Bourne shell and includes many features of the C shell
\end{enumerate}
\item[\texttt{bash}]: Bourne Again Shell
\begin{enumerate}
\item[$\vardiamond$] Developed by Brian Fox for the GNU Project as a free software replacement for the Bourne shell (sh).
\item[$\vardiamond$] Default Shell on Linux and Mac OSX
\item[$\vardiamond$] The name is also descriptive of what it did, bashing together the features of sh, csh and ksh
\end{enumerate}
\item[\texttt{tcsh}]: TENEX C Shell
\begin{enumerate}
\item[$\vardiamond$] Developed by Ken Greer at Carnegie Mellon University
\item[$\vardiamond$] It is essentially the C shell with programmable command line completion, command-line editing, and a few other features.
\end{enumerate}
\end{itemize}
\end{frame}
\begin{frame}[c]
\frametitle{Shell Comparison}
\begin{center}
\taburulecolor{lublue}
\begin{tabular}{a|b|b|b|b|b}
\rowcolor{lublue} & \textbf{sh} & \textbf{csh} & \textbf{ksh} & \textbf{bash} & \textbf{tcsh} \\
Programming Language & \cmark & \cmark & \cmark & \cmark & \cmark \\
Shell Variables & \cmark & \cmark & \cmark & \cmark & \cmark \\
Command alias & \xmark & \cmark & \cmark & \cmark & \cmark \\
Command history & \xmark & \cmark & \cmark & \cmark & \cmark \\
Filename completion & \xmark & \smark & \smark & \cmark & \cmark \\
Command line editing & \xmark & \xmark & \smark & \cmark & \cmark \\
Job control & \xmark & \cmark & \cmark & \cmark & \cmark \\
\end{tabular}
\begin{columns}
\column{0.6\textwidth}
\begin{itemize}
\item[\cmark]: Yes
\item[\xmark]: No
\item[\smark]: Yes, not set by default
\item[] {\fontsize{6}{7}\selectfont\url{http://www.cis.rit.edu/class/simg211/unixintro/Shell.html}}
\end{itemize}
\end{columns}
\end{center}
\end{frame}
\subsection{Variables}
\begin{frame}[fragile,allowframebreaks]
\frametitle{Variables}
\begin{itemize}
\item A variable is a named object that contains data used by one or more applications.
\item There are two types of variables, Environment and User Defined and can contain a number, character or a string of characters.
\item Environment Variables provides a simple way to share configuration settings between multiple applications and processes in Linux.
\item As in programming languages like C, C++ and Fortran, defining your own variables makes the program or script extensible by you or a third party
\item Rules for Variable Names
\begin{enumerate}
\item Variable names must start with a letter or underscore
\item Number can be used anywhere else
\item DO NOT USE special characters such as \texttt{@, \#, \%, \$}
\item Case sensitive
\item Examples
\begin{itemize}
\item Allowed: VARIABLE, VAR1234able, var\_name, \_VAR
\item Not Allowed: 1VARIABLE, \%NAME, \$myvar, VAR@NAME
\end{itemize}
\end{enumerate}
\item To reference a variable, environment or user defined, you need to prepend the variable name with "\$" as in \$VARIABLE, \$PATH, etc.
\item Its a good practice to protect your variable name within \{\dots\} such as \$\{PATH\} when referencing it. (We'll see an example in a few slides)
\item Assigning value to a variable
\begin{center}
\taburulecolor{lublue}
\begin{tabular}{a|b|b}
\rowcolor{lublue}\textbf{Type} & \textbf{sh,ksh,bash} &\textbf {csh,tcsh}\\
Shell & name=value & set name = value \\
Environment & export name=value & setenv name value \\
\end{tabular}
\end{center}
\item \textbf{sh,ksh,bash} THERE IS NO SPACE ON EITHER SIDE OF =
\item \textbf{csh,tcsh} space on either side of = is allowed for the \texttt{set} command
\item \textbf{csh,tcsh} There is no = in the \texttt{setenv} command
\end{itemize}
\end{frame}
\subsection{File Permissions}
\begin{frame}[fragile, allowframebreaks]
\frametitle{File Permissions}
\begin{itemize}
\item In *NIX OS's, you have three types of file permissions
\begin{enumerate}
\item read (r)
\item write (w)
\item execute (x)
\end{enumerate}
\item for three types of users
\begin{enumerate}
\item user
\item group
\item world i.e. everyone else who has access to the system
\end{enumerate}
\begin{exampleblock}{}
\begin{tabular}{lllllllll}
drwxr-xr-x. & 2 & user & user & 4096 & Jan & 28 & 08:27 & Public\\
-rw-rw-r-\,-. & 1 & user & user & 3047 & Jan & 28 & 09:34 & README\\
\end{tabular}
\end{exampleblock}
\item The first character signifies the type of the file
\item[] \Verblubrown{d} for directory
\item[] \Verblubrown{l} for symbolic link
\item[] \Verblubrown{-} for normal file
\item The next three characters of first triad signifies what the owner can do
\item The second triad signifies what group member can do
\item The third triad signifies what everyone else can do
\begin{gather*}
d\underbrace{rwx}_{u}\overbrace{r-x}^g\underbrace{r-x}_o
\end{gather*}
\item Read carries a weight of 4
\item Write carries a weight of 2
\item Execute carries a weight of 1
\item The weights are added to give a value of 7 (rwx), 6(rw), 5(rx) or 3(wx) permissions.
\item \Verblubrown{chmod} is a *NIX command to change permissions on a file
\item To give user rwx, group rx and world x permission, the command is
\item[] \Verblubrown{chmod 751 filename}
\item Instead of using numerical permissions you can also use symbolic mode
\end{itemize}
\begin{description}
\item[u/g/o or a] user/group/world or all i.e. ugo
\item[+/-] Add/remove permission
\item[r/w/x] read/write/execute
\end{description}
\begin{itemize}
\item Give everyone execute permission:
\item[] \Verblubrown{chmod a+x hello.sh }
\item[] \Verblubrown{chmod ugo+x hello.sh}
\item Remove group and world read \& write permission:
\item[] \Verblubrown{chmod go-rw hello.sh}
\item Use the \Verblubrown{-R} flag to change permissions recursively, all files and directories and their contents.
\item[] \Verblubrown{chmod -R 755 \$\{HOME\}/*}
\item[] What is the permission on \$\{HOME\}?
\end{itemize}
\begin{exampleblock}{HPC Users}
If you want to share your files with your colleagues
\begin{enumerate}
\item Make your home directory read accessible to the world
\item[] \Verblubrown{chmod 755 \$\{HOME\}}
\item[] \textsc{do not use the recursive -R flag}
\item Change to your home directory and give read access to the directory that you want to share using the -R flag
\end{enumerate}
\end{exampleblock}
\end{frame}
\subsection{Input and Output}
\begin{frame}[fragile,allowframebreaks]
\frametitle{Input/Output}
\begin{itemize}
\item For reading input from screen/keyboard/prompt
\item[]\textbf{bash} \Verblubrown{read}
\item[]\textbf{tcsh} \Verblubrown{\$<}
\item The \Verblubrown{read} statement takes all characters typed until the \Enter \,key is pressed and stores them into a variable.
\item[]Syntax \Verbblue{read <variable name>}
\item[]Example \Verbblue{read name\Enter}
\item[] \Verbblue{\textit{Alex Pacheco}}
\item \Verblubrown{\$<} can accept only one argument. If you have multiple arguments, enclose the \Verblubrown{\$<} within quotes e.g. \Verblubrown{"\$<"}
\item[]Syntax: \Verbblue{set <variable> = \$<}
\item[]Example: \Verbblue{set name = "\$<"\Enter}
\item[] \Verbblue{\textit{Alex Pacheco}}
\item In the above examples, the name that you enter in stored in the variable \texttt{name}.
\newpage
\item The command \Verblubrown{echo} is used for displaying output to screen
\item Use the \Verblubrown{echo} command to print the variable \texttt{name} to the screen
\item[] \Verbblue{echo \$name\Enter}
\item The \Verblubrown{echo} statement can print multiple arguments.
\item By default, \Verblubrown{echo} eliminates redundant whitespace (multiple spaces and tabs) and replaces it with a single whitespace between arguments.
\item To include redundant whitespace, enclose the arguments within double quotes
\item[] \texttt{echo Welcome to HPC\quad\quad Training}\enter\, (more than one space between HPC and Training)
\item[] \texttt{echo "Welcome to HPC\quad\quad Training"}\enter
\item[] \texttt{read name\enter} or \texttt{set name = "\$<"\enter}
\item[] \textit{Alex\quad\quad\quad Pacheco\enter}
\item[] \texttt{echo \$name\enter}
\item[] \texttt{echo "\$name"\enter}
\framebreak
\item You can also use the \textbf{printf} command to display output
\item[]Syntax: \texttt{printf <format> <arguments>}
\item[]Example: \texttt{printf "\$name"\enter}
\item[] \texttt{printf "\%s$\backslash$n" "\$name"\enter}
\item Format Descriptors
\begin{enumerate}
\item[\%s] print argument as a string
\item[\%d] print argument as an integer
\item[\%f] print argument as a floating point number
\item[$\backslash$n] print new line
\item[] you can add a width for the argument between the \% and \{s,d,f\} fields
\item[] \%4s, \%5d, \%7.4f
\end{enumerate}
\item The \textbf{printf} command is used in \textbf{awk} to print formatted data (more on this later)
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{I/O Redirection}
\begin{itemize}
\item There are three file descriptors for I/O streams
\begin{enumerate}
\item STDIN: Standard Input
\item STDOUT: Standard Output
\item STDERR: Standard Error
\end{enumerate}
\item 1 represents STDOUT and 2 represents STDERR
\item I/O redirection allows users to connect applications
\begin{itemize}
\item[$<$]: connects a file to STDIN of an application
\item[$>$]: connects STDOUT of an application to a file
\item[$> >$]: connects STDOUT of an application by appending to a file
\item[$|$]: connects the STDOUT of an application to STDIN of another application.
\end{itemize}
\item Examples:
\begin{enumerate}
{\footnotesize
\item write STDOUT to file: \texttt{ls -l > ls-l.out }
\item write STDERR to file: \texttt{ls -l 2> ls-l.err }
\item write STDOUT to STDERR: \texttt{ls -l 1>\&2 }
\item write STDERR to STDOUT: \texttt{ls -l 2>\&1 }
\item send STDOUT as STDIN: \texttt{ls -l | wc -l}
}
\end{enumerate}
\end{itemize}
\end{frame}
\section{Shell Scripting}
\begin{frame}
\frametitle{What is a scripting language?}
\begin{exampleblock}{}%{What is a Scripting Language?}
\begin{itemize}
\item A \textbf{scripting language} or \textbf{script language} is a \emph{programming language} that supports the writing of \textbf{scripts}.
\item \textbf{Scripting Languages} provide a higher level of abstraction than standard programming languages.
\item Compared to programming languages, scripting languages do not distinguish between data types: integers, real values, strings, etc.
\item Scripting Languages tend to be good for automating the execution of other programs.
\begin{enumerate}
\item[$\vardiamond$] analyzing data
\item[$\vardiamond$] running daily backups
\end{enumerate}
\item They are also good for writing a program that is going to be used only once and then discarded.
\item A \textbf{script} is a program written for a software environment that automate the execution of tasks which could alternatively be executed one-by-one by a human operator.
\item The majority of script programs are ``quick and dirty'', where the main goal is to get the program written quickly.
\end{itemize}
\end{exampleblock}
\end{frame}
\subsection{Getting Started with Writing Simple Scripts}
\begin{frame}[fragile]
\frametitle{Writing your first script}
\vspace{-0.25cm}
\begin{block}{Three things to do to write and execute a script}
\begin{enumerate}
\item Write a script
\begin{itemize}
\item A shell script is a file that contains ASCII text.
\item Create a file, \texttt{hello.sh} with the following lines
\end{itemize}
\begin{lstlisting}[language=bash]
#!/bin/bash
# My First Script
echo "Hello World!"
\end{lstlisting}
\item Set permissions
\begin{lstlisting}[style=LINUX]
~/Tutorials/BASH/scripts> chmod 755 hello.sh
\end{lstlisting}
\item[] OR
\begin{lstlisting}[style=LINUX]
~/Tutorials/BASH/scripts> chmod a+x hello.sh
\end{lstlisting}
\item Execute the script
\begin{lstlisting}[style=LINUX]
~/Tutorials/BASH/scripts> ./hello.sh
Hello World!
\end{lstlisting}
\item If you do not set execute permission for the script, then
\begin{lstlisting}[style=LINUX]
~/Tutorials/BASH/scripts> sh hello.sh
Hello World!
\end{lstlisting}
\end{enumerate}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Description of the script}
\begin{itemize}
\item My First Script
\begin{lstlisting}[language=bash]
#!/bin/bash
# My First Script
echo "Hello World!"
\end{lstlisting}
\item The first line is called the "ShaBang'' line. It tells the OS which interpreter to use. In the current example, bash
\item Other options are:
\begin{enumerate}
\item[$\vardiamond$] \texttt{sh\quad\,: \#!/bin/sh}
\item[$\vardiamond$] \texttt{ksh : \#!/bin/ksh}
\item[$\vardiamond$] \texttt{csh : \#!/bin/csh}
\item[$\vardiamond$] \texttt{tcsh: \#!/bin/tcsh}
\end{enumerate}
\item The second line is a comment. All comments begin with "\#".
\item The third line tells the OS to print "Hello World!" to the screen.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Special Characters}
\begin{description}
\item[\#:] starts a comment.
\item[\$:] indicates the name of a variable.
\item[$\backslash$:] escape character to display next character literally.
\item[\{\quad\}:] used to enclose name of variable.
\item[;] Command separator [semicolon]. Permits putting two or more commands on the same line.
\item[;;] Terminator in a case option [double semicolon].
\item[.] "dot" command [period]. Equivalent to source. This is a bash builtin.
\item[\$?] exit status variable.
\item[\$\$] process ID variable.
\item[{[\quad]}] test expression
\item[{[[\quad]]}] test expression, more flexible than [ ]
\item[{\$[\quad], ((\quad))}] integer expansion
\item[$||$, \&\&, !] Logical OR, AND and NOT
\end{description}
\end{frame}
\begin{frame}
\frametitle{Quotation}
\begin{itemize}
\item Double Quotation \texttt{" "}
\begin{itemize}
\item Enclosed string is expanded ("\$", "/" and "`")
\item Example: \texttt{echo "\$myvar"} prints the value of \texttt{myvar}
\end{itemize}
\item Single Quotation \texttt{' '}
\begin{itemize}
\item Enclosed string is read literally
\item Example: \texttt{echo '\$myvar'} prints \texttt{\$myvar}
\end{itemize}
\item Back Quotation \texttt{` `}
\begin{itemize}
\item Used for command substitution
\item Enclosed string is executed as a command
\item Example: \texttt{echo `pwd`} prints the output of the \texttt{pwd} command i.e. print working directory
\item In \textbf{bash}, you can also use \texttt{\$($\cdots$)} instead of \texttt{`$\cdots$`}
\item[] e.g. \texttt{\$(pwd)} and \texttt{`pwd`} are the same
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Example}
\lstinputlisting[language=bash]{./scripts/day1/examples/quotes.sh}
\begin{lstlisting}[basicstyle=\tiny\ttfamily,style=LINUX]
~/Tutorials/BASH/scripts/day1/examples> ./quotes.sh
HI
Hello
$HI
Hello
$HI
HelloAlex
/home/apacheco/Tutorials/BASH/scripts/day1/examples
/home/apacheco/Tutorials/BASH/scripts/day1/examples
~/Tutorials/BASH/scripts/day1/examples>
\end{lstlisting}
\end{frame}
\subsection{Arithmetic Operations}
\begin{frame}[fragile,allowframebreaks]
\frametitle{Arithmetic Operations}
\begin{itemize}
\item You can carry out numeric operations on integer variables
% \end{itemize}
\begin{columns}
\column{0.5\textwidth}
\begin{center}
\taburulecolor{lublue}
\begin{tabular}{abb}
\rowcolor{lublue}{\textbf{Operation} }& {\textbf{Operator}} & \\
Addition & + & \\
Subtraction & - & \\
Multiplication & * & \\
Division & / & \\
Exponentiation & ** & (\textbf{bash} only)\\
Modulo & \% & \\
\end{tabular}
\end{center}
\end{columns}
% \begin{itemize}
\item Arithmetic operations in \textbf{bash} can be done within the \texttt{\$(($\cdots$))} or \texttt{\$[$\cdots$]} commands
\begin{enumerate}
\item[$\bigstar$] Add two numbers: \texttt{\$((1+2))}
\item[$\bigstar$] Multiply two numbers: \texttt{\$[\$a*\$b]}
\item[$\bigstar$] You can also use the \texttt{let} command: \texttt{let c=\$a-\$b}
\item[$\bigstar$] or use the \texttt{expr} command: \texttt{c=`expr \$a - \$b`}
\end{enumerate}
\framebreak
\item In \textbf{tcsh},
\begin{enumerate}
\item[$\bigstar$] Add two numbers: \texttt{@ x = 1 + 2}
\item[$\bigstar$] Divide two numbers: \texttt{@ x = \$a / \$b}
\item[$\bigstar$] You can also use the \texttt{expr} command: \texttt{set c = `expr \$a \% \$b`}
\end{enumerate}
\item Note the use of space
\item[\textbf{bash}] space required around operator in the \texttt{expr} command
\item[\textbf{tcsh}] space required between @ and variable, around = and numeric operators.
\item You can also use C-style increment operators
\item[\textbf{bash}] \texttt{let c+=1} or \texttt{let c-{}-}
\item[\textbf{tcsh}] \texttt{@ x -= 1} or \texttt{@ x++}
\item[] \texttt{/=}, \texttt{*=} and \texttt{\%=} are also allowed.
\item[\textbf{bash}]
\item The above examples only work for integers.
\item What about floating point number?
\framebreak
\item Using floating point in \textbf{bash} or \textbf{tcsh} scripts requires an external calculator like GNU \texttt{bc}.
\begin{enumerate}
\item[$\bigstar$] Add two numbers:
\item[]\texttt{echo "3.8 + 4.2" | bc}
\item[$\bigstar$] Divide two numbers and print result with a precision of 5 digits:
\item[]\texttt{echo "scale=5; 2/5" | bc}
\item[$\bigstar$] Call \texttt{bc} directly:
\item[]\texttt{bc $<<<$ "scale=5; 2/5"}
\item[$\bigstar$] Use \texttt{bc -l} to see result in floating point at max scale:
\item[] \texttt{bc -l $<<<$ "2/5"}
\end{enumerate}
\item You can also use \textbf{awk} for floating point arithmetic.
\end{itemize}
\end{frame}
\subsection{Flow Control}
\begin{frame}
\frametitle{Flow Control}
\begin{itemize}
\item Shell Scripting Languages execute commands in sequence similar to programming languages such as C, Fortran, etc.
\item Control constructs can change the sequential order of commands.
\item Control constructs available in \textbf{bash} and \textbf{tcsh} are
\begin{enumerate}
% {\scriptsize
\item Conditionals: \texttt{if}
\item Loops: \texttt{for, while, until}
\item Switches: \texttt{case, switch}
% }
\end{enumerate}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{if} statement}
\begin{itemize}
\item An \texttt{if/then} construct tests whether the exit status of a list of commands is 0, and if so, executes one or more commands.
\begin{columns}
\column{5cm}
\begin{exampleblock}{bash}
\begin{lstlisting}[language=bash]
if [ condition1 ]; then
some commands
elif [ condition2 ]; then
some commands
else
some commands
fi
\end{lstlisting}
\end{exampleblock}
\column{5cm}
\begin{block}{tcsh}
\begin{lstlisting}[language=csh]
if ( condition1 ) then
some commands
else if ( condition2 ) then
some commands
else
some commands
endif
\end{lstlisting}
\end{block}
\end{columns}
\item Note the space between \textit{condition} and "["\quad"]"
\item \textbf{bash} is very strict about spaces.
\item \textbf{tcsh} commands are not so strict about spaces.
\item \textbf{tcsh} uses the \texttt{if-then-else if-else-endif} similar to Fortran.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Comparison Operators}
\begin{center}
\begin{tabular}{ab|b}
\rowcolor{lublue}\multicolumn{3}{c}{Integer Comparison} \\
\rowcolor{lublue}Operation & \textbf{bash} & \textbf{tcsh} \\
equal to & \texttt{if [ 1 -eq 2 ]} & \texttt{if (1 == 2)} \\
not equal to & \texttt{if [ \$a -ne \$b ]} & \texttt{if (\$a != \$b)}\\
greater than & \texttt{if [ \$a -gt \$b ]} & \texttt{if (\$a > \$b)}\\
greater than or equal to & \texttt{if [ 1 -ge \$b ]} & \texttt{if (1 >= \$b)}\\
less than & \texttt{if [ \$a -lt 2 ]} & \texttt{if (\$a < 2)}\\
less than or equal to & \texttt{if [ \$a -le \$b ]} & \texttt{if (\$a <= \$b)} \\
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{ab|b}
\rowcolor{lublue}\multicolumn{3}{c}{String Comparison} \\
\rowcolor{lublue}operation & \textbf{bash} & \textbf{tcsh} \\
equal to & \texttt{if [ \$a == \$b ]} & \texttt{if (\$a == \$b)}\\
not equal to & \texttt{if [ \$a != \$b ]} & \texttt{if (\$a != \$b)}\\
zero length or null & \texttt{if [ -z \$a ] } & \texttt{if (\$\%a == 0)}\\
non zero length & \texttt{if [ -n \$a ] } & \texttt{if (\$\%a > 0)}\\
\end{tabular}
\end{center}
\end{frame}
\begin{frame}
\frametitle{File Test \& Logical Operators}
\begin{center}
\begin{tabular}{ab|b}
\rowcolor{lublue}\multicolumn{3}{c}{File Test Operators} \\
\rowcolor{lublue}Operation & \textbf{bash} & \textbf{tcsh} \\
file exists & \texttt{if [ -e .bashrc ]} & \texttt{if ( -e .tcshrc )}\\
file is a regular file & \texttt{if [ -f .bashrc ]} & \\
file is a directory & \texttt{if [ -d /home ]} & \texttt{if ( -d /home )} \\
file is not zero size & \texttt{if [ -s .bashrc ]} & \texttt{if ( ! -z .tcshrc)} \\
file has read permission & \texttt{ if [ -r .bashrc ]} & \texttt{ if ( -r .tcshrc)} \\
file has write permission & \texttt{ if [ -w .bashrc ]} & \texttt{ if ( -w .tcshrc)} \\
file has execute permission & \texttt{ if [ -x .bashrc ]} & \texttt{ if ( -x .tcshrc)} \\
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{ab|b}
\rowcolor{lublue}\multicolumn{3}{c}{Logical Operators} \\
\rowcolor{lublue}Operation & \textbf{bash} & \textbf{tcsh} \\
Operation & \textbf{bash} & \textbf{tcsh} \\
NOT & \texttt{if [ ! -e .bashrc ]} & \texttt{if ( ! -z .tcshrc)} \\
AND & \texttt{if [ \$a -eq 2 ] \&\& [ \$x -gt \$y ]} & \texttt{if (\$a == 2 \&\& \$x $<=$ \$y )} \\
OR & \texttt{if [[ \$a -eq 2 $||$ \$x -gt \$y ]]} & \texttt{if (\$a == 2 $||$ \$x $<=$ \$y )} \\
\hline
\end{tabular}
\end{center}
\end{frame}
\begin{frame}[fragile]{Examples}
\begin{itemize}
\item Condition tests using the \texttt{if/then} may be nested
\begin{columns}
\column{0.5\textwidth}
\begin{exampleblock}{}
\begin{lstlisting}[language=bash]
read a
if [ "$a" -gt 0 ]; then
if [ "$a" -lt 5 ]; then
echo "The value of \"a\" lies somewhere between 0 and 5"
fi
fi
\end{lstlisting}
\end{exampleblock}
\column{0.45\textwidth}
\begin{block}{}
\begin{lstlisting}[language=csh]
set a = $<
if ( $a > 0 ) then
if ( $a < 5 ) then
echo "The value of $a lies somewhere between 0 and 5"
endif
endif
\end{lstlisting}
\end{block}
\end{columns}
\item This is same as
\begin{columns}
\column{0.5\textwidth}
\begin{exampleblock}{}
\begin{lstlisting}[language=bash]
read a
if [[ "$a" -gt 0 && "$a" -lt 5 ]]; then
echo "The value of $a lies somewhere between 0 and 5"
fi
OR
if [ "$a" -gt 0 ] && [ "$a" -lt 5 ]; then
echo "The value of $a lies somewhere between 0 and 5"
fi
\end{lstlisting}
\end{exampleblock}
\column{0.45\textwidth}
\begin{block}{}
\begin{lstlisting}[language=csh]
set a = $<
if ( "$a" > 0 && "$a" < 5 ) then
echo "The value of $a lies somewhere between 0 and 5"
endif
\end{lstlisting}
\end{block}
\end{columns}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Loop Constructs}
\begin{itemize}
\item A \textit{loop} is a block of code that iterates a list of commands as long as the \textit{loop control condition} is true.
\item Loop constructs available in
\item[\textbf{bash}:] \texttt{for, while} and \texttt{until}
\item[\textbf{tcsh}:] \texttt{foreach} and \texttt{while}
\end{itemize}
\end{frame}
\begin{frame}[fragile]{bash: for loops}
\begin{exampleblock}{}
\begin{itemize}
\item The \texttt{for} loop is the basic looping construct in \textbf{bash}
\begin{lstlisting}[language=bash]
for arg in list
do
some commands
done
\end{lstlisting}
\item the \texttt{for} and \texttt{do} lines can be written on the same line: \texttt{for} \textit{arg} in \textit{list}; \texttt{do}
\item \texttt{for} loops can also use C style syntax
\begin{lstlisting}[language=bash]
for (( EXP1; EXP2; EXP3 )); do
some commands
done
\end{lstlisting}
\end{itemize}
\end{exampleblock}
\begin{columns}
\column{0.3\textwidth}
\begin{exampleblock}{}
\begin{lstlisting}[language=bash]
for i in $(seq 1 10)
do
touch file${i}.dat
done
\end{lstlisting}
\end{exampleblock}
\column{0.35\textwidth}
\begin{exampleblock}{}
\begin{lstlisting}[language=bash]
for i in $(seq 1 10); do
touch file${i}.dat
done
\end{lstlisting}
\end{exampleblock}
\column{0.3\textwidth}
\begin{exampleblock}{}
\begin{lstlisting}[language=bash]
for ((i=1;i<=10;i++))
do
touch file${i}.dat
done
\end{lstlisting}
\end{exampleblock}
\end{columns}
\end{frame}
\begin{frame}[fragile]{tcsh: foreach loop}
\begin{block}{}
\begin{itemize}
\item The \texttt{foreach} loop is the basic looping construct in \textbf{tcsh}
\begin{lstlisting}
foreach arg (list)
some commands
end
\end{lstlisting}
\end{itemize}
\end{block}
\begin{columns}
\column{0.4\textwidth}
\begin{block}{}
\begin{lstlisting}
foreach i (`seq 1 10`)
touch file$i.dat
end
\end{lstlisting}
\end{block}
\end{columns}
\end{frame}
\begin{frame}[fragile]{while Construct}
\begin{itemize}
% \fontsize{7}{9}\selectfont{
\item The \texttt{while} construct tests for a condition at the top of a loop, and keeps looping as long as that condition is true (returns a 0 exit status).
\item In contrast to a \texttt{for} loop, a \texttt{while} loop finds use in situations where the number of loop repetitions is not known beforehand.
\end{itemize}
\vspace{-0.5cm}
\begin{columns}
\column{0.4\textwidth}
\begin{exampleblock}{bash}
\begin{lstlisting}[language=bash]
while [ condition ]
do
some commands
done
\end{lstlisting}
\end{exampleblock}
\column{0.4\textwidth}
\begin{block}{tcsh}
\begin{lstlisting}[language=csh]
while ( condition )
some commands
end
\end{lstlisting}
\end{block}
\end{columns}
\begin{columns}
\column{0.4\textwidth}
\vspace{-0.2cm}
\begin{exampleblock}{factorial.sh}
\lstinputlisting[language=bash]{./scripts/day1/examples/factorial.sh}
\end{exampleblock}
\column{0.4\textwidth}
\vspace{-0.2cm}
\begin{block}{factorial.csh}
\lstinputlisting[language=csh]{./scripts/day1/examples/factorial.csh}
\end{block}
\end{columns}
\end{frame}
\begin{frame}[fragile]{until Contruct (bash only)}
\begin{itemize}
\item The \texttt{until} construct tests for a condition at the top of a loop, and keeps looping as long as that condition is false (opposite of \texttt{while} loop).
\end{itemize}
\begin{columns}
\column{0.4\textwidth}
\begin{exampleblock}{}
\begin{lstlisting}[language=bash]
until [ condition is true ]
do
some commands
done
\end{lstlisting}
\end{exampleblock}
\end{columns}
\begin{columns}
\column{0.4\textwidth}
\begin{exampleblock}{factorial2.sh}
\lstinputlisting[language=bash]{./scripts/day1/examples/factorial2.sh}
\end{exampleblock}
\end{columns}
\end{frame}
\begin{frame}[fragile]{Nested Loops}
\begin{itemize}
\item \texttt{for, while \& until} loops can nested. To exit from the loop use the \texttt{break} command
\end{itemize}
\fontsize{5}{5}\selectfont{
\begin{columns}
\column{0.45\textwidth}
\vspace{-0.2cm}
\begin{exampleblock}{nestedloops.sh}
\lstinputlisting[basicstyle=\fontsize{3.5}{4.5}\selectfont\ttfamily,language=bash]{./scripts/day1/examples/nestedloops.sh}
\end{exampleblock}
\column{0.45\textwidth}
\vspace{-0.2cm}
\begin{block}{nestedloops.csh}
\lstinputlisting[basicstyle=\fontsize{3.5}{4.5}\selectfont\ttfamily,language=csh]{./scripts/day1/examples/nestedloops.csh}
\end{block}
\end{columns}
\framebreak
\begin{columns}
\column{0.45\textwidth}
\vspace{-0.2cm}
\begin{exampleblock}{}
\begin{lstlisting}[basicstyle=\fontsize{4}{5}\selectfont\ttfamily]
~/Tutorials/BASH/scripts/day1/examples> ./nestedloops.sh
Nested for loops
Value of a in outer loop: 1
a * b = 1 * 1 = 1
a * b = 1 * 3 = 3
a * b = 1 * 5 = 5
Value of a in outer loop: 2
a * b = 2 * 1 = 2
a * b = 2 * 3 = 6
2 * 5 > 10
Value of a in outer loop: 3
a * b = 3 * 1 = 3
a * b = 3 * 3 = 9
3 * 5 > 10
Value of a in outer loop: 4
a * b = 4 * 1 = 4
4 * 3 > 10
Value of a in outer loop: 5
a * b = 5 * 1 = 5
5 * 3 > 10
========================
Nested for and while loops
Value of a in outer loop: 1
a * b = 1 * 1 = 1
a * b = 1 * 3 = 3
1 * 5 > 5
Value of a in outer loop: 2
a * b = 2 * 1 = 2
2 * 3 > 5
Value of a in outer loop: 3
a * b = 3 * 1 = 3
3 * 3 > 5
Value of a in outer loop: 4
a * b = 4 * 1 = 4
4 * 3 > 5
Value of a in outer loop: 5
5 * 1 > 5
========================
\end{lstlisting}
\end{exampleblock}
\column{0.45\textwidth}
\vspace{-0.2cm}
\begin{block}{}
\begin{lstlisting}[basicstyle=\fontsize{4}{5}\selectfont\ttfamily]
~/Tutorials/BASH/scripts> ./day1/examples/nestedloops.csh
Nested for loops
Value of a in outer loop: 1
a * b = 1 * 1 = 1
a * b = 1 * 3 = 3
a * b = 1 * 5 = 5
Value of a in outer loop: 2
a * b = 2 * 1 = 2
a * b = 2 * 3 = 6
2 * 5 > 10
Value of a in outer loop: 3
a * b = 3 * 1 = 3
a * b = 3 * 3 = 9
3 * 5 > 10
Value of a in outer loop: 4
a * b = 4 * 1 = 4
4 * 3 > 10
Value of a in outer loop: 5
a * b = 5 * 1 = 5
5 * 3 > 10
========================
Nested for and while loops
Value of a in outer loop: 1
a * b = 1 * 1 = 1
a * b = 1 * 3 = 3
1 * 5 > 5
Value of a in outer loop: 2
a * b = 2 * 1 = 2
2 * 3 > 5
Value of a in outer loop: 3
a * b = 3 * 1 = 3
3 * 3 > 5
Value of a in outer loop: 4
a * b = 4 * 1 = 4
4 * 3 > 5
Value of a in outer loop: 5
5 * 1 > 5
========================
\end{lstlisting}
\end{block}
\end{columns}
}
\end{frame}
\begin{frame}[fragile,allowframebreaks]
\frametitle{Switching or Branching Constructs}
\begin{itemize}
\fontsize{7}{9}\selectfont{
\item The \texttt{case} and \texttt{select} constructs are technically not loops, since they do not iterate the execution of a code block.
\item Like loops, however, they direct program flow according to conditions at the top or bottom of the block.
\begin{columns}
\column{5cm}
\begin{exampleblock}{\texttt{case} construct}
\begin{lstlisting}[language=bash]
case variable in
"condition1")
some command
;;
"condition2")
some other command
;;
esac
\end{lstlisting}
\end{exampleblock}
\column{5cm}
\begin{exampleblock}{\texttt{select} construct}
\begin{lstlisting}[language=csh]
select variable [ list ]
do
command
break
done
\end{lstlisting}
\end{exampleblock}
\end{columns}
\framebreak
\item \texttt{tcsh} has the \texttt{switch} construct
\begin{columns}
\column{5cm}
\begin{exampleblock}{\texttt{switch} construct}
\begin{lstlisting}[language=csh]
switch (arg list)
case "variable"
some command
breaksw
endsw
\end{lstlisting}
\end{exampleblock}
\end{columns}
}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\begin{columns}
\column{0.5\textwidth}
\vspace{-0.5cm}
\begin{exampleblock}{dooper.sh}
\lstinputlisting[basicstyle=\fontsize{4}{4.5}\selectfont\ttfamily,language=bash]{./scripts/day1/examples/dooper.sh}
\end{exampleblock}
\column{0.4\textwidth}