DEV Community

Cover image for Bash, the magic that empowers the Cloud
Sm0ke
Sm0ke

Posted on • Originally published at docs.deploypro.dev

Bash, the magic that empowers the Cloud

Hello Coders!

This article is not about ML, Generative AI, automation, or any other hot topic. It’s about Bash, also known as Bourne Again Shell the workhorse that empowers Cloud Computing and Services like AWS, GCP, Azure or Digital Ocean.

🚀 Provided by DeployPRO - Deployment process simplified — Works with AWS, DO, GCP and Azure

With decent Bash knowledge, anyone can manage a Linux system with ease, deploy projects LIVE, schedule backups, and execute maintenance tasks using the CRON System. If this sounds like something useful, let’s move forward and point out a few Bash-related topics and tools.


What is Bash?​

Bash, short for “Bourne Again Shell” is a command-line interface and scripting language used in Unix-like operating systems. It provides a way to interact with the operating system by running commands, and it also supports writing scripts to automate tasks.

Getting Started

  • Open a terminal on your Linux system. This is where you’ll interact with Bash.
  • Type a command and press Enter to execute it. For example, ls lists files in the current directory.
  • Arguments can be added after the command, like ls -l to list files in long format.

Basic Commands

  • ls: List files and directories in the current location.
  • cd: Change directory.
  • pwd: Print the current working directory.
  • mkdir: Create a new directory.
  • touch: Create an empty file.
  • cp: Copy files or directories.
  • mv: Move or rename files or directories.
  • rm: Remove files or directories (use with caution).

Variables

Bash uses variables to store and manipulate data.

name="John"
echo "Hello, $name!"
Enter fullscreen mode Exit fullscreen mode

Input and Output

  • echo: Print text to the screen.
  • read: Read input from the user.
echo "What's your name?"
read name
echo "Hello, $name!"
Enter fullscreen mode Exit fullscreen mode

Conditional Statements

Bash supports if statements for conditional execution.

age=25
if [ "$age" -lt 18 ]; then
    echo "You're a minor."
else
    echo "You're an adult."
fi
Enter fullscreen mode Exit fullscreen mode

Loops

for fruit in apple banana orange; do
    echo "I like $fruit."
done
Enter fullscreen mode Exit fullscreen mode

Functions

greet() {
    echo "Hello, $1!"
}
greet "Alice"
Enter fullscreen mode Exit fullscreen mode

Simple Calculator Script

#!/bin/bash
read -p "Enter first number: " num1
read -p "Enter second number: " num2
sum=$((num1 + num2))
echo "Sum: $sum"
Enter fullscreen mode Exit fullscreen mode

Bash & Linux Tools

This section presents a few tools we can use to manage different aspects of a Linux box like backups, text browsing, and file editing.

Cron System

The cron system is a time-based job scheduling mechanism in Unix-like operating systems. It allows you to automate repetitive tasks by scheduling them to run at specific intervals. The term “cron” comes from the Greek word “chronos,” meaning time.

Components of the Cron System

  • Cron Daemon (cron): The cron daemon is a background service that manages the scheduling and execution of cron jobs. It constantly checks the system's crontab files to determine when to run scheduled tasks.
  • Cron Jobs: A cron job is a command or script that you want to run automatically at a specified time or interval. Each cron job consists of a command and a schedule defined by cron syntax.

Cron Syntax: Cron uses a specific syntax to define when a job should be executed. The syntax consists of five fields (plus an optional command field):

* * * * * command_to_run
| | | | |
| | | | +----- Day of the week (0 - 6) (Sunday = 0)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Enter fullscreen mode Exit fullscreen mode

Cron Samples

  • Run a Script Every Night at Midnight
0 0 * * * /path/to/script.sh
Enter fullscreen mode Exit fullscreen mode
  • Send System Statistics Every Hour
0 * * * * /usr/bin/collect_stats.sh
Enter fullscreen mode Exit fullscreen mode

cURL — Client URL

curl (short for "Client URL") is a command-line tool and library for transferring data with URLs. It supports various protocols, including HTTP, HTTPS, FTP, FTPS, SCP, SFTP, LDAP, and more. curl is widely used to interact with web services, download files, and perform data transfers from the command line in Unix-like operating systems.

cURL Samples

  • Downloading a File
curl -O https://www.example.com/file.txt
Enter fullscreen mode Exit fullscreen mode
  • Fetching Only Headers
curl -I https://www.example.com
Enter fullscreen mode Exit fullscreen mode
  • Sending POST Data
