Advertisement
vasilev5

Untitled

Feb 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.30 KB | None | 0 0
  1. #! /bin/sh
  2.  
  3.  
  4. #Shell script program that takes user input and displays information about the system.
  5.  
  6.  
  7. #Prints choices to the console for the user to select.
  8. echo "Choose and option from below:"
  9. echo "1: Display information about the CPU"
  10. echo "2: Display the interrupts on the system"
  11. echo "3: Display the names of the output devices and the number of megabits read."
  12. echo "4: Display the PID of a process that is running on the server and its status."
  13. echo "-1: Exit"
  14. echo
  15.  
  16.  
  17.  
  18. #Takes user input from the user.
  19. read userInput
  20.  
  21.  
  22. #While statement executes
  23. while [ $userInput != "-1" ]
  24. do
  25.  
  26.    
  27.     echo "Choose an option from below:"
  28.     echo "1: Display information about the CPU."
  29.     echo "2: Display the interrupts on the system."
  30.     echo "3: Display the names of the output devices and the number of megabits read."
  31.     echo "4: Display the PID of a process that is running on the server and its status."
  32.     echo "-1: Exit"
  33.     read userInput #Takes input from the user.
  34.  
  35.     if [ $userInput = "1" ]
  36.     then
  37.         echo
  38.         echo "***CPU Information***"
  39.         cat /proc/cpuinfo
  40.     elif [ $userInput = "2" ]
  41.     then
  42.         echo
  43.         echo "***Interrupts Info***"
  44.         cat /proc/interrupts
  45.     elif [ $userInput = "3" ]
  46.     then
  47.         echo
  48.         echo "***Names of output devices and number of megabits read***"
  49.         cat /proc/diskstats
  50.     elif [ $userInput = "4" ]
  51.     then
  52.         echo
  53.        
  54.  
  55.         ps > info.txt #information about current proccesses running is redirected to the info.txt file.    
  56.  
  57.         pidVar=`grep "bash" info.txt |  awk 'NR==2{ print $1}' info.txt` #Looks into the txt file for  the line that contains the  process called 'bash' and stores its process id into the pidVar to be used as a variable in the proc directory when searcing for information about the process.Furthermore the awk command combined with the 'NR==2' argument selects the first word in the second line of the document which is the process id of 'bash'.
  58.        
  59.                 echo
  60.                 pidString="PID:"
  61.                
  62.                 echo $pidString #Prints out PID string
  63.         echo $pidVar #Prints out the process PID
  64.         cat /proc/$pidVar/status | grep 'Name\|State' #Prints out lines that contain the words 'Name' and 'State'.
  65.         echo
  66.        
  67.     elif [ $userInput = "-1" ] #Terminates the while loop.
  68.     then
  69.         echo "You have successfully exited the loop!"
  70.         break
  71.     fi 
  72.  
  73.    
  74.  
  75. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement