Linux Environment Lab - Shell Script
Linux Environment Lab - Shell Script
1
© 2021 C-DAC, Hyderabad
Table of Contents
Objective 2
Prerequisites 3
Problem Statement 3
Summary 3
Fundamental concepts 3
References 9
2
© 2021 C-DAC, Hyderabad
1. Objective
2. Prerequisites
Prerequisites Version
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 4 Assignment
Step 5 Solution
5. Fundamental Concepts
Shell Scripting
3
© 2021 C-DAC, Hyderabad
6. Template for each step
● 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
● 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