curl -X POST -d "key=value" https://www.example.com/post_endpoint
Enter fullscreen mode Exit fullscreen mode

Gzip

gzip is a command-line utility used to compress and decompress files in Unix-like operating systems. It is named after the "GNU zip" project, which aims to provide a free and efficient replacement for the older compress utility.

Basic Usage

gzip [options] file
Enter fullscreen mode Exit fullscreen mode

Gzip Samples

  • Compressing a File
gzip file.txt
Enter fullscreen mode Exit fullscreen mode
  • Decompressing a File
gzip -d file.txt.gz
Enter fullscreen mode Exit fullscreen mode
  • Testing Compressed Files
gzip -t file.txt.gz
Enter fullscreen mode Exit fullscreen mode

gzip is commonly used to compress individual files and is often used in combination with other tools and utilities. It produces files with the .gz extension and these compressed files can save disk space and accelerate data transfer when distributing or storing files.


Lynx

lynx is a text-based web browser that allows users to browse the internet and view web pages from the command line in Unix-like operating systems. It's a versatile tool for accessing information from websites without requiring a graphical user interface.

While it doesn’t render images or complex layouts like modern graphical browsers, it’s useful for quick access to text-based content, checking links, and accessing web resources in a terminal environment.

Basic Usage

lynx [options] [URL]
Enter fullscreen mode Exit fullscreen mode

Lynx Samples

  • Viewing a Web Page
lynx https://www.example.com
Enter fullscreen mode Exit fullscreen mode
  • Viewing Page Source
lynx -source https://www.example.com
Enter fullscreen mode Exit fullscreen mode
  • Viewing Plain Text Version
lynx -dump https://www.example.com
Enter fullscreen mode Exit fullscreen mode

Rsync

Using the rsync command in Bash, you can synchronize the contents of a directory between two locations, either locally or across different machines over a network.

Basic Usage

rsync [options] source_directory destination
Enter fullscreen mode Exit fullscreen mode

Rsync Samples

  • Local Sync
rsync -av source/ destination/
Enter fullscreen mode Exit fullscreen mode
  • Remote Sync over SSH
rsync -av source/ user@remote_host:destination/
Enter fullscreen mode Exit fullscreen mode
  • Excluding Files with Progress Indicators
rsync -av --progress --exclude '*.txt' source/ destination/
Enter fullscreen mode Exit fullscreen mode

These examples cover some of the common use cases for rsync. The command offers many more options and functionalities for more complex synchronization scenarios.


Wget

wget is a command-line utility used for downloading files from the internet. It supports various protocols, including HTTP, HTTPS, FTP, and FTPS. wget is commonly used to retrieve files and web content from the command line in Unix-like operating systems.

Basic Syntax

wget [options] [URL]
Enter fullscreen mode Exit fullscreen mode

Wget Samples

  • Downloading with Custom Output Name
wget -O output.txt https://www.example.com/file.txt
Enter fullscreen mode Exit fullscreen mode
  • Recursively Downloading a Website
wget -r https://www.example.com
Enter fullscreen mode Exit fullscreen mode

wget is a reliable tool for downloading files and web content from the command line. Its simplicity and range of features make it a preferred choice for tasks that involve fetching resources over the Internet.


Thanks for reading! For more resources and deployment assistance feel free to access:

  • 👉 Read more about Bash & related tools
  • 👉 Deploy Projects using your own cloud provider (AWS, GCP, Azure)

Top comments (5)

Collapse
 
manchicken profile image
Mike Stemle

This article is great! I love shell scripting, and I’m glad to see I’m not alone.

The one thing to keep in mind is that a lot of these commands are independent programs, distributed separately more or less.

For example, if you go to Linux, some commands will have a set of arguments, but on BSD or macOS they may have different arguments, even though you’re still using the same version of Bash. That’s because things like ls aren’t just commands in Bash, they’re their own program, separate and independent from Bash.

Keep posting stuff like this, it’s marvelous and it warms my heart to see more stuff like this. These are essential skills, and they’re the fundamentals of unix and linux.

Thank you.

Collapse
 
sm0ke profile image
Sm0ke

Ty Mike! :)

Collapse
 
appseedprojects5 profile image
AppSeed Generated Projects

Old-school stuff.

Collapse
 
sm0ke profile image
Sm0ke

Ty 🚀🚀

Collapse
 
arzguu profile image
arzgu • Edited

متاورس چیست؟

dev-to-uploads.s3.amazonaws.com/up...