Shell Scripting – Getting Started
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In this blog series, we will explore one of the most commonly used scripting languages, shell scripting. A shell script, in a nutshell, is basically a grouping of multiple commands to get executed in the shell of Unix/Linux/POSIX-compliant operating systems. You execute a command or a set of commands through shell which intern interacts with the system kernel and give you the output of execution.
Kernel-Shell:
A kernel is the core of any operating system that acts as an intermediate between system hardware and software. A shell on the other hand links between the kernel and end-users. Together they achieve the most common day to day work of an operating system.
Hardware: Hardware consists of all physical devices attached to the System. For example Hard disk drive, RAM, Motherboard, CPU, etc.
Kernel: Kernel is the core component for any Unix/Linux/POSIX-compliant operating system which directly interacts with the hardware.
Shell: Shell is the interface that takes input from Users and sends instructions to the Kernel, also takes the output from Kernel, and sends the result back to output shell.
Applications: These are the utility programs that run on Shell. This can be any application like Your web browser, media player, text editor, etc.
Major Types Of Shell:
Bourne shell (sh): Developed by Stephen Bourne at Bell Labs, it was a replacement for the Thompson shell, whose executable file had the same name—sh. It was released in 1979 in the Version 7 Unix release distributed to colleges and universities. Although it is used as an interactive command interpreter, it was also intended as a scripting language and contains most of the features that are commonly considered to produce structured programs.
C shell (csh): The C shell (csh or the improved version, tcsh) is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the Berkeley Software Distribution (BSD) which Joy first distributed in 1978.
KornShell (ksh): ksh is a Unix shell that was developed by David Korn at Bell Labs in the early 1980s and announced at USENIX on July 14, 1983. The initial development was based on the Bourne shell source code. Other early contributors were Bell Labs developers Mike Veach and Pat Sullivan, who wrote the Emacs and vi-style line editing modes’ code, respectively. KornShell is backward-compatible with the Bourne shell and includes many features of the C shell, inspired by the requests of Bell Labs users.
Bourne-again shell (bash): GNU Bash or simply Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used widely as the default login shell for most Linux distributions and Apple’s macOS Mojave and earlier versions.
Tee Cee Shell (tcsh): tcsh is a Unix shell based on and compatible with the C shell (csh). It is essentially the C shell with programmable command-line completion, command-line editing, and a few other features. Unlike the other common shells, functions cannot be defined in a tcsh script and the user must use aliases instead (as in csh). It is the native root shell for BSD-based systems such as FreeBSD.
Shell Script Components:
Shebang: The #! syntax used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems.
Command: The actual command that will be executed in your script.
Comment: The # syntax used in the scripts to indicate a comment. The shell ignores any line beginning with # with one exception of shebang.
Shell Scripting – Getting Started:
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 |
####################################### ## Shell Scripting | Getting Started ## ####################################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ## ------- ## Shebang ## ------- ## Check all the shell's available in your system cat /etc/shells ## Create your 1st shell script (.sh extension is not mandetory but useful for editor formatting) vi myshscript.sh ------------------- #!/bin/bash # this is a comment and will not get executed. echo "hello world" ------------------- :wq ## Provide execute permission to the script chmod +x myshscript.sh ## Execute the script ./myshscript.sh #or bash myshscript.sh #for bash script ## Another example of shebang cat /dev/null > myshscript.sh && vi myshscript.sh ------------------ #!/bin/rm # Self-deleting script. echo "This script will never say hello world" ------------------ :wq ## Execute the script ./myshscript.sh #nothing happens ## Check if the script exist ls -l ## ------- ## Comment ## ------- ## Create the script > myshscript.sh && vi myshscript.sh ------------------- #!/bin/bash # This is a single line comment # This is a # multi-line comment : ' This is also a multi-line comment using so Not 100% safe to use ' << --MULTILINE-COMMENT-- This is also a multi-line comment using HEREDOC Not 100% safe to use this way of commenting --MULTILINE-COMMENT-- << #### This is another version of a multi-line comment using HEREDOC you can replace #### with any charecter as long as start and end match the type and count of the charecter Not 100% safe to use this way of commenting #### echo "all the comments are hidden" ------------------- :wq ## Execute the script ./myshscript.sh ## ---------- ## User input ## ---------- ## take input from user > myshscript.sh && vi myshscript.sh ------------------- #!/bin/bash echo -n "Enter your name: " read name echo "hello $name" ------------------- :wq ## Execute the script ./myshscript.sh ## take input from user, using parameter > myshscript.sh && vi myshscript.sh ------------------- #!/bin/bash echo "hello $1" ------------------- :wq ## Execute the script with your name as parameter ./myshscript.sh ## take input from user, using file echo "cloudaffaire" > name.txt > myshscript.sh && vi myshscript.sh ------------------- #!/bin/bash echo "hello $( ------------------- :wq ## Execute the script ./myshscript.sh |
Hope you have enjoyed this blog post. In the next blog post, we will discuss special characters in the shell.