Scripting of Shell Learning 1

Source: Internet
Author: User

Script Requirements:

After executing the script

Accepts input 1, displays the current time, and prompts you to continue the selection.

Accepts input 2, displays the CPU load, and prompts you to continue the selection.

Accepts input 3, displays the remaining memory, and prompts you to continue the selection.

Accept input 0 and exit the script.

·····················································································································

Ideas:

Show Current Time

[Email protected] ~]# date +%t
08:26:04

························································································································· ·······················································································

Uptime command can show 1) current system time;

2) The machine has been running time;

3) The current system has several user connections;

4) The average load of the system for the last 1 minutes, 5 minutes, 15 minutes of the machine.

As follows:

We can use awk to select the output of the uptime output.

························································································································· ···························································································

The FREE-M command can display memory usage in M, as well as swap partition swap usage, using awk to print out the information in the 4th column of the Mem line, which is the information we want.

························································································································· ·····························································································

As for whether to continue the selection, you can use the while loop. The final script is as follows:

1#!/bin/Bash2 #3  while["1"=="1" ]4  Do5     Clear6     Echo "~~~~~~~~~~~~~~~~~~~menu~~~~~~~~~~~~~~~~~~~~~~"7     Echo "1) time now"8     Echo "2) CPU Load"9     Echo "3) Memory free"Ten     Echo "0) Exit"   One     Echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" A     Echo "Please enter a chose[0~3]:" - Read NUM -      Case${num}inch the       1)Echo "Time now : ' Date +%t '" ;; -       2)Echo "CPU Load: ' Uptime |awk-f ' [:,] ' {print $6} '" ;; -       3)Echo "Memory Free: ' free-m |awk ' $1=="Mem:"{print $4} ' m" ;; -       0) exit;; +*)Echo "This chose was not in [0~3]."  -     Esac +     Echo-N"Do you continue [y/n]:" A Read Continue at     if["${contiune}"=="N"-O"${continue}"=="N" ] -      Then - Exit -     fi -  Done

Scripting of Shell Learning 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.