Linux Shell Scripting For Getting User and Server Details
Shell scripting is a way to write and execute scripts or programs using a command-line shell, like Bash or PowerShell. It allows you to automate tasks and perform various operations in a command-line environment.
With shell scripting, you can write a series of commands and instructions in a script file. These scripts can be used to combine existing commands, perform system administration tasks, manipulate files and directories, handle input/output operations, and more.
Here are some of the benefits of learning shell scripting:
- It can help you automate tasks and save time.
- It can help you integrate with other applications and services.
- It can help you create custom commands and tools.
- It can help you learn more about the Unix-like operating system.
Note: To run a shell script file, firstly the filename should be .sh and the file should have execute permissions.
In this shell Script, I have used some commands and explained them below:
1. echo
The “echo” command in Linux is used to display text or variables on the terminal. It is a simple and commonly used command for outputting information.
2. sleep
The “sleep” command in Linux is used to pause the execution of a script or a command for a specified amount of time. It allows you to introduce delays or time intervals within scripts or command sequences.
3. whoami
The “whoami” command is used to retrieve and display the username of the current user who is logged into the system. When you run the “whoami” command in the terminal, it will output the username associated with your current session.
4. date
The “date” command is used to display or set the system date and time. It provides various options to customize the output format and perform operations related to dates and times.
When you run the “date” command without any options, it will display the current system date and time in the default format.
5. awk
The “awk” command is a versatile text-processing tool used for extracting and manipulating data from files or input streams. It operates on a line-by-line basis, splitting each line into fields and applying specified patterns and actions to process the data.
The awk command provides powerful text-processing capabilities, allowing you to perform complex operations on structured or unstructured data. It is commonly used for data extraction, report generation, data transformations, and more in Linux command-line environments.
6. uptime
The “uptime” command provides information about the system’s uptime. When you run the “uptime” command in the terminal, it displays several pieces of information, including the current time, the system uptime, the number of logged-in users, and the system load average.
7. df -h
The “df -h” command is used to display information about disk space usage on file systems. It provides a summary of disk space usage in a human-readable format.
Here’s a breakdown of the command:
1. “df”: The command itself, which stands for “disk free.”
2. “-h”: An option that specifies the output should be displayed in a human-readable format, using units like “K” (kilobytes), “M” (megabytes), “G” (gigabytes), etc
8. xargs
The “xargs” command in Linux is used to build and execute commands from standard input. It takes input from standard input (stdin) or from a file and uses it as an argument to construct and execute new commands.
9. free -h
The “free -h” command in Linux is used to display information about the system’s memory usage. It provides a summary of the available, used, and total memory in a human-readable format.
Here’s a breakdown of the command:
1. “free”: The command itself, which is used to display memory usage information.
2. “-h”: An option that specifies the output should be displayed in a human-readable format, using units like “K” (kilobytes), “M” (megabytes), “G” (gigabytes), etc.
Shell Script for user data and server details
Step 1: Create a file with .sh extension and write your script
echo "************Shell Script of server details**************"
sleep 2
echo " "
echo "*********************************************************"
echo " "
sleep 1
echo "Welcome to 2023, by User:$(whoami)"
echo " "
echo "Today is:"
date | awk '{print $1 "/" $2 "/" $3}'
echo " "
echo "Time is:"
date | awk '{print $4}'
echo " "
echo "*********************************************************"
echo " "
sleep 1
echo "Server Uptime is:"
uptime
echo " "
echo "Top $1 last logins:"
last -a |head $1
echo " "
echo "*********************************************************"
echo " "
sleep 1
echo "My Server RAM(memory) Utilization:"
df -h | xargs | awk '{print "Free Disk Space is:" $11}'
df -h | xargs | awk '{print "Total Disk Space is:" $9}'
echo " "
echo "*********************************************************"
echo " "
sleep 1
echo "My Server RAM(memory) Utilization"
free -h | xargs | awk '{print "Free Memory is:" $10}'
df -h | xargs | awk '{print "Total Disk Space is:" $9}'
echo " "
echo "*********************************************************"
echo " "
sleep 1
echo "My Server RAM(memory) Utilization"
free -h | xargs | awk '{print "Free Memory is:" $10}'
free -h | xargs | awk '{print "Total Memory is:" $8}'
echo " "
echo "************Come afterwards and check me again ************"
Step 2: Give execute permission to the file
Step 3: Run the file using the command “./filename”
Happy Learning! :)
Thanks for reading. I hope you find this article useful.
Don’t forget to follow me to read more articles related to Cloud Engineering and DevOps.
Click here to check the LinkedIn Post