Let's Dive into Shell Scripting!

Ankita LunawatAnkita Lunawat
3 min read

A shell script is a computer program intended to run by a Unix-like shell, which is a command-line interpreter; it is essentially a text file containing a series of commands that the shell interprets and executes.

Why Use Shell Scripts?

Shell scripts are useful for automating repetitive tasks, saving time and effort, managing systems efficiently, customizing your environment to meet specific needs, and learning a powerful tool that provides a deeper understanding of the Unix/Linux environment.

Simple Shell Scripts

A shell script is a series of commands executed sequentially.

Here's a basic example:

#!/bin/bash
echo "Hello, World!"
date

The #!/bin/bash line specifies the interpreter to use.

Shell Variables

Variables store values that can be used later in the script.

#!/bin bash
name="Alice"
echo "Hello, $name!"

File System Commands, IO Commands, and IO Redirection

  • File System Commands:

    • ls: Lists files and directories

    • cd: Changes the current directory

    • mkdir: Creates a directory

    • rm: Removes files or directories

    • mv: Moves or renames files

    • cp: Copies files

  • IO Commands:

    • cat: Concatenates and prints files

    • head: Prints the first few lines of a file

    • tail: Prints the last few lines of a file

  • IO Redirection:

    • >: Redirect output to a file

    • <: Redirect input from a file

    • >>: Append output to a file

    • 2>: Redirect standard error

    • 2>&1: Redirect standard error to standard output

Example:

#!/bin/bash
ls -la > file_list.txt

Command Line Arguments

You can pass arguments to a script when you run it.

#!/bin/bash
echo "Hello, $1!"

Run it as: ./script.sh Alice

Evaluating Expressions in Shell

Use the expr command or arithmetic expansion $(( )):

#!/bin/bash
result=$((5 + 3))
echo "Result: $result"

Predicates, Operators for Testing Strings, Integers, and Files

  • String Comparison:

    • =: Equal to

    • !=: Not equal to

    • -z: String is empty

    • -n: String is not empty

  • Integer Comparison:

    • -eq: Equal to

    • -ne: Not equal to

    • -gt: Greater than

    • -ge: Greater than or equal to

    • -lt: Less than

    • -le: Less than or equal to

  • File Testing:

    • -e: File exists

    • -f: File is a regular file

    • -d: File is a directory

    • -r: File is readable

    • -w: File is writable

    • -x: File is executable

If-Then-Else in Shell

#!/bin/bash
if [ "$name" == "CloudHub" ]; then
  echo "Hello, CloudHub!"
else
  echo "Hello, stranger!"
fi

For, While, and Do-While Loops

  • For Loop
for i in 1 2 3 4 5; do
  echo $i
done
  • While Loop
count=0
while [ $count -lt 5 ]; do
  echo $count
  count=$((count+1))
done
  • Do-While Loop
count=0
do
  echo $count
  count=$((count+1))
while [ $count -lt 5 ]; do
done
0
Subscribe to my newsletter

Read articles from Ankita Lunawat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ankita Lunawat
Ankita Lunawat

Hi there! I'm a passionate AWS DevOps Engineer with 2+ years of experience in building and managing scalable, reliable, and secure cloud infrastructure. I'm excited to share my knowledge and insights through this blog. Here, you'll find articles on: AWS Services: Deep dives into core AWS services like EC2, S3, Lambda, and more. DevOps Practices: Best practices for CI/CD, infrastructure as code, and automation. Security: Tips and tricks for securing your AWS environments. Serverless Computing: Building and deploying serverless applications. Troubleshooting: Common issues and solutions in AWS. I'm always eager to learn and grow, and I hope this blog can be a valuable resource for fellow DevOps enthusiasts. Feel free to connect with me on [LinkedIn/Twitter] or leave a comment below!