DEV Community

Mitch Chimwemwe Chanza
Mitch Chimwemwe Chanza

Posted on

The Power Of Bash Scripting

When it comes to System administration there is no boring thing like going through tones of commands to accomplish a task. As a System administrator, Your terminal, with its famous color black, is the tool that makes everything possible.

Bash scripting is a powerful tool for automating tasks and making them more efficient. Bash is a Unix shell, which is a command-line interface for interacting with an operating system. Bash scripts are written in the Bash programming language and are used to automate tasks that would otherwise be performed manually on the command line.

One of the key benefits of bash scripting is that it allows you to automate tasks and processes that would be difficult or time-consuming to perform manually. For example, you could use a bash script to automate the process of creating and managing user accounts, or to automate the process of backing up and restoring files.

Here is an example of a bash script that automates the process of creating and managing user accounts:

#!/bin/bash

# Check if the script is being run as root
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

# Prompt for the username and password
read -p "Enter the username: " username
read -sp "Enter the password: " password
echo

# Create the user account
useradd -m $username
echo "$password" | passwd "$username" --stdin

# Add the user to the sudo group
usermod -aG sudo $username
Enter fullscreen mode Exit fullscreen mode

This script prompts the user for a username and password, and then creates a new user account with the specified username and password. The user is also added to the sudo group, which gives them the ability to execute commands with root privileges.

To automate the process of backing up and restoring files, you can use a bash script like this:

#!/bin/bash

# Check if the script is being run as root
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

# Prompt for the source and destination directories
read -p "Enter the source directory: " src
read -p "Enter the destination directory: " dest

# Create the destination directory if it doesn't exist
if [ ! -d "$dest" ]; then
  mkdir "$dest"
fi

# Perform the backup
rsync -av "$src" "$dest"

Enter fullscreen mode Exit fullscreen mode

This script prompts the user for the source and destination directories, and then uses the rsync command to copy all files and directories from the source to the destination. The -a option tells rsync to preserve file permissions and other metadata, and the -v option enables verbose output so that you can see the progress of the backup.

I hope these examples give you a sense of the kind of tasks that can be automated with bash scripts.

In addition to automation, bash scripts can also be used to customize the command line interface and make it more user-friendly. For example, you could use a bash script to create custom aliases or functions that simplify complex commands, or to create a custom prompt that displays useful information about the current system state.

Overall, the power of bash scripting lies in its ability to automate tasks and processes, making them more efficient and less prone to error.

Top comments (2)

Collapse
 
webjose profile image
José Pablo Ramírez Vargas • Edited

I would say Scripting, in general, regardless of the nature of the script. Now, which script technology to use? I guess that depends a lot.

If you care for performance, in my limited experience, bash is slower than PowerShell by a lot. For example, I created one bash and one PowerShell script that do the same job (they are here). You clearly see how slow bash is.

Collapse
 
mitch1009 profile image
Mitch Chimwemwe Chanza

You are right. i have come across such cases. its realy slow in conparison to powershell.