Linux Shell Learning notes in the last section, the temperature and knowledge of the new _ other

Source: Internet
Author: User
Tags function definition

Scripting Tips

Script file name naming rules

The characters for the file name include

Letters, Numbers, ".", "_" and "-", note: The script's filename cannot begin with a number.

Name the name of the script file, use English words, phrases, or short abbreviations as much as possible.

Script Version Management:

Script action. Major version number. Number of modifications. Shell type

Variable Naming conventions

Variable names can be made up of numbers and letters

Variable names take all English characters in lowercase

Use abbreviations for variable names, preferably no more than 6 characters

${variable Name} example: ${port},${ipaddr} ...

Scripting Code Annotation Specification

Script code comments can be commented using the "#" and "; (semicolon)" methods. #一般由于描述性的注释, designed to illustrate the role of the code or how to use it.

Often used for example annotations, especially in some configuration files, so we use these two ways to annotate our scripts.

Reference Symbol Usage Specification

Use single quotes as little as possible

Use \ (backslash) to mask special meaning for special characters

When you use quotes to mask characters, other reference symbols are generally not applicable in single quotes unless you print the special symbol itself.

When executing a shell command with an inverted quotation mark, an inverted quotation mark is generally added to a reference symbol.

Scripting function Definition Specification

Variable names can be made up of numbers and letters

Use the Hump nomenclature (starting with the second word, capitalizing each word)

Name as much as possible not to use abbreviations unless it is well known to

A name can consist of two or three words, but usually not more than three

Text Output typesetting

Homework after the first day of class

1, grep "20081011" text.txt out of this day's data

2, grep "200804" Text.txt take out these one months of data

3, grep "200806" Text.txt | Grep-v "-" take out one months of rising data

4, grep "200807" Text.txt | Grep-v "-" | Wc-l listed a few days is the number of rises, with statistics

5, grep "200808" Text.txt | Sort-k5-n | Tail-1 judge up and then tail take the last line

grep "200808" Text.txt | Sort-k5-r |head-1 judged up and then head took the first line

6, grep "200810" Text.txt | awk ' {if ($4>0) {print $1,$5}} ' takes up data in January and increases

7, grep "200811" Text.txt | awk ' {if ($4>5 && $4<20) {print $}} ' judgment interval takes out the entire row

Scripting code Example Analysis 1

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

192.168.1.1,192.168.1.31

The above IP address is written directly in the script. Upon completion of the execution,

Should display IP addresses that can ping and IP addresses that cannot be ping

#!/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

Scripting code Example Analysis 2

Modify Analysis 1, but read IP from iplist.txt

#!/bin/bash

For IP in ' cat iplist.txt '

Todo

If ping $ip-C 1

Then

echo "${ip} online"

Else

echo "${ip} offline"

Fi

Done

Scripting code Example Analysis 3

Modify analysis 2 to remove unwanted information

#!/bin/bash

For IP in ' cat iplist.txt '

Todo

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

Then

echo "${ip} online"

Else

echo "${ip} offline"

Fi

Done

Scripting code Example Analysis 4

Modify Analysis 3 to generate records

#!/bin/bash

>hoststatus.txt

For IP in ' cat iplist.txt '

Todo

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

Scripting code Example Analysis 5

Generate an IP address pool, generate 255 IP, and modify analysis 4 to achieve multi-threaded ping

#!/bin/bash

>iplist

For IP in ' SEQ 1 255 '

Todo

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 '

Todo

Fastping $ip &

Done

Wait

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

RM Temp

Temp ./ping.sh             Test How long a script executes

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.