Linux Learning Experience: Savor the details in shell scripts and user experience ^_^

Source: Internet
Author: User
Tags mygw

I have been studying Linux for a week and a half. I wrote more than 20 shell scripts under the pressure of Marco Polo... Shell regular expressions, awk statements, program execution streams, test statements, sed statements, functions, and other small commands, such as TR, grep, cut, and WC. More and more think of shell script cute, yes, it is cute! Because of its Quick Start, charming regular expressions, unpredictable program execution streams and awk statements, and powerful functions, it is hard to imagine that such a simple language is perfectly integrated with Linux. It seems like a lovely "wife" of Linux ".

According to Marco, Chinese programmers are not inferior to Indian programmers. Many international programming competitions have won awards for Chinese programmers, but software outsourcing is inferior to Indian programmers. Not the program they compiled is good, but the programmer programming in China is a bit "bean curd residue process-only implement all the functions required to be implemented, and nothing else will happen... It is also a small program. Indians may write hundreds of lines and move forward, while Chinese write only dozens of lines .. It is not for the purpose of "User Experience" that people cannot write so short ".

If you don't talk nonsense, let's get started with the topic.

###########################

Shell script details:

The first is the use of variables: Sometimes we have to enter a path multiple times. In this case, we can define the path as a variable, for example, file =/etc/passwd, use $ file for reference. This technique is useful, but when a script has multiple paths or variables, the variable names can be easily mixed or typed incorrectly. Therefore, in my opinion, the usage principles of variables are as follows:

1. Avoid referencing too many variables as much as possible. Otherwise, the entire script is full of variables, but it is not easy to understand.

2. variables are used to make the entire script more convenient. All parameters that appear multiple times require variables to be referenced.

3. Variable names should be easy to read, easy to remember, and easy to input. Avoid variables that are too similar.

4. Sometimes, variables need to be re-assigned multiple times in a script, but be sure to use them; otherwise, errors may occur.

 

Do all statements need to be simplified?

In the past, I always thought that the Code should be short, the better, so the execution efficiency will be higher. But now there is a problem. If the code is easy to crash, what is the purpose of streamlining? Sometimes, you have to consider program availability based on the completion of program functions. For example, what should I do when a user inputs an error, what should I do when the user suddenly exits, and how should I verify the user input data. For example, if you mistakenly set the IP address and gateway to different CIDR blocks, you cannot access the Internet. For example

Write a script:
1. prompt the user to select the network card to be set;
2. the user is prompted to use DHCP or static as the bootproto of the selected Nic.
A. if you select DHCP, set the value of bootproto in the configuration file to DHCP, and then restart the NIC;
B. if you select static, set the value of bootproto in the configuration file to static, and prompt the user to enter the IP address, subnet mask, and gateway. The Gateway can be empty, however, the IP address or subnet mask cannot be blank. Restart the NIC after the settings are complete;
3. Regardless of the above dynamic or static settings, after the configuration is complete, the information of the nic ip address is displayed to the user again;

 

Let's analyze: the function of this program is actually very simple. We only need to be able to identify whether the user's input information is DHCP or static, and then set it accordingly, but I will consider:

 

Considerations: 1. If you do not want to set any changes, how can you exit?
2. if the user has already set some information, such as the IP address, but does not want to set it, and uses Ctrl + C, how can this problem be solved?

3. What if the user inputs an incorrect IP address?

4. What if the user inputs the incorrect netmask?

5. What if the user inputs an unmatched IP address and gateway?

6. What if the user input already exists?

7. Does the NIC input by the user exist?

8. What if an error occurs during Nic restart?

The Code is as follows:

