DEV Community

Mohd Saquib Mansoori
Mohd Saquib Mansoori

Posted on

Creating a small project on Counting Using Bash Scripting

Title: Building a Simple Timer Project Using Bash Scripting

Introduction:
In the world of programming, mastering various languages is essential for expanding your skills and enhancing problem-solving abilities. Bash scripting is one such powerful tool that comes in handy for automating tasks, managing processes, and creating small projects. In this blog, we'll explore how to build a simple yet practical timer project using Bash scripting. Whether you're a beginner or an experienced developer, this project will not only be a fun challenge but also a great way to strengthen your Bash skills.

Prerequisites:
Before diving into the project, make sure you have basic knowledge of Bash scripting and a Linux or macOS environment. Bash is a popular shell and scripting language that comes pre-installed on most Unix-based systems.

Project Overview:
The goal of this project is to create a timer that allows users to set a specific time interval and receive a notification when the timer expires. We'll use the sleep command to achieve this, along with some basic user input and output handling in Bash.

Step 1: Getting Started
To begin, open your terminal and create a new file called timer.sh using your favorite text editor. This file will contain the Bash script that defines the timer's functionality.

Step 2: Setting the Timer
In this step, we'll ask the user to input the desired time interval for the timer. We'll use the read command to prompt the user for input and store it in a variable.

#!/bin/bash

echo "Welcome to the Bash Timer!"

# Ask the user to enter the time interval
read -p "Enter the time in seconds: " time_interval
Enter fullscreen mode Exit fullscreen mode

Step 3: Timer Logic
With the user's input, we'll implement the timer logic using the sleep command. The sleep command suspends the script's execution for a specified number of seconds.

# Check if the input is a positive integer
if ! [[ "$time_interval" =~ ^[0-9]+$ ]]; then
    echo "Error: Please enter a positive integer."
    exit 1
fi

# Display a confirmation message to the user
echo "Timer set for $time_interval seconds. Press Ctrl+C to stop the timer."

# Start the timer
sleep "$time_interval"

# Timer expires - display the notification
echo "Timer expired! Time's up!"
Enter fullscreen mode Exit fullscreen mode

Step 4: Make the Script Executable
Before we can run the script, we need to make it executable using the chmod command.

chmod +x timer.sh
Enter fullscreen mode Exit fullscreen mode

Step 5: Run the Timer
You're now ready to run the timer! Execute the script in your terminal by typing:

./timer.sh
Enter fullscreen mode Exit fullscreen mode

Conclusion:
Congratulations! You've successfully built a simple timer project using Bash scripting. This project may seem modest, but it lays the groundwork for more complex and useful applications. Feel free to expand this project by adding features like sound notifications, multiple timers, or integrating it with other scripts. With Bash scripting, the possibilities are endless!

Remember, practice is key to mastering any programming language. So, keep exploring, experimenting, and building more projects to enhance your skills and become a proficient developer.

If there is any thing wrong or is not correct, let me know.

Top comments (0)