cancel
Showing results for 
Search instead for 
Did you mean: 
Chetan_Tiwary_
Community Manager
Community Manager
  • 224 Views

Red Hat Linux Interview Series 30

Jump to solution

Q.) cat actors.txt 

Chetan_Tiwary__0-1730751140363.png

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.

2 Solutions

Accepted Solutions
Trevor
Starfighter Starfighter
Starfighter
  • 98 Views

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 "Red Hat Evangelist" Chandler

View solution in original post

Trevor
Starfighter Starfighter
Starfighter
  • 97 Views

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   I'll have mine increment by 1 anyway - starting from 0!

 

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

=====================================

 

 

Trevor "Red Hat Evangelist" Chandler

View solution in original post

9 Replies
Trevor
Starfighter Starfighter
Starfighter
  • 123 Views

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

 

Trevor "Red Hat Evangelist" Chandler
TM
Flight Engineer Flight Engineer
Flight Engineer
  • 120 Views

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

Chetan_Tiwary_
Community Manager
Community Manager
  • 62 Views

@TM yes it does the job, wonderful!

0 Kudos
Chetan_Tiwary_
Community Manager
Community Manager
  • 62 Views

@Trevor the requirement is like this

1 ":" abcd

2 ":" efgh

 

cat -n actors.txt only gives the number.

 

0 Kudos
Trevor
Starfighter Starfighter
Starfighter
  • 113 Views

 

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 "Red Hat Evangelist" Chandler
Chetan_Tiwary_
Community Manager
Community Manager
  • 61 Views

@Trevor I did try your script in my VM but it does not seem to work - have I typed the same as yours ?

Chetan_Tiwary__0-1730918462031.png

 

0 Kudos
Trevor
Starfighter Starfighter
Starfighter
  • 99 Views

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 "Red Hat Evangelist" Chandler
Chetan_Tiwary_
Community Manager
Community Manager
  • 60 Views

@Trevor This works like a charm !! great job and it is easily understandable for beginners as well!

0 Kudos
Trevor
Starfighter Starfighter
Starfighter
  • 98 Views

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   I'll have mine increment by 1 anyway - starting from 0!

 

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

=====================================

 

 

Trevor "Red Hat Evangelist" Chandler
Join the discussion
You must log in to join this conversation.