cancel
Showing results for 
Search instead for 
Did you mean: 
Chetan_Tiwary_
Community Manager
Community Manager
  • 833 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
  • 611 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
  • 704 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

11 Replies
Trevor
Starfighter Starfighter
Starfighter
  • 636 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
  • 633 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
  • 575 Views

@TM yes it does the job, wonderful!

0 Kudos
Chetan_Tiwary_
Community Manager
Community Manager
  • 575 Views

@Trevor the requirement is like this

1 ":" abcd

2 ":" efgh

 

cat -n actors.txt only gives the number.

 

Trevor
Starfighter Starfighter
Starfighter
  • 388 Views

I thought that assignment was too straightforward!!!  
I completely ignored the appearance of the colon (:)
in the output.  No wonder I could never gain entry into
the consulting world

Well, the awk tool has already been used by TM, so
I'll take another route.  It's some extra heavy lifting, and
not an approach that would necessarily be seen in the
practical world.  However, it just might add a little something
to a reader's knowledgebase. 

Here goes:

$   nl actors.txt  | sed  -r  's/([[:space:]]){5}([[:digit:]])/\2 :/'

 

As a result of my going down this road, I think I'll start a
little series on this all-important text manipulating tool. 
I'm hoping that it will build some muscle with sed, as well
as getting some reps in with regular expressions!

 

 

Trevor "Red Hat Evangelist" Chandler
0 Kudos
Trevor
Starfighter Starfighter
Starfighter
  • 626 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 $charac 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
  • 574 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

 

Trevor
Starfighter Starfighter
Starfighter
  • 376 Views

Oh brother, I have a very bad habit rearing its ugly head.
The bad habit?  Not proofing my work!!!!

 

Below is my original script - minus the comments:

read -n 1 charac

case $charact in         
  [aeiou] | [AEIOU])
    echo -e '\t is a vowel';;
  *)
    echo -e "\t is a consonant";;
esac

 

I have put in BOLD font, the issue of my script
not doing what I intended it to do.  The error is
so elementary, even a Freshman Summer intern
wouldn't oversight what I did.

On the line with the "read" command, I used the
variable name of "charac".  However, for some
reason, on the first line of my "case" construct,
I decided to add the letter 't' to the variable name
of "charac" - resulting in the name "charact".

Wow!  Ouch!  Wow!  Ouch!

As any Linux 101 student would know, from their
2nd week of being exposed to shell scripts, the
variable names of that "read" command and that
"case" construct, MUST BE the same.  So, either
I have reverted to a Linux 101 level, or I'm just 
plain negligent in performing my duties.  Guilty as
charged of negligence!!!!!!

And so, in the real world, this type of negligence
could easily result in 2 to 4 hours of wasted time.
If I'm a repeat offender, this can't be tolerated.
When it's time for my performance review, I'll 
wonder why I received a rating of 4 ( 1 to 20 scale),
and no increase in pay.  Simply unacceptable!!!
Not knowing how to do something gets a pass. 
Typos, that would have been avoided by proofreading,
are an act of negligence!!!

Moral of this story, exercise ZERO TRUST with
anything that I provide.  In other words, execute
everything!!!!  How does the saying go:
VERIFY!  VERIFY!  VERIFY!  

 

The script, in its correct form is shown below:

read -n 1 charac

case $charac in         
  [aeiou] | [AEIOU])
    echo -e '\t is a vowel';;
  *)
    echo -e "\t is a consonant";;
esac

 

Okay, let me stop here before I make
another blunder

 

Trevor "Red Hat Evangelist" Chandler
0 Kudos
Trevor
Starfighter Starfighter
Starfighter
  • 612 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
Join the discussion
You must log in to join this conversation.