0% found this document useful (0 votes)
9 views9 pages

Linux Environment Lab - Shell Script

Lab Manual
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Linux Environment Lab - Shell Script

Lab Manual
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Linux Environment Lab 3

1
© 2021 C-DAC, Hyderabad
Table of Contents
Objective 2

Prerequisites 3

Problem Statement 3

Summary 3

Fundamental concepts 3

Template for each step 4


Step-1: Create a Shell Script 4
Step-2 : Save and Execute Shell Script 5
Step-3 : Another way of executing script 6
Step-4 : Assignment 6
Step-5 : Solution 6

References 9

2
© 2021 C-DAC, Hyderabad
1. Objective

Understanding the Linux Architecture, File System, Commands etc.

2. Prerequisites

Prerequisites Version

Operating System Linux (Any flavor)

3. Problem Statement
To understand the basics of Shell Scripting and how we can use it to automate the
repetitive tasks.

4. Summary

Steps Description

Step 1 Create a shell script

Step 2 Save & execute a shell script

Step 3 Another way of executing shell script

Step 4 Assignment

Step 5 Solution

5. Fundamental Concepts

Shell Scripting

A shell script is a computer program designed to be run by the Unix shell, a


command-line interpreter. Typical operations performed by shell scripts include file
manipulation, program execution, and printing text. The commands and syntax of
the shell script are the same as that entered at the command line. Because of this,
there is no need to switch to a completely different syntax. It is much faster to write
a code in shell script than in other programming languages

3
© 2021 C-DAC, Hyderabad
6. Template for each step

Step 1: Creating a Shell Script


● Create a file hello.sh using command touch

● Open the hello.sh file in nano editor by executing below command and
write the program given in below screenshot

4
© 2021 C-DAC, Hyderabad
Step 2: Execute the Script
● Save the file by pressing keys Ctrl + O then Ctrl + X to exit from the editor
and, execute the file using command: bash hello.sh

Step 3: Another way of executing the script


● Make the file executable. First, check the current file permissions using
command ls -l hello.sh

● Now, we will give execute permission to all users for hello.sh file using
command chmod 0755 hello.sh and then check the file permission using
command ls -l

● Now, all users have a execute permission to file hello.sh, we will run the
file by writing its name and location.
5
© 2021 C-DAC, Hyderabad
Step 4: Assignment
Create a shell script which will print the below system information on the terminal like:
● Hostname
● File System disk space usage
● Free and used memory in the system
● System uptime and load
● All logged-in users

Hint: You can take a help of below commands. First execute each command on the terminal
and see what each command is doing and later you can use it in the shell script.
● who, hostnamectl, echo, free, df -h, uptime

Step 5: Solution
● Create a file with name sysinfo.sh using touch command

6
© 2021 C-DAC, Hyderabad
● Open the file using nano and copy and paste the below code in it.

#!/bin/bash
# Display Hostname information:
echo "[***** HOSTNAME INFORMATION *****]"
hostnamectl
echo ""
# Display File system disk space usage:
echo "[***** FILE SYSTEM DISK SPACE USAGE *****]"
df -h
echo ""
# Display Free and used memory in the system:
echo "[***** FREE AND USED MEMORY *****]"
free
echo ""
# Display the System uptime and load:
echo "[***** SYSTEM UPTIME AND LOAD *****]"
uptime
echo ""
# Display Logged-in users:
echo "[***** CURRENTLY LOGGED-IN USERS *****]"
who
echo ""

7
© 2021 C-DAC, Hyderabad
● Save the file by pressing key Ctrl + O and Enter and execute the script

8
© 2021 C-DAC, Hyderabad
7. References
● https://en.wikipedia.org/wiki/Shell_script

9
© 2021 C-DAC, Hyderabad

You might also like