The last section of linux Shell learning notes:

Source: Internet
Author: User

Script Writing Skills

Script File Name naming rules

The characters in the file name include

Letters, numbers, ".", "_", and "-". Note: The script file name cannot start with a number.

Name the script file name. Try to use the abbreviations of English words, phrases, or short sentences.

Script version management:

Script Function. Main version number. Number of modifications. shell Type

Variable naming rules

Variable names can consist of numbers and letters

The variable name must be in lowercase letters.

Use abbreviations for variable names, preferably at least 6 Characters

$ {Variable name} example: $ {port}, $ {ipaddr }...

Script code annotation Specification

You can use the "#" and "; (semicolon)" methods to annotate script code. # Generally, descriptive comments are used to explain the role or usage of the Code.

However, annotations are usually used for demonstration purposes, especially in some configuration files. Therefore, we use these two methods to annotate our scripts.

Reference symbol usage Specification

Use as few single quotes as possible

Use \ (backslash) to block special characters with special meanings.

When characters with quotation marks are used for shielding, other referenced symbols are generally not used in single quotation marks unless special symbols are printed.

When a shell command is executed using reverse quotation marks, other reference symbols are generally added to the reverse quotation marks.

Script Function Definition Specification

Variable names can consist of numbers and letters

Use the camper name method (starting from the second word, the first letter of each word is capitalized)

Do not use abbreviations unless they are well known.

A name may consist of two or three words, but generally should not contain more than three words.

Text output Layout

Homework after class on the first day

1. grep "20081011" text.txt retrieves the data of the day

2. grep "200804" text.txt retrieves data for this month

3. grep "200806" text.txt | grep-v "-" retrieves the monthly increase data

4. grep "200807" text.txt | grep-v "-" | wc-l indicates the number of increases in a few days, with statistics

5. grep "200808" text.txt | sort-k5-n | tail-1 determines the rise, and then tail takes the last row.

Grep "200808" text.txt | sort-k5-r | head-1 determines the rise, and then head takes the first line

6. grep "200810" text.txt | awk '{if ($4> 0) {print $1, $5}' to retrieve the rising data time and increase in January

7. grep "200811" text.txt | awk '{if ($4> 5 & $4 <20) {print $0}' judge the interval to retrieve the entire row

Script code instance analysis 1

Write a shell script and ping the following address automatically after executing the script:

192.168.1.1, 192.168.1.31

The above IP addresses are directly written in the script. After the execution is complete,

The IP addresses that can be pinged and those that cannot be pinged should be displayed.

#! /Bin/bash

If ping 192.168.1.1-c 1

Then

Echo "192.168.1.1 online"

Else

Echo "192.168.1.1 offline"

Fi

If ping 192.168.1.31-c 1

Then

Echo "192.168.1.31 online"

Else

Echo "192.168.1.31 offline"

Fi

Script code instance analysis 2

Modify Analysis 1, but read the IP address from iplist.txt

#! /Bin/bash

For ip in 'cat iplist.txt'

Do

If ping $ ip-c 1

Then

Echo "$ {ip} online"

Else

Echo "$ {ip} offline"

Fi

Done

Script code instance analysis 3

Modify Analysis 2 to Remove useless information

#! /Bin/bash

For ip in 'cat iplist.txt'

Do

If ping $ ip-c 1>/dev/null 2> & 1

Then

Echo "$ {ip} online"

Else

Echo "$ {ip} offline"

Fi

Done

Script code example 4

Modify Analysis 3 and generate records

#! /Bin/bash

> Hoststatus.txt

For ip in 'cat iplist.txt'

Do

If ping $ ip-c 1>/dev/null 2> & 1

Then

Echo "$ {ip} online" | tee-a hoststatus.txt

Else

Echo "$ {ip} offline" | tee-a hoststatus.txt

Fi

Done

Script code instance analysis 5

Generate an IP address pool, generate 255 IP addresses, and modify analysis 4 to implement multi-thread ping

#! /Bin/bash

> Iplist

For ip in 'seq 1 255'

Do

Echo "192.168.1. $ {ip}"> iplist

Done

#! /Bin/bash

> Hoststatus.txt

> Temp

Fastping ()

{

If ping $ {1}-c 1>/dev/null 2> & 1

Then

Echo "$ {ip} online" | tee-a temp

Else

Echo "$ {ip} offline" | tee-a temp

Fi

}

For ip in 'cat iplist.txt'

Do

Fastping $ ip &

Done

Wait

Sort-t.-k4-n temp> hoststatus.txt

Rm temp

How long does temp./ping. sh test the execution of a script?

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.