Skip to main content

Getting Started with Shell Scripting in Unix/Linux πŸ§πŸ’»


    Creating and running scripts in Unix/Linux can simplify repetitive tasks, automate processes, and make your workflow more efficient. Whether you're checking logged-in users, displaying the date, or showcasing your current working directory, shell scripting can help you do it all with just a few lines of code. Let’s walk through the basics of creating and running a simple shell script, exploring built-in commands, and enhancing our script with special characters in echo statements.

Writing Your First Script ✍️

Imagine you want to create a script that shows the current date and all users currently logged in. To start, open your favorite text editor (like nano or vim) and type the following commands:

date
echo "Users currently logged in:"
who

After typing, save this file with a name like check_users.sh. This script uses:

  • date to display the current date.
  • echo to print a message.
  • who to list all currently logged-in users.

Making the Script Executable πŸš€

Before you can run your script, you need to mark it as executable. To do this, use the chmod command:

chmod +x check_users.sh

This command gives your script execution permissions. Now, you can run the script by typing:

./check_users.sh

Shebang (#!) and Specifying the Shell πŸ“œ

Adding a shebang at the top of your script specifies which shell should interpret the script commands. For example:

#!/bin/bash

This line tells the system to use bash as the interpreter. Other popular shells include:

  • #!/bin/ksh for Korn shell
  • #!/bin/sh for Bourne shell

Anything following the # character in the code is treated as a comment, allowing you to add notes or explanations directly into your script for clarity.

Using Built-in Commands πŸ› ️

Unix/Linux shells like Bourne, Korn, and Bourne Again (Bash) come with built-in commands. Here are a few commonly used ones that help control the flow of scripts:

  • exit: Ends the script or command session.
  • if, for, while, until: Control structures for condition-based and looped operations.
  • let: Performs arithmetic operations.
  • read: Reads input from the user.
  • test: Evaluates conditions (such as checking if a file exists).

These built-ins allow for more advanced functionality and customization in scripts.

Enhanced Script: Displaying Date, Users, and Directory πŸ“…πŸ‘₯πŸ“‚

Here’s an example script that displays the current date, the number of logged-in users, and your current working directory path:

#!/bin/bash
#
# Displays the current date and time, number of users logged on,
# and the full path of the current working directory.
date # Display the current date
who | wc -l # Display the number of users logged in
pwd # Display the current working directory

This script will produce output like this:

Wednesday February 1 14:54:35 GMT 2023
32
/home/username/current_directory

Adding Improvements: Formatting Output with echo πŸ’¬

To make the output more user-friendly, let’s enhance it with echo and special characters:

#!/bin/bash
#
# Displays the current date and time, the number of users on the system,
# and the full path of the current working directory.
echo # Skips a line for readability
echo -e "Date and time: \c" # `\c` keeps output on the same line
date
echo -e "Number of users on the system: \c"
who | wc -l
echo -e "Your current working directory: \c"
pwd
echo # Adds an extra line at the end

Expected Output:

Date and time: Wednesday February 1 15:14:14 GMT 2023
Number of users on the system: 18
Your current directory: /home/username/current_directory

This script uses:

  • -e in echo to enable interpretation of backslash escapes.
  • \c to keep output on the same line.
  • Empty echo statements to add blank lines for readability.

Special Characters in echo 🎩

The echo command supports escape characters to control output formatting. Here are a few helpful ones:

  • \b: Backspace
  • \c: Keeps output on the same line
  • \n: Newline
  • \r: Carriage return (moves to the start of the line)
  • \t: Tab
  • \0n: ASCII code in octal form for special characters

Example:

echo -e "Welcome to Unix/Linux Scripting!\nLet’s automate tasks!"

This produces:

Welcome to Unix/Linux Scripting!
Let’s automate tasks!

Wrapping Up 🌟

With these scripting basics, you’re ready to start creating scripts that save time, automate processes, and simplify daily tasks. Start small by trying out different commands and adding them to scripts, then experiment with built-ins, command chaining, and custom formatting to level up your scripts! Happy scripting! πŸŽ‰ 

Comments

Popular posts from this blog

Unraveling the Apache Hadoop Ecosystem: The Ultimate Guide to Big Data Processing πŸŒπŸ’ΎπŸš€

In the era of big data, organizations are constantly seeking efficient ways to manage, process, and analyze large volumes of structured and unstructured data. Enter Apache Hadoop , an open-source framework that provides scalable, reliable, and distributed computing solutions. With its rich ecosystem of tools, Hadoop has become a cornerstone for big data projects. Let’s explore the various components and layers of the Hadoop ecosystem and how they work together to deliver insights. Data Processing Layer πŸ› ️πŸ” The heart of Hadoop lies in its data processing capabilities, powered by several essential tools: Apache Pig 🐷 : Allows Hadoop users to write complex MapReduce transformations using a scripting language called Pig Latin , which translates to MapReduce and executes efficiently on large datasets. Apache Hive 🐝 : Provides a SQL-like query language called HiveQL for summarizing, querying, and analyzing data stored in Hadoop’s HDFS or compatible systems like Amazon S3. It makes inter...

Understanding Cloud Computing: SaaS, PaaS, IaaS, and DaaS Explained ☁️πŸ’»πŸš€

 In today’s digital world, cloud computing has revolutionized the way businesses and individuals store, access, and manage data and applications. From reducing the burden of software management to providing scalable platforms for app development, the cloud offers a wide range of services tailored to different needs. Let’s dive into the most common cloud services: SaaS, PaaS, IaaS, and DaaS . 1. SaaS – Software as a Service πŸ–₯️✨ SaaS is the most recognizable form of cloud service for everyday consumers. It takes care of managing software and its deployment, making life easier for businesses by removing the need for technical teams to handle installations, updates, and licensing. πŸ”‘ Key Benefits : Cost Reduction : No need for a dedicated IT team or expensive licensing fees. Ease of Use : Access software directly through the internet without complex setup. πŸ› ️ Popular SaaS Applications : Salesforce : A leading CRM platform that helps businesses manage customer relationships. Google ...

Managing Subscriptions and Data Restoration with PostgreSQL Triggers

PostgreSQL Triggers   Introduction: In the world of database management, efficient handling of data is a critical aspect. One common scenario is managing subscriptions and ensuring data restoration for deleted records. In this technical blog, we will delve into the process of achieving this using PostgreSQL triggers. We will explore the concepts of triggers, their types, and how they can be applied to ensure seamless data management. Understanding the Scenario:      In the realm of database management, one common challenge revolves around maintaining and restoring data integrity when dealing with subscriptions and deleted records. Consider a scenario where an application manages APIs and their corresponding subscriptions. As APIs are created, users subscribe to them to receive updates and notifications. However, situations may arise where APIs are deleted due to updates, changes in business requirements, or other reasons. When APIs are deleted, their associated subsc...