#! /Bin/bash # create a temporary file and flag = 0 tmpfile = 'mktemp/tmp/ETH. XXXXXXXX '# Read user input read-P "interface:" ethcard # determine whether the user-entered Nic exists allecard = 'ifconfig-A | awk'/^ [^ [: Space:] l]/{print $1} ''until echo $ allecard | grep "$ ethcard" &>/dev/NULL; do echo-e "\ 033 [31 mwrong Card Name. \ 033 [0 m "Read-P" interface: "ethcarddone # Nic PATH variable, in the future, you will need to apply ethfile =/etc/sysconfig/network-scripts/ifcfg-$ ethcard # To determine whether the user-entered protocol is correct read-P "Boot protoco L: "mybootprotountil echo $ mybootproto | grep-e" DHCP | static "&>/dev/NULL; do echo-e" \ 033 [31 mwrong bootproto. \ 033 [0 m "Read-P" Boot Protocol: "mybootprotodone # determine DHCP and staticif [" $ mybootproto "=" DHCP "]; then sed-I "s/^ bootproto =. */bootproto = DHCP/g "$ ethfile if [$? -EQ 0]; then ifdown $ ethcard & IFUP $ ethcard [$? -EQ 0] & Echo "set $ ethcard done. "fielif [" $ mybootproto "=" static "]; then Cat $ ethfile> $ tmpfile read-P" IP Address: "myip # determining IP correctness until [[$ myip = ~ ^ ([1-9] | [1-9] [0-9] | 1 [0-9] {1, 2} | 2 [01] [0-9] | 22 [0-3]) (\. ([0-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-4]) {2 }(\. ([1-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-4]) $]; do echo-e "\ 033 [31 mwrong IP. \ 033 [0 m "Read-P" IP Address: "myip done # determine the correctness of netmask read-P" netmask: "mynm until [[[$ mynm = ~ ^ 255 (\. (0 | 255) {3} $]; do echo-e "\ 033 [31 mwrong netmask. \ 033 [0 m "Read-P" netmask: "mynm done read-P" Gateway: "mygw # determine the IP address and gateway match for I in {1 .. 4}; do [$ ['echo $ myip | cut-D. -F $ I '& 'echo $ mynm | cut-D. -F $ I ']-ne $ ['echo $ mygw | cut-D. -F $ I '& 'echo $ mynm | cut-D. -F $ I '] & flag = 1 & break done until [[$ mygw = ~ ^ ([1-9] | [1-9] [0-9] | 1 [0-9] {1, 2} | 2 [01] [0-9] | 22 [0-3]) (\. ([0-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-4]) {2 }(\. ([1-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-4]) $] & [$ flag-EQ 0]; do echo-e "\ 033 [31 mwrong gateway. \ 033 [0 m "Read-P" Gateway: "mygw for I in {1 .. 4}; do [$ ['echo $ myip | cut-D. -F $ I '& 'echo $ mynm | cut-D. -F $ I ']-ne $ ['echo $ mygw | cut-D. -F $ I '& 'echo $ mynm | cut-D. -F $ I '] & & Flag = 1 & break | flag = 0 done # Whether the information in the file exists, call the temporary file sed-I "s/^ bootproto =. */bootproto = static/g "$ tmpfile grep" ^ ipaddr = "$ tmpfile &>/dev/null & sed-I" s/ipaddr =. */ipaddr = $ myip/"$ tmpfile | echo" ipaddr = $ myip "> $ tmpfile grep" ^ netmask = "$ tmpfile &>/dev/null & SED -I "s/netmask =. */netmask = $ mynm/"$ tmpfile | echo" netmask = $ mynm "> $ tmpfile if [-Z $ mygw]; then sed-I '/^ gateway =. */d' $ t Mpfile else grep "^ gateway =" $ tmpfile &>/dev/null & sed-I "s/gateway =. */gateway = $ mygw/"$ tmpfile | echo" Gateway = $ mygw ">>$ tmpfile fi CP-F $ tmpfile $ ethfile ifdown $ ethcard & IFUP $ ethcard [$? -EQ 0] & Echo "set $ ethcard done." else echo "No such options." Exit 1fi # Delete temporary files to avoid excessive temporary files Rm-F $ tmpfile

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.