DEV Community

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

Posted on Edited 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!"
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

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!"
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

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
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

Loops

for fruit in apple banana orange; do
    echo "I like $fruit."
done
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

Functions

greet() {
    echo "Hello, $1!"
}
greet "Alice"
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

Simple Calculator Script

#!/bin/bash
read -p "Enter first number: " num1
read -p "Enter second number: " num2
sum=$((num1 + num2))
echo "Sum: $sum"
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

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)
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

Cron Samples

  • Run a Script Every Night at Midnight
0 0 * * * /path/to/script.sh
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
  • Send System Statistics Every Hour
0 * * * * /usr/bin/collect_stats.sh
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

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
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
  • Fetching Only Headers
curl -I https://www.example.com
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
  • Sending POST Data
curl -X POST -d "key=value" https://www.example.com/post_endpoint
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

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
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

Gzip Samples

  • Compressing a File
gzip file.txt
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
  • Decompressing a File
gzip -d file.txt.gz
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
  • Testing Compressed Files
gzip -t file.txt.gz
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

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]
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

Lynx Samples

  • Viewing a Web Page
lynx https://www.example.com
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
  • Viewing Page Source
lynx -source https://www.example.com
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
  • Viewing Plain Text Version
lynx -dump https://www.example.com
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

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
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

Rsync Samples

  • Local Sync
rsync -av source/ destination/
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
  • Remote Sync over SSH
rsync -av source/ user@remote_host:destination/
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
  • Excluding Files with Progress Indicators
rsync -av --progress --exclude '*.txt' source/ destination/
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

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]
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

Wget Samples

  • Downloading with Custom Output Name
wget -O output.txt https://www.example.com/file.txt
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
  • Recursively Downloading a Website
wget -r https://www.example.com
🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung 🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung

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)

🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
 
appseedprojects5 profile image
AppSeed Generated Projects •

Old-school stuff.

🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
 
sm0ke profile image
Sm0ke •

Ty 🚀🚀

🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
 
sm0ke profile image
Sm0ke •

Ty Mike! :)

🔴 Live TV: smke/bash the magic that empowers the cloud - Jadwal Siaran Langsung
 
arzguu profile image
arzgu • • Edited

متاورس چیست؟

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