-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-scripting.html
1004 lines (915 loc) · 45.2 KB
/
bash-scripting.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>Shell Scripting</title>
<meta content="" name="description">
<meta content="" name="keywords">
<!-- Favicons -->
<link href="assets/img/Favicon-1.png" rel="icon">
<link href="assets/img/Favicon-1.png" rel="apple-touch-icon">
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,500,500i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">
<!-- Vendor CSS Files -->
<link href="assets/vendor/aos/aos.css" rel="stylesheet">
<link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
<link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
<link href="assets/vendor/glightbox/css/glightbox.min.css" rel="stylesheet">
<link href="assets/vendor/swiper/swiper-bundle.min.css" rel="stylesheet">
<!-- Creating a python code section-->
<link rel="stylesheet" href="assets/css/prism.css">
<script src="assets/js/prism.js"></script>
<!-- Template Main CSS File -->
<link href="assets/css/style.css" rel="stylesheet">
<!-- To set the icon, visit https://fontawesome.com/account-->
<script src="https://kit.fontawesome.com/5d25c1efd3.js" crossorigin="anonymous"></script>
<!-- end of icon-->
<!-- Include Prism styles -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/themes/prism.min.css">
<!-- Include Prism scripts -->
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/components/prism-core.min.js" integrity="sha384-uEbBqWkE4LEVKVKJBii7Pb3pDi8svYlmL8jq5vHEewoYW6hDyyMuwQX+FRQ0PK5c" crossorigin="anonymous"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/plugins/autoloader/prism-autoloader.min.js" integrity="sha384-uEbBqWkE4LEVKVKJBii7Pb3pDi8svYlmL8jq5vHEewoYW6hDyyMuwQX+FRQ0PK5c" crossorigin="anonymous"></script>
<!-- =======================================================
* Template Name: iPortfolio
* Updated: Sep 18 2023 with Bootstrap v5.3.2
* Template URL: https://bootstrapmade.com/iportfolio-bootstrap-portfolio-websites-template/
* Author: BootstrapMade.com
* License: https://bootstrapmade.com/license/
======================================================== -->
</head>
<body>
<!-- ======= Mobile nav toggle button ======= -->
<i class="bi bi-list mobile-nav-toggle d-xl-none"></i>
<!-- ======= Header ======= -->
<header id="header">
<div class="d-flex flex-column">
<div class="profile">
<img src="assets/img/myphoto.jpeg" alt="" class="img-fluid rounded-circle">
<h1 class="text-light"><a href="index.html">Arun</a></h1>
<div class="social-links mt-3 text-center">
<a href="https://www.linkedin.com/in/arunp77/" target="_blank" class="linkedin"><i class="bx bxl-linkedin"></i></a>
<a href="https://github.com/arunp77" target="_blank" class="github"><i class="bx bxl-github"></i></a>
<a href="https://twitter.com/arunp77_" target="_blank" class="twitter"><i class="bx bxl-twitter"></i></a>
<a href="https://www.instagram.com/arunp77/" target="_blank" class="instagram"><i class="bx bxl-instagram"></i></a>
<a href="https://arunp77.medium.com/" target="_blank" class="medium"><i class="bx bxl-medium"></i></a>
</div>
</div>
<nav id="navbar" class="nav-menu navbar">
<ul>
<li><a href="index.html#hero" class="nav-link scrollto active"><i class="bx bx-home"></i> <span>Home</span></a></li>
<li><a href="index.html#about" class="nav-link scrollto"><i class="bx bx-user"></i> <span>About</span></a></li>
<li><a href="index.html#resume" class="nav-link scrollto"><i class="bx bx-file-blank"></i> <span>Resume</span></a></li>
<li><a href="index.html#portfolio" class="nav-link scrollto"><i class="bx bx-book-content"></i> <span>Portfolio</span></a></li>
<li><a href="index.html#skills-and-tools" class="nav-link scrollto"><i class="bx bx-wrench"></i> <span>Skills and Tools</span></a></li>
<li><a href="index.html#language" class="nav-link scrollto"><i class="bi bi-menu-up"></i> <span>Languages</span></a></li>
<li><a href="index.html#awards" class="nav-link scrollto"><i class="bi bi-award-fill"></i> <span>Awards</span></a></li>
<li><a href="index.html#professionalcourses" class="nav-link scrollto"><i class="bx bx-book-alt"></i> <span>Professional Certification</span></a></li>
<li><a href="index.html#publications" class="nav-link scrollto"><i class="bx bx-news"></i> <span>Publications</span></a></li>
<!-- <li><a href="index.html#extra-curricular" class="nav-link scrollto"><i class="bx bx-rocket"></i> <span>Extra-Curricular Activities</span></a></li> -->
<!-- <li><a href="#contact" class="nav-link scrollto"><i class="bx bx-envelope"></i> <span>Contact</span></a></li> -->
</ul>
</nav><!-- .nav-menu -->
</div>
</header><!-- End Header -->
<main id="main">
<!-- ======= Breadcrumbs ======= -->
<section id="breadcrumbs" class="breadcrumbs">
<div class="container">
<div class="d-flex justify-content-between align-items-center">
<h2>Bash</h2>
<ol>
<li><a href="Data-engineering.html" class="clickable-box"><i class="fas fa-arrow-left"></i> Content </a></li>
<li><a href="index.html#portfolio" class="clickable-box"> Portfolio section <i class="fas fa-arrow-right"></i> </a></li>
</ol>
</div>
</div>
</section><!-- End Breadcrumbs -->
<!------ right dropdown menue ------->
<div class="right-side-list">
<div class="dropdown">
<button class="dropbtn"><strong>Shortcuts:</strong></button>
<div class="dropdown-content">
<ul>
<li><a href="cloud-compute.html"><i class="fas fa-cloud"></i> Cloud</a></li>
<li><a href="AWS-GCP.html"><i class="fas fa-cloud"></i> AWS-GCP</a></li>
<li><a href="amazon-s3.html"><i class="fas fa-cloud"></i> AWS S3</a></li>
<li><a href="ec2-confi.html"><i class="fas fa-server"></i> EC2</a></li>
<li><a href="Docker-Container.html"><i class="fab fa-docker" style="color: rgb(29, 27, 27);"></i> Docker</a></li>
<li><a href="Jupyter-nifi.html"><i class="fab fa-python" style="color: rgb(34, 32, 32);"></i> Jupyter-nifi</a></li>
<li><a href="snowflake-task-stream.html"><i class="fas fa-snowflake"></i> Snowflake</a></li>
<li><a href="data-model.html"><i class="fas fa-database"></i> Data modeling</a></li>
<li><a href="sql-basics.html"><i class="fas fa-table"></i> QL</a></li>
<li><a href="sql-basic-details.html"><i class="fas fa-database"></i> SQL</a></li>
<li><a href="Bigquerry-sql.html"><i class="fas fa-database"></i> Bigquerry</a></li>
<li><a href="scd.html"><i class="fas fa-archive"></i> SCD</a></li>
<li><a href="sql-project.html"><i class="fas fa-database"></i> SQL project</a></li>
<!-- Add more subsections as needed -->
</ul>
</div>
</div>
</div>
<!-- ======= Portfolio Details Section ======= -->
<section id="portfolio-details" class="portfolio-details">
<div class="container">
<div class="row gy-4">
<div class="col-lg-8">
<div class="portfolio-details-slider swiper">
<div class="swiper-slide">
<h1>Shell Scripting Tutorial</h1>
<figure>
<img src="assets/img/data-engineering/terminal-bash-svgrepo-com.svg" class="img-fluid" alt="" style="max-width: 35%; height: auto;">
<figcaption></figcaption>
</figure>
</div>
</div>
</div>
<div class="col-lg-4 grey-box">
<div class="section-title">
<h3>Content</h3>
<ol>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#chapter-1">Chapter-1: Various shell</a></li>
<li><a href="#chapter-2">Chapter-2: Some common terminologies</a></li>
<li><a href="#chapter-3">Chapter-3: Execution</a></li>
<li><a href="#chapter-4">Chapter-4: Example scripts</a></li>
<li><a href="#chapter-5">Chapter-5: Variables</a></li>
<li><a href="#chapter-6">Chapter-6: Loop</a></li>
<li><a href="#chapter-7">Chapter-7: Test</a></li>
<li><a href="#chapter-8">Chapter-8: The case statement</a></li>
<li><a href="#chapter-9">Chapter-9: Bash command references</a></li>
<li><a href="#reference">References</a></li>
</ol>
</div>
</div>
</div>
<!------Section-1 ---------->
<section id="introduction">
<h2>Introduction</h2>
The shell script programming (aka shell scripting), is a powerful skill for automating tasks in a command-line environment. It's great for streamlining repetitive tasks and managing system processes.
</section>
<!-------- Section-2 -------->
<section id="chapter-1">
<h2>Various shell</h2>
There are various types of shells, each with its own features and capabilities. Here are some of the commonly used types of shells:
<ul>
<li><strong>Bourne Shell (sh): </strong>The original Bourne shell, developed by Stephen Bourne. It is a simple and efficient shell, but it lacks some of the features found in more modern shells. The Bourne shell is a command-line interpreter or shell that was the original Unix shell. </li>
<li><strong>Bash (Bourne Again SHell): </strong>Bash is one of the most popular and widely used shells. It is an enhanced version of the original Bourne shell, providing additional features like command history, job control, and improved scripting capabilities. Bash is the default shell for many Unix-like operating systems.</li>
<li><strong>Zsh (Z Shell): </strong>Zsh is an extended Bourne shell with features from Bash, Korn shell, and others. It includes advanced tab completion, spelling correction, and improved scripting capabilities. Zsh is highly customizable and user-friendly.</li>
<li><strong>PowerShell: </strong>Developed by Microsoft, PowerShell is a task automation framework and shell. It is especially prevalent in Windows environments but is also available for Linux and macOS.</li>
<li><strong>Korn Shell (ksh): </strong>The Korn shell was developed by David Korn as a superset of the Bourne shell with additional features. It includes features like command-line editing, history, and associative arrays. There are two main versions: ksh88 and ksh93.</li>
<li><strong>Dash: </strong>Dash is a minimalistic POSIX-compliant shell, designed to be lightweight and fast. It is often used as the default system shell on some Unix-like systems for scripts and system utilities.</li>
<li><strong>Ash (Almquist Shell): </strong>Ash is a lightweight shell that aims to be POSIX-compliant. It is often used in embedded systems and is known for its small memory footprint.</li>
<li><strong>Fish (Friendly Interactive SHell): </strong>Fish is designed to be user-friendly and interactive. It provides features like syntax highlighting, autosuggestions, and a consistent command syntax. While not POSIX-compliant, it offers a unique and intuitive user experience.</li>
</ul>
These shells vary in terms of features, syntax, and use cases. The choice of which shell to use often depends on personal preference, system requirements, and specific features needed for scripting or interactive use. If you have a specific context or use case in mind, feel free to ask for more tailored advice!
</section>
<section id="Chapter-2">
<h2>Some common terminologies</h2>
Scripting involves a variety of terminology that is commonly used across different programming and scripting languages. Here are some key terms:
<ul>
<li><strong>Script: </strong>A script is a series of commands or instructions written in a scripting language to be executed by a runtime environment.</li>
<li><strong>Interpreter: </strong>An interpreter is a program that reads and executes scripts line by line. It interprets the script's instructions without the need for compilation.</li>
<li><strong>Syntax: </strong>Syntax refers to the rules governing the structure of statements in a script. Correct syntax is essential for the script to be properly interpreted and executed.</li>
<li><strong>Variable: </strong>A variable is a named storage location in which data can be stored and retrieved. Variables are used to store information that may change during the script's execution.</li>
<li><strong>Data Types: </strong>Data types define the type of data that a variable can hold, such as integers, strings, or booleans. </li>
<li><strong>Conditional Statements: </strong>Conditional statements (e.g., <code>if</code>, <code>else</code>, <code>elif</code>) allow the script to make decisions based on certain conditions.</li>
<li><strong>Loop: </strong>A loop is a control structure that allows a set of instructions to be repeated multiple times. Common loop types include <code>for</code> and <code>while</code> loops.</li>
<li><strong>Function: </strong>A function is a reusable block of code that performs a specific task. Functions help in organizing and modularizing code.</li>
<li><strong>Arguments/Parameters: </strong>Arguments or parameters are values that can be passed to a function or a script. They provide input to the code.</li>
<li><strong>Comments: </strong>Comments are non-executable lines in a script used to provide explanations, notes, or documentation for the code. They are ignored during execution.</li>
<li><strong>Debugging: </strong>Debugging is the process of identifying and fixing errors (bugs) in a script. This often involves using tools to trace and analyze the script's execution.</li>
<li><strong>Shebag (#!)</strong>The shebang is a special character sequence (e.g., <code>`#!/bin/bash`</code> ) at the beginning of a script that specifies the interpreter to be used for executing the script.</li>
<li><strong>Scripting Languages: </strong>Scripting languages are programming languages designed for scripting, often with a focus on ease of use and rapid development. Examples include Bash, Python, Perl, and Ruby.</li>
<li><strong>Environment Variables: </strong>Environment variables are variables outside the script that store configuration settings, paths, or other information relevant to the execution environment.</li>
</ul>
These terms are foundational to understanding and working with scripts in various programming and scripting languages. Depending on the language, some terms may have language-specific nuances.
</section>
<section id="chapter-3">
<h3>Execution: </h3>
<ul>
<li>Create a file using command ===> <code>`vi filename.sh`</code></li>
<li>To make the text file executable ===> <code>`chmod 755 filename.sh`</code> </li>
<li>To run the script ===> <code>`./filename.sh`</code></li>
<li>A tab (to create a space) in between the script like: <code>echo "Hello `tab` World"</code> will show a space as <code>Hello `tab` World</code></li>
<li>But when there is just space placed, but not tab between <code>`Hello World`</code>, it will not show any space. </li>
</ul>
The <code>`chmod u+rx myscript.sh`</code> command is used to change the permissions of a file in a Unix-like operating system, such as Linux or macOS. Let's break down the components of this command:
<ul>
<li><strong><code>chmod`</strong>:</code> Stands for "change mode" and is the command used to change file permissions.</li>
<li><strong><code>u+rx</strong>: </code>This specifies the changes to be made to the permissions of the file. Here's what each part means:
<ul>
<li><code>u</code>: Refers to the user (owner) of the file.</li>
<li><code>+rx</code>: Adds read (`r`) and execute (`x`) permissions.</li>
</ul>
</li>
<li><strong><code>`myscript.sh`</strong></code>This is the name of the file for which you want to change the permissions. Replace it with the actual name of your script.</li>
</ul>
Putting it all together, `chmod u+rx myscript.sh` means "change the file permissions of `myscript.sh` to give the owner (user) read and execute permissions."
<ul>
<li><strong>read(r)</strong>: Allows the owner to read the contents of the file.</li>
<li><strong>Execute (`x`): </strong>Allows the owner to execute the file as a program.</li>
</ul>
After running this command, the owner of the file `myscript.sh` will have the necessary permissions to read and execute the script. Adjusting permissions in this way is common when you want to make a script executable by its owner.
</section>
<section id="chapterter-4">
<h2>Example scripts:</h2>
<table border="1">
<thead>
<tr>
<th>Action</th>
<th>Description</th>
<th>Script</th>
</tr>
</thead>
<tbody>
<tr>
<td>echo</td>
<td>Outputs text or variables to the terminal.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
echo "Hello, World!"</code></pre>
</td>
</tr>
<tr>
<td>read</td>
<td>Reads input from the user and assigns it to a variable (The `-p` option is used to specify a prompt that will be displayed to the user. In this case, the prompt is "Enter your name: ")</td>
<td>
<pre><code class="language-bash">#!/bin/sh
read -p "Enter your name: " username</code></pre>
</td>
</tr>
<tr>
<td>if</td>
<td>Conditional statement for branching based on a condition.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
if [ "$var" -gt 10 ]; then
echo "Greater than 10"
fi</code></pre>
</td>
</tr>
<tr>
<td>for</td>
<td>Looping construct for iterating over a sequence of values.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
for i in {1..5}; do
echo $i
done</code></pre>
</td>
</tr>
<tr>
<td>while</td>
<td>Looping construct that continues as long as a specified condition is true.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
counter=0
while [ $counter -lt 5 ]; do
echo $counter
((counter++))
done</code></pre>
</td>
</tr>
<tr>
<td>case</td>
<td>Case statement for conditional branching based on pattern matching.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
case "$option" in
"a") echo "Option A";;
"b") echo "Option B";;
*) echo "Default Option";;
esac</code></pre>
</td>
</tr>
<tr>
<td>function</td>
<td>Declares and defines a function for code modularity.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
function greet {
echo "Hello, $1!"
}
greet "John"</code></pre>
</td>
</tr>
<tr>
<td>ls</td>
<td>Lists files and directories in a directory.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
ls -l</code></pre>
</td>
</tr>
<tr>
<td>cp</td>
<td>Copies files or directories.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
cp file1.txt file2.txt</code></pre>
</td>
</tr>
<tr>
<td>mv</td>
<td>Moves or renames files or directories.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
mv oldfile.txt newfile.txt</code></pre>
</td>
</tr>
<tr>
<td>rm</td>
<td>Removes files or directories.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
rm file.txt</code></pre>
</td>
</tr>
<tr>
<td>touch</td>
<td>Creates an empty file or updates the access and modification times of a file.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
touch newfile.txt</code></pre>
</td>
</tr>
<tr>
<td>grep</td>
<td>Searches for a pattern in a file or input stream.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
grep "pattern" file.txt</code></pre>
</td>
</tr>
<tr>
<td>sed</td>
<td>Stream editor for filtering and transforming text.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
sed 's/old/new/g' input.txt</code></pre>
</td>
</tr>
<tr>
<td>awk</td>
<td>Text processing tool for pattern scanning and processing.</td>
<td>
<pre><code class="language-bash">#!/bin/sh
awk '{print $2}' data.txt</code></pre>
</td>
</tr>
</tbody>
</table>
Here
<ul>
<li><strong>Unix system: </strong><code>`#!/bin/sh`</code>: The first line tells Unix that the file is to be executed by <code>/bin/sh</code>. </li>
<li><strong>Perl: </strong>script may start with the line `#!/usr/bin/perl` to tell your interactive shell that the program which follows should be executed by perl. For Bourne shell programming, we shall stick to <code>`#!/bin/sh`</code>.</li>
</ul>
</section>
<section id="chapter-5">
<h2>Variables</h2>
<p>In shell scripting, variables are used to store and manipulate data. The concept of variables - a symbolic name for
a chunk of memory to which we can assign values, read and manipulate its contents. Here are the key aspects of
variables in shell scripting:</p>
<ol>
<li><strong>Variable Declaration and Assignment:</strong>
<p><strong>Syntax:</strong></p>
<pre><code>variable_name=value</code></pre>
<p><strong>Example:</strong></p>
<pre><code>greeting="Hello, World!"</code></pre>
</li>
<li><strong>Variable Naming Rules:</strong>
<ul>
<li>Variable names are case-sensitive.</li>
<li>They can consist of letters, numbers, and underscores.</li>
<li>The first character must be a letter or an underscore.</li>
</ul>
</li>
<li><strong>Accessing Variable Values:</strong>
<pre><code>#!/bin/sh
echo $greeting</code></pre>
</li>
<li><strong>Special Variables:</strong>
<ul>
<li><strong>Positional Parameters:</strong> $0, $1, $2, ..., represent the script name and its
arguments.</li>
<li><strong>Number of Arguments:</strong> $# gives the number of command-line arguments.</li>
<li><strong>All Arguments:</strong> $@ or $* represents all command-line arguments.</li>
<li><strong>Exit Status:</strong> $? holds the exit status of the last executed command.</li>
</ul>
</li>
<li><strong>Read Input into a Variable:</strong>
<pre><code>read -p "Enter your name: " username</code></pre>
</li>
<li><strong>Variable Concatenation:</strong>
<pre><code>fullname="$firstname $lastname"</code></pre>
</li>
<li><strong>Variable Scope:</strong>
<pre><code>export myvar="some value"</code></pre>
</li>
<li><strong>Unsetting Variables:</strong>
<pre><code>unset myvar</code></pre>
</li>
<li><strong>Using Variables in Commands:</strong>
<pre><code>count=5
echo "There are $count apples."</code></pre>
</li>
</ol>
<p>These are fundamental concepts when working with variables in shell scripting. They provide a way to store,
retrieve, and manipulate data within your scripts, making them dynamic and adaptable.</p>
</section>
<div style="background-color: #f2f2f7; padding: 20px; border: 1px solid #ccc; border-radius: 8px; width: 100%; margin: 50px auto;">
<h4>Wildcards: </h4>
<ul>
<li>Copy all the files from /tmp/a into /tmp/b
<p style="color: #e20df5;">
$ cp /tmp/a/* /tmp/b/<br>
$ cp /tmp/a/*.txt /tmp/b/<br>
$ cp /tmp/a/*.html /tmp/b/<br>
</p>
</li>
<li> rename all .txt files to .bak
<p style="color: #e20df5;">
$ mv *.txt *.bak
</p>
</li>
</ul>
<h4>Escape Characters: </h4>
Escape characters in bash scripting are used to represent characters that are difficult to type or might have a special meaning in the context of a script. They are preceded by a backslash (\) to indicate that the following character should be treated differently. Here are some common escape characters in bash scripting along with examples:
<ul>
<li>Newline (<code>`\n`</code>): Represents a new line.
<p style="color: #e20df5;">echo -e "Line 1\nLine 2"</p>
<strong>Output: </strong> <br>
Line 1 <br>
Line 2
</li>
<li>Tab (<code>`\t</code>): Represents a horizontal tab.
<p style="color: #e20df5;">$ echo -e "Column 1\tColumn 2"</p>
<strong>Output: </strong> Column 1 Column 2
</li>
<li>Backspace (<code>`\b`</code>): Moves the cursor one character to the left.
<p style="color: #e20df5;">$echo -e "Hello\bWorld"</p>
<strong>Output: </strong> HellWorld
</li>
<li>Carriage Return (<code>`\r`</code>): Moves the cursor to the beginning of the line.
<p style="color: #e20df5;">$echo -e "123\rABC"</p>
<strong>Output: </strong> ABC
</li>
<li>Single Quote (<code> `\'`</code>): Escapes a single quote within a single-quoted string.
<p style="color: #e20df5;">$echo 'It '\''s a single quote'</p>
<strong>Output: </strong> It 's a single quote
</li>
<li>Double Quote (<code>`\"`</code>): Escapes a double quote within a double-quoted string.
<p style="color: #e20df5;">$echo "She said, \"Hello!\""</p>
<strong>Output: </strong> She said, "Hello!"
</li>
<li>Dollar Sign (<code>`\$`</code>): Escapes the dollar sign to prevent variable expansion.
<p style="color: #e20df5;">$echo "The cost is \$10"</p>
<strong>Output: </strong> The cost is $10
</li>
<li>Backslash (<code>`\\`</code>): Escapes a backslash.
<p style="color: #e20df5;">$echo "This is a backslash: \\"</p>
<strong>Output: </strong> This is a backslash: \
</li>
<li>
<p style="color: #e20df5;">
$ echo Hello World
</p>
<strong>Output:</strong> Hello World
</li>
<li>
<p style="color: #e20df5;">
$ echo "Hello World"
</p>
<strong>Output:</strong> Hello World
</li>
<li>
<p style="color: #e20df5;">
$ echo "Hello "World""
</p>
<strong>Output:</strong> Hello "World"
</li>
<li>Characters (*, ', etc):
<p style="color: #e20df5;">$ echo *</p>
<strong>Output:</strong> LICENSE README.md Shell-scripting-tutorials.ipynb all_args.sh count_args.sh exit_status.sh my-script-0.sh my-script-1.sh my-script-2.sh my-script-3.sh my-script-4.sh myvar2.sh myvar3.sh var.sh var2.sh var3.sh var4.sh test.txt<br>
(basically all files in the current directory)
<p style="color: #e20df5;">$ echo *txt</p>
<strong>Output:</strong> test.txt<br>
(*txt means all files ending in txt)
<p style="color: #e20df5;">$ echo "*"</p>
<strong>Output:</strong> * <br>
( * in double quotes, is interpreted literally.)
<p style="color: #e20df5;">$ echo *txt</p>
<strong>Output:</strong> test.txt
<p style="color: #e20df5;">$ echo "*txt"</p>
<strong>Output:</strong> *txt <br>
(the same applies, but we have appended txt to the string)
</li>
<li>
<p style="color: #e20df5;">$echo "A quote is \", backslash is \\, backtick is \`."</p>
<strong>Output: </strong> A quote is ", backslash is \, backtick is `.
</li>
<li>
<p style="color: #e20df5;">$echo "A few spaces are and dollar is \$. \$X is ${X}."</p>
<strong>Output: </strong> A few spaces are and dollar is $. $X is 5.
</li>
<li>Some other examples:
<p>echo "The cost is $10" ==> The cost is 0 </p>
<p>echo 'The cost is $10' ==> The cost is $10 </p>
<p>echo "The cost is \$10" ==> The cost is $10 </p>
</li>
</ul>
</div>
<section id="chapter-6">
<h2>Loop</h2>
Most languages have the concept of loops: If we want to repeat a task twenty times, we don't want to have to type in the code twenty times, with maybe a slight change each time.
In Bash scripting, you can use various types of loops to iterate through a set of commands or statements. The two primary types of loops are the for loop and the while loop. Here are examples of both:
<h4>1. 'for' loop:</h4>
The <code>`for`</code> loop is used when you know in advance how many times you want to execute a set of statements.
<ul>
<li><strong>Numeric Range:</strong>
<pre><code class="language-bash">
#!/bin/bash <br>
for i in {1..5}<br>
do<br>
echo "Iteration $i"<br>
done<br>
</code></pre>
<strong>Output: </strong><br>
Iteration 1<br>
Iteration 2<br>
Iteration 3<br>
Iteration 4<br>
Iteration 5
</li>
<li><strong>Iterate Over Items in an Array:</strong>
<pre><code class="language-bash">
#!/bin/bash
fruits=("Apple" "Banana" "Orange")
for fruit in "${fruits[@]}"
do
echo "Fruit: $fruit"
done
</code></pre>
<strong>Output: </strong><br>
Fruit: Apple<br>
Fruit: Banana<br>
Fruit: Orange<br>
</li>
</ul>
<h4>2. `while` Loop:</h4>
The <code>while</code> loop is used when you want to execute a set of statements as long as a condition is true.
<ul>
<li>
</ul>
<pre><code class="language-bash">
#!/bin/bash
counter=5
while [ $counter -gt 0 ]
do
echo "Countdown: $counter"
((counter--))
done
</code></pre>
<ul>
<li><strong>`-gt`</strong>: This is a comparison operator that stands for "greater than." </li>
<li><strong>`0`</strong>: This is the value that $counter is being compared to.</li>
</ul>
<strong>Output: </strong><br>
Countdown: 5<br>
Countdown: 4<br>
Countdown: 3<br>
Countdown: 2<br>
Countdown: 1<br>
</li>
Remember to make your script executable using <code>chmod +x script.sh</code> and then execute it with <code>./script.sh</code> assuming the script is named <code>script.sh</code>.
</section>
<section id="chapter-7">
<h2>Test</h2>
In shell scripting, the <code>`test`</code> command is often used to evaluate conditional expressions. Alternatively, you can use square brackets <code>`[ ]`</code>
as a synonym for <code>`test`</code>. Test is most often invoked indirectly via the if and while statements. It is also the reason you will come into difficulties
if you create a program called test and try to run it, as this shell builtin will be called instead of your program!
The syntax for <code>if...then...else...</code> is:
<pre><code class="language-bash">
#!/bin/bash
if [ ... ]
then
# if-code
else
# else-code
fi
</code></pre>
Note that <code>fi</code> is <code>if</code> backwards! This is used again later with case and esac. Also, be aware of the syntax - the "<code>if [ ... ]</code>" and the "<code>then</code>"
commands must be on different lines. Alternatively, the semicolon "<code>;</code>" can separate them:
<pre><code class="language-bash">
#!/bin/bash
if [ ... ]; then
# do something
fi
</code></pre>
You can also use the elif, like this
<pre><code class="language-bash">
#!/bin/bash
if [ something ]; then
echo "Something"
elif [ something_else ]; then
echo "Something else"
else
echo "None of the above"
fi
</code></pre>
<strong>Examples: </strong>
<ul>
<li><strong>String Comparison: </strong>
<pre><code class="language-bash">
#!/bin/bash
str1="hello"
str2="world"
if [ "$str1" = "$str2" ]; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi
</code></pre>
</li>
<li><strong>Numeric Comparison: </strong>
<pre><code class="language-bash">
#!/bin/bash
num1=5
num2=10
if [ "$num1" -eq "$num2" ]; then
echo "Numbers are equal"
else
echo "Numbers are not equal"
fi
</code></pre>
</li>
<li><strong>File Existence: </strong>
<pre><code class="language-bash">
#!/bin/bash
file_path="/path/to/some/file.txt"
if [ -e "$file_path" ]; then
echo "File exists"
else
echo "File does not exist"
fi
</code></pre>
</li>
<li><strong>Logical AND: </strong>
<pre><code class="language-bash">
#!/bin/bash
num1=5
num2=10
if [ "$num1" -gt 0 ] && [ "$num2" -lt 20 ]; then
echo "Both conditions are true"
else
echo "At least one condition is false"
fi
</code></pre>
</li>
</ul>
</section>
<section id="chapter-8">
<h2>The case statement</h2>
A <code>`case`</code> statement in Bash scripting is used for conditional branching based on pattern matching.
It is often used as an alternative to multiple <code>`if-elif-else`</code> statements when you have multiple
conditions to check. Its syntax is really quite simple:
<p><strong>Example: </strong></p>
<pre><code class="language-bash">
#!/bin/bash
echo "Enter a fruit: "
read fruit
case "$fruit" in
"apple")
echo "Selected fruit: Apple"
;;
"banana" | "plantain")
echo "Selected fruit: Banana or Plantain"
;;
"orange")
echo "Selected fruit: Orange"
;;
*)
echo "Unknown fruit"
;;
esac
</code></pre>
</section>
<section id="chapter-9">
<h2>Bash command references</h2>
<table>
<tr>
<th>Command</th>
<th>Description</th>
<th>Example</th>
</tr>
<tr>
<td>&</td>
<td>Run the previous command in the background</td>
<td>ls &</td>
</tr>
<tr>
<td>&&</td>
<td>Logical AND</td>
<td>if [ "$foo" -ge "0" ] && [ "$foo" -le "9" ]</td>
</tr>
<tr>
<td>||</td>
<td>Logical OR</td>
<td>if [ "$foo" -lt "0" ] || [ "$foo" -gt "9" ]</td>
</tr>
<tr>
<td>^</td>
<td>Start of line</td>
<td>grep "^foo"</td>
</tr>
<tr>
<td>$</td>
<td>End of line</td>
<td>grep "foo$"</td>
</tr>
<tr>
<td>=</td>
<td>String equality (cf. -eq)</td>
<td>if [ "$foo" = "bar" ]</td>
</tr>
<tr>
<td>!</td>
<td>Logical NOT</td>
<td>if [ "$foo" != "bar" ]</td>
</tr>
<tr>
<td>$$</td>
<td>PID of current shell</td>
<td>echo "my PID = $$"</td>
</tr>
<tr>
<td>$!</td>
<td>PID of last background command</td>
<td>ls & echo "PID of ls = $!"</td>
</tr>
<tr>
<td>$?</td>
<td>Exit status of last command</td>
<td>ls ; echo "ls returned code $?"</td>
</tr>
<tr>
<td>$0</td>
<td>Name of current command (as called)</td>
<td>echo "I am $0"</td>
</tr>
<tr>
<td>$1</td>
<td>Name of current command's first parameter</td>
<td>echo "My first argument is $1"</td>
</tr>
<tr>
<td>$9</td>
<td>Name of current command's ninth parameter</td>
<td>echo "My ninth argument is $9"</td>
</tr>
<tr>
<td>$@</td>
<td>All of current command's parameters (preserving whitespace and quoting)</td>
<td>echo "My arguments are $@"</td>
</tr>
<tr>
<td>$*</td>
<td>All of current command's parameters (not preserving whitespace and quoting)</td>
<td>echo "My arguments are $*"</td>
</tr>
<tr>
<td>-eq</td>
<td>Numeric Equality</td>
<td>if [ "$foo" -eq "9" ]</td>
</tr>
<tr>
<td>-ne</td>
<td>Numeric Inequality</td>
<td>if [ "$foo" -ne "9" ]</td>
</tr>
<tr>
<td>-lt</td>
<td>Less Than</td>
<td>if [ "$foo" -lt "9" ]</td>
</tr>
<tr>
<td>-le</td>
<td>Less Than or Equal</td>
<td>if [ "$foo" -le "9" ]</td>
</tr>
<tr>
<td>-gt</td>
<td>Greater Than</td>
<td>if [ "$foo" -gt "9" ]</td>
</tr>
<tr>
<td>-ge</td>
<td>Greater Than or Equal</td>
<td>if [ "$foo" -ge "9" ]</td>
</tr>
<tr>
<td>-z</td>
<td>String is zero length</td>
<td>if [ -z "$foo" ]</td>
</tr>
<tr>
<td>-n</td>
<td>String is not zero length</td>
<td>if [ -n "$foo" ]</td>
</tr>
<tr>
<td>-nt</td>
<td>Newer Than</td>
<td>if [ "$file1" -nt "$file2" ]</td>
</tr>
<tr>
<td>-d</td>
<td>Is a Directory</td>
<td>if [ -d /bin ]</td>
</tr>
<tr>
<td>-f</td>
<td>Is a File</td>
<td>if [ -f /bin/ls ]</td>
</tr>
<tr>
<td>-r</td>
<td>Is a readable file</td>
<td>if [ -r /bin/ls ]</td>
</tr>
<tr>
<td>-w</td>
<td>Is a writable file</td>
<td>if [ -w /bin/ls ]</td>
</tr>
<tr>
<td>-x</td>
<td>Is an executable file</td>
<td>if [ -x /bin/ls ]</td>
</tr>
<!-- ... (repeat for the rest of the commands) -->
<tr>
<td>( ... )</td>
<td>Function definition</td>
<td>function myfunc() { echo hello }</td>
</tr>
</table>
</section>
<!-------Reference ------->
<section id="reference">
<h2>References</h2>
<ul>
<li><a href="https://github.com/arunp77/bash-scripting/tree/main" target="_blank">Please find some Tutorial sessions on my github repository.</a></li>
<li><a href="https://www.shellscript.sh/" target="_blank">The Shell Scripting Tutorial</a></li>
</ul>
</section>
<hr>
<div style="background-color: #f0f0f0; padding: 15px; border-radius: 5px;">
<h3>Some other interesting things to know:</h3>
<ul style="list-style-type: disc; margin-left: 30px;">
<li>Visit my website on <a href="sql-project.html">For Data, Big Data, Data-modeling, Datawarehouse, SQL, cloud-compute.</a></li>
<li>Visit my website on <a href="Data-engineering.html">Data engineering</a></li>
</ul>
</div>
<div class="navigation">
<a href="Data-engineering.html" class="clickable-box">
<span class="arrow-right">Content</span>
</a>
<a href="index.html#portfolio" class="clickable-box">
<span class="arrow-left">Portfolio section</span>
</a>
</div>
</div>
</div>
</section><!-- End Portfolio Details Section -->
</main><!-- End #main --
<!-- ======= Footer ======= -->
<footer id="footer">
<div class="container">
<div class="copyright">
© Copyright <strong><span>Arun</span></strong>
</div>
</div>
</footer><!-- End Footer -->
<a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>
<!-- Vendor JS Files -->
<script src="assets/vendor/purecounter/purecounter_vanilla.js"></script>
<script src="assets/vendor/aos/aos.js"></script>
<script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="assets/vendor/glightbox/js/glightbox.min.js"></script>
<script src="assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
<script src="assets/vendor/swiper/swiper-bundle.min.js"></script>
<script src="assets/vendor/typed.js/typed.umd.js"></script>
<script src="assets/vendor/waypoints/noframework.waypoints.js"></script>
<script src="assets/vendor/php-email-form/validate.js"></script>
<!-- Template Main JS File -->
<script src="assets/js/main.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
hljs.initHighlightingOnLoad();
});
</script>