Exercises small shell script basis (b)

Exercises small shell script basis (b)

Write a memusage.sh, memory usage is calculated based on the results of the command Free

Ideas:

1: In the screen output "free" to see the results of the command

Exercises small shell script basis (b)

2: According to the results given that we need to use "total" and "free"

total: column shows the total available system physical memory and swap space.

free: column shows how much physical memory and swap space available for use.

Here we look at Mem (memory usage) can be.

3: We create a script but we just use ( "total" and "free") in free of Mem in how to write, here we have to use "grep (filtered)", the pipe symbol (|) and awk

grep: filtering keywords corresponding to the line, where we look at "Mem"

awk: column unit reads data filtering tools

wherein awk -F (-F) [delimiter is used to specify part is specified, the default separated by a space or tab], here we see the "free" we are not directly delimiter followed by a space on the back awk it.

awk '{prin $ 2}': this is a fixed format columns here that $ 2 for free in the total where you want to use it in a column just behind the "$" write a few numbers. But here we begin to run from the first column is not to be confused with Mem.

Exercises small shell script basis (b)

4: We started to think about how to use the calculation to show the occupancy rate in the shell can not come out like we used to use handwriting or using a calculator as a direct knock digital computing {(135,276 divided by 1,867,024) multiplied by 100} the outcome does not support fractional arithmetic operations with numerical variable when the shell, I think so can put "free" multiplied by 100 and divided by the "total" is ($ a \ * 100 / $ b)

Exercises small shell script basis (b)

Exercises small shell script basis (b)

Because I'm free now changed so that the result is

Exercises small shell script basis (b)

Guess you like

Origin blog.51cto.com/14557584/2455084