(241) Master SHELL SCRIPTING in ONE VIDEO for Beginners 🔥 [HINDI] | MPrashant
Introduction
Complete course on shell scripting starting from basics.
Over 40 scripts and more than 5 hours of content, including real-life projects.
Basics of Shell
Hardware: CPU, RAM, and storage devices.
Operating systems enable the interaction with hardware through commands.
Terminal: A user interface for executing commands.
Common shell types: Bash (most common), SH, KSH, CSH, TCSH, and ZSH.
Shell Scripting Overview
Shell scripting consists of a series of commands to automate tasks.
The script is an executable file that can consolidate multiple commands.
Scripts facilitate the automation of repetitive tasks, saving time and effort.
Scripts can be scheduled to run at specific times, eliminating the need for constant monitoring.
Writing Your First Script
Creating a simple "Hello World" script:
Use
#!followed by the path to the shell (e.g.,#!/bin/bash).Use the
echocommand to display text.
Use
touchto create a new script file andchmod +xto make it executable.Running the script can involve checking for execution permissions.
Comments in Scripts
Use
#for single-line comments and: ' ':for multi-line comments.
Variables in Shell Scripting
Variables are containers for storing data.
To create a variable, use:
variable_name=value.Access a variable with
$variable_name.Example: using variables for user data in scripts.
Conditionals and Logic
Writing
ifstatements to control the flow based on conditions.Usage of logical operators:
&&,||, and!for combining conditions.Using
eliffor multiple conditional branches.Commands can be tested for success or failure using exit statuses.
Loops in Shell Scripting
For Loops
Syntax for loops:
for variable in list do commands doneLoop through files, directories, or a sequence of numbers.
While Loops
The syntax allows for executing as long as a condition is true:
while [ condition ]; do commands done
Until Loops
Executes until the condition is true:
until [ condition ]; do commands done
Functions
Functions encapsulate reusable pieces of code:
Define a function using
function_name() { commands }.Call a function by its name.
Utilize parameters within functions to receive input data.
Error Handling and Debugging
Use
set -eto exit on errors, enhancing reliability.Utilize logging for tracking execution.
Use
trapto handle signals and clean up during script execution.
Cron Jobs and Scheduling
Use
cronfor scheduling tasks:Access the crontab with
crontab -e.Schedule scripts to run at defined intervals.
Conclusion
Shell scripting is a powerful tool for automating repetitive tasks and managing system operations.
Understanding the basic syntax and functionalities enables effective script writing.