Shell Scripting Essentials

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
Search for a command to run...

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
No comments yet. Be the first to comment.
1. Difference between Docker and Kubernetes Docker → Builds and runs containers.Kubernetes → Orchestrates containers across multiple nodes. Key points: Docker = container runtime. Kubernetes = container orchestration tool. Kubernetes provides auto...
In this session, we learn how to monitor a Kubernetes cluster using Prometheus and Grafana.This is not just theory — there is a GitHub repository containing all installation commands and demo steps.The repo will also be enhanced later with advanced K...
1. What is a ConfigMap in Kubernetes? A ConfigMap is used to store non-sensitive configuration data that your application needs — such as: Database port Connection type Any general configuration values In normal applications (non-Kubernetes), de...
Kubernetes normally supports built-in resources like: Deployment Service Pod ConfigMap Secret Ingress These are called native resources. Sometimes companies (Istio, ArgoCD, Prometheus Operator, Kyverno, etc.) want to add new features that Kub...
1. Why Kubernetes Services Are Needed When a Pod is created in Kubernetes, it receives a dynamic IP address.If the Pod dies and restarts, its IP changes.So other Pods (like checkout → payments) cannot rely on Pod IP because it changes, creating issue...
Shell scripting is a powerful way to automate tasks in a Unix/Linux environment. This blog will cover the fundamental concepts of writing and running scripts, understanding the shebang line, commenting code, using variables, and performing basic arithmetic operations.
To create a shell script, you can use any text editor like nano, vim, or gedit.
# Create a new script file
nano myscript.sh
Inside myscript.sh, you can write your script. To run your script, you need to make it executable and then execute it.

chmod +x myscript.sh

./myscript.sh

The shebang (#!) is the first line in a script that tells the system which interpreter to use to execute the script. For example, to use Bash as the interpreter, you would start your script with:
#!/bin/bash
This line ensures that the script is run with the Bash shell.

Comments are essential for making your scripts understandable. In shell scripts, comments begin with the # symbol. Anything following # on that line will be ignored by the interpreter.
# This is a comment
echo "Hello World!" # This prints a greeting

Variables allow you to store data for reuse throughout your script. In shell scripting, you don’t need to declare a variable before using it. Simply assign a value without spaces around the equal sign.
name="Dinesh"
echo "Hello, $name!" # Output: Hello, Dinesh!

Output:

You can perform basic arithmetic operations in shell scripts using the expr command or the $((...)) syntax.
expr:result=$(expr 5 + 3) # Adds 5 and 3
echo "The result is: $result" # Output: The result is: 8

Output:

$((...)):result=$((5 + 3)) # Adds 5 and 3
echo "The result is: $result" # Output: The result is: 8

Output:

Addition: +
Subtraction: -
Multiplication: *
Division: /
Modulus: %
Shell scripting is a vital skill for automating tasks and managing systems efficiently. By mastering these basics—writing and running scripts, understanding shebang, commenting, using variables, and performing arithmetic—you will be well on your way to creating powerful scripts.
Experiment with these concepts and gradually build more complex scripts as you become comfortable. Happy scripting!