Exercises small shell script basis (b)
Enterprise
2019-12-01 21:44:49
views: null
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)](https://s1.51cto.com/images/blog/201912/01/36bce04832a0337e4f0997ce971ba04f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
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)](https://s1.51cto.com/images/blog/201912/01/9b584577e854365f9f13c2c56643bf8c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
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)](https://s1.51cto.com/images/blog/201912/01/c8ae079ef445b7813be8634d39cfb50a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
![Exercises small shell script basis (b)](https://s1.51cto.com/images/blog/201912/01/07e014f843c33bb44d2e4a0328178a09.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
Because I'm free now changed so that the result is
![Exercises small shell script basis (b)](https://s1.51cto.com/images/blog/201912/01/fbef7b40eb0c7d123ec35b22ffb37d68.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
Origin blog.51cto.com/14557584/2455084