Shell Scripting is the King of Devlopment

PIYUSH SHARMAPIYUSH SHARMA
3 min read

Shell Scripting

Shell scripting is a powerful process of automating day-to-day or regular activities on a Linux computer. It can be implemented on any AWS-hosted Linux machine or a Linux-hosted laptop. Here's an introduction to shell scripting and its commands:


Creating a File in Linux

touch first-shell-script.sh

This command creates a new file named first-shell-script.sh.

To verify if the file has been created:

ls

The ls command lists all files and directories in the current directory.

Use ls -ltr for detailed information about files and directories, including:

  1. Hard links

  2. Owner and group

  3. File size in bytes

  4. Date and time of last modification

  5. Name of the file/directory

To get detailed information about any command, use:

man ls

The man command provides a manual or description of the specified command.


Editing Files

Use vim or vi to edit files. vim needs to be installed, whereas vi is pre-installed.

Example:

vim first-shell-script.sh

Inside the editor:

  • Press Esc and then i to insert text.

  • To save changes, press Esc and type :wq!.

  • To exit without saving, use :q!.

To view the file content:

cat first-shell-script.sh

Writing a Basic Shell Script

Start a script with a shebang:

#!/bin/bash

The shebang (#!) defines the interpreter for the script. Examples include:

  • /bin/bash for Bash

  • /bin/sh for SH

  • /bin/ksh for KSH

Example script content:

#!/bin/bash
echo "My name is Piyush"

To execute the script:

sh first-shell-script.sh

or

./first-shell-script.sh

Permissions with chmod

The chmod command changes file permissions. Permissions are divided into three categories:

  1. Root user/administrator

  2. Group

  3. Others

Each permission is represented numerically:

  • 4 = Read

  • 2 = Write

  • 1 = Execute

Example:

chmod 777 first-shell-script.sh

This grants read, write, and execute permissions to all users.


Useful Commands

  • mkdir <directory-name>: Create a directory.

  • cd <directory-name>: Change directory.

  • rm -rf <file/directory>: Force delete files or directories.

  • history: View the history of all executed commands.

To write comments in shell scripts:

# This is a comment

Debugging Mode

Enable debug mode in your script using:

set -x

Example:

set -x
echo "Print disk space"
df -h
echo "Print memory"
free -g

Process Management

  • ps -ef: List all running processes with detailed information.

  • ps -ef | grep <keyword>: Filter processes containing a specific keyword.

  • | (Pipe): Pass the output of one command as input to another command.

Example:

ps -ef | grep "amazon"

Summary

  • Creating and editing files: Using touch, vim, and cat.

  • Permissions: Managed with chmod.

  • Debugging and process management: Using set -x and ps -ef.

  • Integration: The pipe (|) command connects outputs and inputs.

Mastering these commands and techniques makes shell scripting a valuable tool for Linux automation.

Follow me on:

0
Subscribe to my newsletter

Read articles from PIYUSH SHARMA directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

PIYUSH SHARMA
PIYUSH SHARMA

"Passionate DevOps enthusiast, automating workflows and optimizing infrastructure for a more efficient, scalable future."