For working professionals
For fresh graduates
More
1. Introduction
6. PyTorch
9. AI Tutorial
10. Airflow Tutorial
11. Android Studio
12. Android Tutorial
13. Animation CSS
16. Apex Tutorial
17. App Tutorial
18. Appium Tutorial
21. Armstrong Number
22. ASP Full Form
23. AutoCAD Tutorial
27. Belady's Anomaly
30. Bipartite Graph
35. Button CSS
39. Cobol Tutorial
46. CSS Border
47. CSS Colors
48. CSS Flexbox
49. CSS Float
51. CSS Full Form
52. CSS Gradient
53. CSS Margin
54. CSS nth Child
55. CSS Syntax
56. CSS Tables
57. CSS Tricks
58. CSS Variables
61. Dart Tutorial
63. DCL
65. DES Algorithm
83. Dot Net Tutorial
86. ES6 Tutorial
91. Flutter Basics
92. Flutter Tutorial
95. Golang Tutorial
96. Graphql Tutorial
100. Hive Tutorial
103. Install Bootstrap
107. Install SASS
109. IPv 4 address
110. JCL Programming
111. JQ Tutorial
112. JSON Tutorial
113. JSP Tutorial
114. Junit Tutorial
115. Kadanes Algorithm
116. Kafka Tutorial
117. Knapsack Problem
118. Kth Smallest Element
119. Laravel Tutorial
122. Linear Gradient CSS
129. Memory Hierarchy
133. Mockito tutorial
134. Modem vs Router
135. Mulesoft Tutorial
136. Network Devices
138. Next JS Tutorial
139. Nginx Tutorial
141. Octal to Decimal
142. OLAP Operations
143. Opacity CSS
144. OSI Model
145. CSS Overflow
146. Padding in CSS
148. Perl scripting
149. Phases of Compiler
150. Placeholder CSS
153. Powershell Tutorial
158. Pyspark Tutorial
161. Quality of Service
162. R Language Tutorial
164. RabbitMQ Tutorial
165. Redis Tutorial
166. Redux in React
167. Regex Tutorial
170. Routing Protocols
171. Ruby On Rails
172. Ruby tutorial
173. Scala Tutorial
175. Shadow CSS
178. Snowflake Tutorial
179. Socket Programming
180. Solidity Tutorial
181. SonarQube in Java
182. Spark Tutorial
189. TCP 3 Way Handshake
190. TensorFlow Tutorial
191. Threaded Binary Tree
196. Types of Queue
197. TypeScript Tutorial
198. UDP Protocol
202. Verilog Tutorial
204. Void Pointer
205. Vue JS Tutorial
206. Weak Entity Set
207. What is Bandwidth?
208. What is Big Data
209. Checksum
211. What is Ethernet
214. What is ROM?
216. WPF Tutorial
217. Wireshark Tutorial
218. XML Tutorial
Shell scripting is a powerful skill that empowers users to interact with their operating systems efficiently and automate repetitive tasks. In this Shell Scripting Tutorial, we will delve into the world of shell scripting, exploring its various aspects and equipping you with the knowledge to become proficient in this essential tool.
At its core, shell scripting involves creating scripts that utilize the command-line interface (CLI) to execute a series of commands. These scripts can be written in various shell languages, such as Bash, and offer a wide range of functionalities. Let's start by understanding the key components that form the foundation of shell scripting.
The kernel is the core component of an operating system. It is responsible for managing the system's resources, such as the central processing unit (CPU), memory, and input/output devices. The kernel acts as an intermediary between the hardware and software, facilitating communication between them.
When a user interacts with the operating system by running commands or executing programs, the requests are handled by the kernel.
The kernel also plays a crucial role in ensuring that different processes running on the system are isolated from one another, preventing them from interfering with each other's memory or resources.
A shell is a user interface that allows users to interact with an operating system. It acts as a command-line interpreter, taking input from users in the form of text-based commands and executing them to perform various tasks. The shell is a crucial bridge between the user and the operating system's kernel.
When a user opens a terminal on a Unix-like system or a command prompt on Windows, they are interacting with a shell. The shell interprets the commands entered by the user and communicates with the operating system's kernel to execute those commands.
The primary function of a shell is to provide a way for users to interact with the operating system without the need for a graphical user interface.
There are two main categories of shells:
Command line shells, as the name suggests, allow users to interact with the system by entering text-based commands. Bash (Bourne Again SHell) is one of the most popular command-line shells and is the default shell for many Unix-like systems.
The "ls" command is run with the "-l" option in the screenshot up top. It will display a long listing of every file in the current working directory.
It can be challenging for novices to learn so many commands while using a command line shell. It is highly strong; users can save commands in a file and execute them all at once.
Any repetitive task can then be mechanized with ease in this way. In Windows, these files are typically referred to as batch files, and in Linux/macOS systems, as shell scripts.
Graphical shells provide a user-friendly graphical interface for interacting with the operating system. These shells often use windows, icons, and menus (WIM) to facilitate user interactions. Examples include GNOME Shell and Windows PowerShell.
By enabling actions like opening, closing, moving, and resizing windows, as well as switching focus between windows, graphical shells offer a way to manipulate programs with a graphical user interface (GUI). A good example of an operating system that offers a graphical user interface (GUI) for application interaction is Windows OS or Ubuntu OS.
For Linux systems, a number of shells are available, including:
Each shell performs the same purpose, although they each recognize various commands and offer various built-in features.
A terminal is a program that provides users with a text-based interface to interact with the shell. It allows users to enter commands and view the results of those commands in a command-line environment. The terminal acts as a window into the shell, providing an interactive way to work with the system. Simply type "terminal" into the search box and double-click to open the terminal.
You can view how Red Hat Linux's terminal appears here.
Shell scripting involves writing scripts using shell languages to automate tasks and perform complex operations. Shell scripts can be used to execute a series of commands in a sequence, making it easy to repeat tasks and save time. For instance, you can write a shell script to back up files or perform system maintenance tasks.
To avoid doing this repetitive labor, we can write these commands in a file and execute them in a shell, as a shell can also accept commands as input from files. Shell Scripts or Shell Programs are the names of these files.
The components of a shell script include the following:
Unix shell scripting is the process of writing scripts or programs that utilize Unix shell commands to automate tasks or perform specific operations in a Unix or Unix-like operating system. The shell is a command-line interface that allows users to interact with the operating system by executing commands. Common Unix shells include Bash (Bourne Again SHell), C Shell (csh), Korn Shell (ksh), and Z Shell (zsh).
Here are some key concepts and features of Unix shell scripting:
Example of a simple Bash shell script that prints "Hello, World!":
To execute this script, save it to a file (e.g., hello.sh) and make it executable with chmod +x hello.sh. Then, run it using ./hello.sh.
#!/bin/bash
echo "Enter a file/directory path: "
read path
if [ -e "$path" ]; then
if [ -f "$path" ]; then
echo "$path is a regular file."
elif [ -d "$path" ]; then
echo "$path is a directory."
else
echo "Unknown file type."
fi
else
echo "$path does not exist."
fi
If the user enters an existing regular file path, the output will be:
#!/bin/bash
is_even_odd() {
if (( $1 % 2 == 0 )); then
echo "$1 is even."
else
echo "$1 is odd."
fi
}
echo "Enter a number: "
read num
is_even_odd $num
Assume the script is executed, and the user enters the number 7 when prompted. The output will be as follows:
In this example, the script reads the input number 7 from the user using the read command. It then calls the function is_even_odd with the value of num (which is 7) as an argument. Inside the function, it performs the check (( $1 % 2 == 0 )) to determine whether the number is even or odd. Since 7 % 2 equals 1, the condition $1 % 2 == 0 evaluates to false, and the script prints "7 is odd."
Shell scripts offer several advantages:
Mastering shell scripting is an invaluable skill for any developer or system administrator. It enables you to streamline your workflow, automate tedious tasks, and gain a deeper understanding of how your operating system works. Embrace the world of shell scripting, and you'll find yourself unleashing the true potential of your system. We hope that this Shell scripting tutorial for beginners.Happy scripting!
Command-line arguments allow you to provide input to a shell script when executing it from the command line. Within a shell script, you can access these arguments using special variables: $1 for the first argument, $2 for the second argument, and so on. You can access all the arguments at once using $@ or $*.
In shell scripting, you can redirect standard output (stdout) and standard error (stderr) to different files or the same file. To redirect both to the same file, you can use the following syntax: command > output.log 2>&1. This redirects stdout to output.log and then redirects stderr to the same file descriptor as stdout, effectively merging both streams into the file.
Shell scripts can become more efficient and modular by creating libraries of reusable functions. To do this, you can create a separate file containing your functions (e.g., mylibrary.sh), and then use the source or . command to include the functions in other scripts.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.