Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Linux IntroductionChapter 5. Redirection, Pipe and Shell Script

Shell Script

Shell Script

Introduction to Shell Scripting

A shell script is a text file that contains a sequence of commands for a Linux or Unix operating system. In the command line, you can usually run only a single command. Using the shell script, you can run multiple commands as one operation without another human intervention during the process.

How to develop a shell script?

There are some rules to develop shell scripts.

File name

Add .sh at the end of the file name as a file extension.

Document structure

  • Declaration: You need to specify which shell will be used for the shell script.
  • Comment: You can add comments with # at the beginning of comments

Here is an example of creating a shell script using some commands discussed in the previous section.

Use vim to create a shell script file named test.sh.

Command Line - INPUT
vim test.sh

In the document, add a declaration and commands with comments like the one below. Press the i key to switch to the insert mode. Next, copy the code below and paste it into the file. After pasting the code, press the esc key followed by the : key and save the file by pressing the w + q keys. To learn how to use Vim, check Chapter 3. Vim Editor.

test.sh
#!/bin/bash

# display product items
cat purchase_product.txt

# extract unique items and count duplicated items
sort purchase_product.txt | uniq -c

Add an execution permission

Usually, execution permission is not added when you create a file. In order to make the script executable, you need to add an execution permission to the file. To grant permission to the owner of the file, run the chmod u+x command followed by the file path.

For the test.sh file, grant the execution permission to the owner user.

Command Line - INPUT
sudo chmod u+x test.sh

Run a shell script

To run a shell script, you just need to specify the file path of the shell script like the one below.

Command Line - INPUT
./test.sh

You'll see the original list of purchased products followed by unique product items with numbers of duplicated items.

Command Line - RESPONSE
Apple
Orange
Apple
Apple
Bananas
Apple
Grapes
Orange
Bananas
Grapes
Grapes
Bananas
Bananas
Apple
Grapes
      5 Apple
      4 Bananas
      4 Grapes
      2 Orange

Call another shell script from a shell script

You can call another shell script from a shell script. This is like creating a child process from a parent process.

Call another shell script from a shell script

To call another shell script, you just need to add a shell script execution command to the shell script.

For example, create a new shell script file named test_child.sh and edit the file as shown below.

Note: the echo command will be explained in the next section.

Command Line - INPUT
vim test_child.sh
test_child.sh
#!/bin/bash

echo "----------------------------------------------"
echo "Unique Product List with # of Duplicated Items"

Also, copy the test.sh file and create a new file test_parent.sh. Then, edit the file to add a command to execute test_child.sh.

Command Line - INPUT
cp test.sh test_parent.sh
vim test_parent.sh

Add ./test_child.sh between the cat command and the sort command.

test_parent.sh
#!/bin/bash
  
# display product items
cat purchase_product.txt

./test_child.sh

# extract unique items and count duplicated items
sort purchase_product.txt | uniq -c

To execute the shell scripts, add permissions and run the test_parent.sh like shown below.

Command Line - INPUT
sudo chmod u+x test_parent.sh
sudo chmod u+x test_child.sh
./test_parent.sh

You can see that test_parent.sh successfully called test_child.sh.

Command Line - RESPONSE
Apple
Orange
Apple
Apple
Bananas
Apple
Grapes
Orange
Bananas
Grapes
Grapes
Bananas
Bananas
Apple
Grapes
----------------------------------------------
Unique Product List with # of Duplicated Items
      5 Apple
      4 Bananas
      4 Grapes
      2 Orange

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

How to Make a Pull Request on GitHub

Request for Review and Merge – Pull Request

Adding Files to the Staging Area with git add

Add Files to Staging Area – git add

Launching Your Git Project: Key Steps Explained

Project Initiator – Key Steps To Launch Git Project

Modifying Access Modes with chmod Command

chmod (Change Access Mode)

Exploring GitHub's Additional Features

GitHub Other Features

How to Make a Pull Request on GitHub

Request for Review and Merge – Pull Request

Adding Files to the Staging Area with git add

Add Files to Staging Area – git add

Launching Your Git Project: Key Steps Explained

Project Initiator – Key Steps To Launch Git Project

Modifying Access Modes with chmod Command

chmod (Change Access Mode)

Exploring GitHub's Additional Features

GitHub Other Features

Tags:

Shell Script

Bash

Linux Introduction
Course Content

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server