Q.) cat actors.txt
print the file with its line count as shown below :
1 : Tom Hanks
2 : Leonardo DiCaprio ....likewise
Q.) Write a Bash script that takes a single character as input from the user. The script should determine whether the input character is a vowel or a consonant. Adjust for both lowercase and uppercase condition.
Q.) As a system administrator, you need to script a way to quickly gather and display essential server metrics for routine monitoring. This information is crucial for assessing server health and performance. Include the metrics like Date, Uptime, Memory Usage, Network interface along with IPs, CPU only report with 2 seconds interval and 2 times, top 5 processes by memory usage.
Bonus Q.) Write a Bash script that continuously displays a countdown starting from 0, incrementing by 1 in each iteration. The script should run indefinitely until manually terminated.
Level L1 and above
I'll be posting a series of Linux-related questions covering various skill levels. Feel free to share your insights and expertise. Your contributions will benefit learners at all stages, from those in current roles to those preparing for Linux interviews.
Q.) As a system administrator, you need to script a way to quickly gather and display essential server metrics for routine monitoring. This information is crucial for assessing server health and performance. Include the metrics like Date, Uptime, Memory Usage, Network interface along with IPs, CPU only report with 2 seconds interval and 2 times, top 5 processes by memory usage.
Solution:
------------------------------------------------------------------------------------------------------
# This script will require the installation of the "sysstat" rpm package
echo "The current date on this system is: " $(date)
echo "The current UPTIME information for this system is: " $(uptime); sleep 10
clear
echo Memory Usage stats on this system are:
echo $(cat /proc/meminfo | sed -n "1,15p"); sleep 10
echo -e '\n'Memory Usage stats continued...:
echo $(cat /proc/meminfo | sed -n "16,30p"); sleep 10
echo -e '\n'Memory Usage stats continued...:
echo $(cat /proc/meminfo | sed -n "31,45p"); sleep 10
echo -e '\n'Final output of Memory Usage stats:
echo $(cat /proc/meminfo | sed -n "46,60p"); sleep 10
clear
echo Network interface/IP information for this system is:
ip a s; sleep 10
clear
echo CPU only stats for this system:
sar -u 2 2; sleep 10
clear
echo Finally, the top 5 processes on this system, by memory usage are:'\n'
ps -eo pid,%mem,cmd --sort=-%mem | head -6; sleep 10
clear
echo Want to see these metrics one more time? Just run the script again!!!!
-----------------------------------------------------------------------------------------------------
That's all!!!
Bonus Q.) Write a Bash script that continuously displays a countdown starting from 0, incrementing by 1 in each iteration. The script should run indefinitely until manually terminated.
Observation: If the script is going to be counting down, shouldn't it be decrementing instead of incrementing
Solution:
=================================
# This script will run until the cows come home, the electricity is turned off,
# or which ever comes first - unless there is manual intervention on the
# part of the user to terminate it!
val=0
while [ true ]
do
echo The value of val is $val
val=$((val+1))
sleep 1
done
=====================================
Q.) cat actors.txt
I had to study (not read) this question because surely I was oversighting
something. After walking around it, I didn't see a trap door, so here's my
response:
$ cat -n actors.txt
Okay, where's the catch
In order to respect the output that has the line number followed by a colon then the line content (all without space) the below will do
awk ' { print NR":"$0 } ' actors.txt
@TM yes it does the job, wonderful!
Q.) Write a Bash script that takes a single character as input from the user. The script should determine whether the input character is a vowel or a consonant. Adjust for both lowercase and uppercase condition.
One approach:
==========================================================
echo Input a single alpha character: a or b or c ...z A or B or C ...Z
echo There is no need to press the Enter key after entering the character
echo Depending on the alpha character that you input, the script will inform
echo you if that character is a vowel or a consonant
echo '\n'
read -n 1 charac
case $charact in
[aeiou] | [AEIOU])
echo -e '\t is a vowel';;
*)
echo -e "\t is a consonant";;
esac
============================================================
Being the lazy programmer that I am, my script contains not even a
single comment
@Trevor I did try your script in my VM but it does not seem to work - have I typed the same as yours ?
Q.) As a system administrator, you need to script a way to quickly gather and display essential server metrics for routine monitoring. This information is crucial for assessing server health and performance. Include the metrics like Date, Uptime, Memory Usage, Network interface along with IPs, CPU only report with 2 seconds interval and 2 times, top 5 processes by memory usage.
Solution:
------------------------------------------------------------------------------------------------------
# This script will require the installation of the "sysstat" rpm package
echo "The current date on this system is: " $(date)
echo "The current UPTIME information for this system is: " $(uptime); sleep 10
clear
echo Memory Usage stats on this system are:
echo $(cat /proc/meminfo | sed -n "1,15p"); sleep 10
echo -e '\n'Memory Usage stats continued...:
echo $(cat /proc/meminfo | sed -n "16,30p"); sleep 10
echo -e '\n'Memory Usage stats continued...:
echo $(cat /proc/meminfo | sed -n "31,45p"); sleep 10
echo -e '\n'Final output of Memory Usage stats:
echo $(cat /proc/meminfo | sed -n "46,60p"); sleep 10
clear
echo Network interface/IP information for this system is:
ip a s; sleep 10
clear
echo CPU only stats for this system:
sar -u 2 2; sleep 10
clear
echo Finally, the top 5 processes on this system, by memory usage are:'\n'
ps -eo pid,%mem,cmd --sort=-%mem | head -6; sleep 10
clear
echo Want to see these metrics one more time? Just run the script again!!!!
-----------------------------------------------------------------------------------------------------
That's all!!!
@Trevor This works like a charm !! great job and it is easily understandable for beginners as well!
Bonus Q.) Write a Bash script that continuously displays a countdown starting from 0, incrementing by 1 in each iteration. The script should run indefinitely until manually terminated.
Observation: If the script is going to be counting down, shouldn't it be decrementing instead of incrementing
Solution:
=================================
# This script will run until the cows come home, the electricity is turned off,
# or which ever comes first - unless there is manual intervention on the
# part of the user to terminate it!
val=0
while [ true ]
do
echo The value of val is $val
val=$((val+1))
sleep 1
done
=====================================
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.