Shell script Application (i) case

Source: Internet
Author: User


Case One

• Write a login welcome script for ROOT to report the number of processes in the current host, the number of logged-in users, and the disk usage of the root file system.



1. Using the VIM command in the /root directory is a new script file welcome.sh, which reads as follows:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/8A/72/wKioL1gxJe6Ck1hoAAAJB8S-f5E578.png "style=" float: none; "title=" 2016-11-20_115833.png "alt=" Wkiol1gxje6ck1hoaaajb8s-f5e578.png "/>

Note: ' {print $} ' is used here as a single quote, not a reverse apostrophe.


2. Add the Execute (x) permission to the welcome.sh file using the chmod command

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8A/76/wKiom1gxJe-i21lLAAAB9s0zKd0077.png "style=" float: none; "title=" 2016-11-20_115926.png "alt=" Wkiom1gxje-i21llaaab9s0zkd0077.png "/>


3. Modify the/root/.bash_profile file to invoke the welcome.sh script every time the root user logs on to the system

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8A/72/wKioL1gxJe-AKcyyAAACMrb6gA0996.png "style=" float: none; "title=" 2016-11-20_115959.png "alt=" Wkiol1gxje-akcyyaaacmrb6ga0996.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/8A/76/wKiom1gxJe_w0UN5AAABUbRkOCE670.png "style=" float: none; "title=" 2016-11-20_120215.png "alt=" Wkiom1gxje_w0un5aaabubrkoce670.png "/>


4. Verify: Log back in with the root user to verify the output information of the Welcome script

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/8A/72/wKioL1gxJe_zZK6mAAAHdXMLlPo663.png "style=" float: none; "title=" 2016-11-20_121116.png "alt=" Wkiol1gxje_zzk6maaahdxmllpo663.png "/>



Case Two

• Write start, stop, status three script programs to start and stop various system services.


1. use the VIM command to Create a new script file in the/bin directory start.sh

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/8A/76/wKiom1gxJe-iKAAJAAACzy6WpG8996.png "style=" float: none; "title=" 2016-11-20_121404.png "alt=" Wkiom1gxje-ikaajaaaczy6wpg8996.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/8A/76/wKiom1gxJfCgjTSnAAACMqtVClQ357.png "style=" float: none; "title=" 2016-11-20_121447.png "alt=" Wkiom1gxjfcgjtsnaaacmqtvclq357.png "/>


2. Set x permissions

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/8A/72/wKioL1gxJfCSb4oTAAACPXDz2xI473.png "style=" float: none; "title=" 2016-11-20_121530.png "alt=" Wkiol1gxjfcsb4otaaacpxdz2xi473.png "/>


3. Modify aliases

Alias start= ' start.sh '//To enter start direct execution need to create a new alias (this alias is temporary, the system restarts after the failure, if the permanent effect, also need to modify the/ROOT/.BASHRC configuration file, add this line command)

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/8A/76/wKiom1gxJfDTYS2pAAACKvjpM5Q692.png "style=" float: none; "title=" 2016-11-20_121640.png "alt=" Wkiom1gxjfdtys2paaackvjpm5q692.png "/>


4. Test scripts

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/8A/72/wKioL1gxJfGBtPetAAANI5hRJbo992.png "style=" float: none; "title=" 2016-11-20_121830.png "alt=" Wkiol1gxjfgbtpetaaani5hrjbo992.png "/>


The script file stop and status are similar to start and are no longer described here.



Case Three

• Filter out the MAC address of the ETH0 network card and assign a value to the variable hwaddr.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/8A/72/wKioL1gxMRixZA1GAAAGXN43PUA955.png "style=" float: none; "title=" 2016-11-20_125228.png "alt=" Wkiol1gxmrixza1gaaagxn43pua955.png "/>



Case Four

• Write a small script sumsquare.sh to calculate the sum of squares of two integers.


1. Create a script

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8A/72/wKioL1gxMRiRuFUdAAAB-8hgWMc923.png "style=" float: none; "title=" 2016-11-20_125324.png "alt=" Wkiol1gxmrirufudaaab-8hgwmc923.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/8A/76/wKiom1gxMRjSA-0hAAAFiHH6OCA741.png "style=" float: none; "title=" 2016-11-20_125931.png "alt=" Wkiom1gxmrjsa-0haaafihh6oca741.png "/>


2. Assigning Script Execution permissions

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/8A/76/wKiom1gxMRniSnm6AAACH3ZKo98609.png "style=" float: none; "title=" 2016-11-20_130003.png "alt=" Wkiom1gxmrnisnm6aaach3zko98609.png "/>


3. Testing

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8A/72/wKioL1gxMRnBRFvAAAAEIkmaLeQ830.png "style=" float: none; "title=" 2016-11-20_130250.png "alt=" Wkiol1gxmrnbrfvaaaaeikmaleq830.png "/>



Case Five

• Write a small script memusage.sh to settle the memory usage based on the results of the free command.


1. Create a script

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/8A/76/wKiom1gxMRnAnC6CAAAB_Nxb0nI754.png "style=" float: none; "title=" 2016-11-20_130402.png "alt=" Wkiom1gxmrnanc6caaab_nxb0ni754.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/8A/72/wKioL1gxMRmQynKfAAAI-VCef0k778.png "style=" float: none; "title=" 2016-11-20_130927.png "alt=" Wkiol1gxmrmqynkfaaai-vcef0k778.png "/>

Note: In a Linux system, the actual memory usage is recommended to calculate the ratio of total memory space, whichever is the used Buffers/cache. Also, because the bash shell does not support decimal operations, and the usage percentage is less than 1, you need to multiply the numerator by 100 before calculating the result.


2. Give script x permission

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/8A/73/wKioL1gxMRrDYvPaAAACGLgmZ8E296.png "style=" float: none; "title=" 2016-11-20_130959.png "alt=" Wkiol1gxmrrdyvpaaaacglgmz8e296.png "/>


3. Test scripts

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/8A/76/wKiom1gxMRqh8iWFAAACW3nDgDg431.png "style=" float: none; "title=" 2016-11-20_131033.png "alt=" Wkiom1gxmrqh8iwfaaacw3ndgdg431.png "/>



This article is from the "Network Technology" blog, please be sure to keep this source http://fjc365.blog.51cto.com/11891287/1874752

Shell script Application (i) case

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.