Linux note 3-11 3-12 shell script

Source: Internet
Author: User

# # 1. Create a new shell script  ##

1. Create a text file that contains the bash command. The first line of the file should be:

#!/bin/bash

2. Make the file executable (using chmod +x scripts)

3. script debug mode :

#!/bin/bash-x

# Bash-x Scripts

# # 2. References and escapes # #

Weak references

Place the string in double quotes, preserving the literal value of all characters in the string, except for the $, ', \, and! characters. In other words, variable extensions and command extensions still work within double quotation marks.

Cases:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8B/9D/wKiom1hSiISCOKSAAACCA21NuPk806.png "style=" float: none; "title=" Image 1.png "alt=" Wkiom1hsiiscoksaaacca21nupk806.png "/>


Strong references place the string in single quotation marks, preserving the literal value of all characters in the string, and disabling all extensions:

Cases:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/8B/9A/wKioL1hSiIXzUtqnAACDAlVQn3A291.png " title= "Picture 2.png" style= "Float:none;" alt= "Wkiol1hsiixzutqnaacdalvqn3a291.png"/>

Escape

the non-referenced \ is an escape character. It retains the literal value of the next character. (for example, \ $PATH is the exact string $path, not the contents of the PATH variable.) )

Cases:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/8B/9D/wKiom1hSiIbju6R4AABsAITLvug946.png " title= "Picture 3.png" style= "Float:none;" alt= "Wkiom1hsiibju6r4aabsaitlvug946.png"/>

# 3. Numerical Operations # #

1.$[]

Cases:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/8B/9D/wKiom1hSiR_Aae_vAAAomg4Ip7w272.png "style=" float: none; "title=" Image 4.png "alt=" Wkiom1hsir_aae_vaaaomg4ip7w272.png "/>

2.Expr

Cases:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/8B/9A/wKioL1hSiSCyUtDWAAAmVHIYLQk348.png " title= "Picture 5.png" style= "Float:none;" alt= "Wkiol1hsiscyutdwaaamvhiylqk348.png"/>

3.(())

Cases:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/8B/9D/wKiom1hSiSHwRvC0AABKZUG3ook977.png " title= "Picture 6.png" style= "Float:none;" alt= "Wkiom1hsishwrvc0aabkzug3ook977.png"/>

4. Let

Cases:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/8B/9A/wKioL1hSiSHwGMBJAABKCN996z0904.png " title= "Picture 7.png" style= "Float:none;" alt= "Wkiol1hsishwgmbjaabkcn996z0904.png"/>

# 4. Loop for do doing # #

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/8B/9D/wKiom1hSiTmScMXJAABKtPmhxdk274.png " title= "Picture 8.png" alt= "Wkiom1hsitmscmxjaabktpmhxdk274.png"/>

# 5. Positional Parameters # #

Total Parameters:$#

position parameters themselves: $, $, $, $....

all positional parameters : [email protected], $*

Cases:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/8B/9E/wKiom1hSiVHCdEVhAABhDoLrCN8507.png "title=" Picture 9.png "alt=" Wkiom1hsivhcdevhaabhdolrcn8507.png "/>

# #6. Test condition to determine # #

syntax:test EXPRESSION or [EXPRESSION]

non-0 or 0-length string operator:Test-{n|z} string

Returns 0 if the expression is true, returns a non-0 exit state if the expression is False

# # character comparison operator # #

= , !=

Cases:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/8B/9A/wKioL1hSiV_w958QAAAjiuagn30768.png "title=" Picture 10.png "alt=" Wkiol1hsiv_w958qaaajiuagn30768.png "/>

# # number comparison operator # #

-eq equals

-ne Not equal to

-lt less than

-le less than or equal to

-GT greater than

-ge greater than or equal to

Cases:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/8B/9A/wKioL1hSiWjRFBfyAABC0sX1kwk933.png "title=" Picture 11.png "alt=" Wkiol1hsiwjrfbfyaabc0sx1kwk933.png "/>

# # File status Operator # #

syntax:test-{b|c|e|f|d|r|w|x|s| L} file/directory,

or [-{b|c|e|f|d|r|w|x|s| L} File/directory]

- b block File

- C character File

- e file exists

- F Normal file

- D Catalogue

- R file readable

- L Soft Links

Cases:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/8B/9E/wKiom1hSiXPy-yi4AAB02tiM7GA296.png "title=" Picture 12.png "alt=" Wkiom1hsixpy-yi4aab02tim7ga296.png "/>

# # logical Operator # #

- o or

-A and

! Non-

&& for true time execution

|| for fake time execution

Cases:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8B/9A/wKioL1hSiXyjIU2FAAA4CeZ4VFE790.png "title=" Picture 13.png "alt=" Wkiol1hsixyjiu2faaa4cez4vfe790.png "/>

# # 7.if Statement # #

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8B/9E/wKiom1hSiYXSjrVsAAB4menPSIo448.png "title=" Picture 14.png "alt=" Wkiom1hsiyxsjrvsaab4menpsio448.png "/>

# # 8.expect Statement # #

#!/usr/bin/expect

This line tells the code in the operating system script to use that shell to execute.

Set Timeout 10

Sets the time-out of the wait response for All subsequent expect commands in seconds.

Spawn talk

Pawn is an internal command of expect, which adds a shell to the shell command behind it, which is used to pass interactive instructions.

Expect "who"

determine if the last output contains a "who" string, and if so, return immediately, otherwise wait for the time-out to return.

Send "westos\n"

performing an interactive action is equivalent to manually entering "Westos".

Expect EOF

The function is to search for the file terminator in the output , and if there is no line, the script exits immediately without getting the correct result.

Interact

After the completion of the implementation of the interactive State, the control to the console, this time can be manually operated. Otherwise, log out.

$ARGV parameter array

Xpect scripts can accept parameters that are passed from bash. can be obtained using [lindex $argv n], n starting from 0, respectively

one , the second, the third .... Parameters.

Scripting Exercises:

scan the current LAN host, if you can ping through. Just log in and create the UserFile.

user and corresponds to the password in the Passwdfile. If the user exists, do not change their password.

Scan script:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/8B/9A/wKioL1hSiZCiZdeHAAB8w2X60bk479.png "title=" Picture 15.png "alt=" Wkiol1hsizcizdehaab8w2x60bk479.png "/>

SCP Script:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/8B/9A/wKioL1hSiZ7zkTIOAAGknC7IigM919.png "title=" Picture 16.png "alt=" Wkiol1hsiz7zktioaagknc7iigm919.png "/>

SSH script:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/8B/9E/wKiom1hSibnA3l6sAACj_Lk7jwY135.png "style=" float: none; "title=" Image 17.png "alt=" Wkiom1hsibna3l6saacj_lk7jwy135.png "/>

To create a user script:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/8B/9A/wKioL1hSibvjXfs_AAHPZhSQEQs683.png "title=" Picture 18.png "style=" Float:none; "alt=" Wkiol1hsibvjxfs_aahpzhsqeqs683.png "/>

Test results:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/8B/9E/wKiom1hSibuyr7usAACa0UVa034614.png "title=" Picture 19.png "style=" Float:none; "alt=" Wkiom1hsibuyr7usaaca0uva034614.png "/>

Linux note 3-11 3-12 shell 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.