Linux Scripting Technology---8

Source: Internet
Author: User
Tags ord

first, what is a script

A script is an executable (777) text file that contains a series of command sequences. When the script file is run, the sequence of commands contained in the file will be automatically executed.

Second, script programming

#!/bin/sh first Row fixed format

#! The parser that indicates the script, which uses/bin/sh to parse

2.1 Variables

Shell scripts allow users to set and use their own variables, which can be numbers or strings, and users do not need to specify their types or define them before they are used.

#!/bin/sh
A= "Hello World"

B=5
echo "A is:A”     //MakeUseChange volume to with

Symbol

echo "B is $b"

2.2 Parameters

As with C programs, command-line arguments can also be used in shell

    1. $#: The number of command-line arguments passed in the script
    2. $*: All command line parameter values, with spaces between individual parameter values
    3. $: Command itself (shell file name)
    4. $: First command-line argument
    5. $: Second command-line argument

2.3 Mathematical calculations

The shell script provides a specialized command expr to calculate mathematical expressions, such as expr 5 +1, but you must also use the inverse number when you want to assign the result of the calculation to other variables (the ~ key on the keyboard is the inverse quotation mark).

var= ' Expr 20/10 '

2.4if statements

The most basic Process Control statement in a shell script is if-then, using the format:

if [condition] (special reminder: the left and right sides of square brackets must have spaces)

Then
Commands
Else
Commands
Fi

2.4.1for statements

The shell script provides a for command to create a loop similar to the for statement in C programming. Use format:

for Var in list
Do
Commands
Done

2.4.2while statements

The shell script provides a while command to create a loop similar to the while statement in C programming. Use format:

while [condition]
Do
Commands
Done

2.5 Comparison of items

Comparison Action integer Action string operation
Same-eq =
Different-ne! =
Greater than-GT >
Less than-lt <
Greater than or equal to-ge
Less than or equal to-le
is empty-Z
Not empty-n

Cases:

    1. Compares integers A and b for equality: if [ a=
  • b] (eq also available)
  • Determines whether the integer a is greater than the integer b:if [a−gt
  • b
  • Compares strings A and b for equality: if [ a=
  • b
  • Determine if string A is empty: if [-Z $a]
  • Determines whether the integer variable A is greater than b:if [a−gt
    1. b

Attention:
1. Leave a space around the "[" and "]" symbols
2. "=" There are spaces around

-e file already exists
-F file is a normal file
-S file size is not zero
-d file is a directory
-R file can be read by the current user
-W file can be written to the current user
The-x file can be performed on the current user
  

2.6sed Editor

Sed (Stream editor): The stream editors are also named line editors, editing only one line at a time, using the following format:

sed [Operation options] ' command ' file name
Common Operation options:
-N: Specifies that only the row is displayed after processing
-N: Specifies that only the row is displayed after processing
-E: Multiple editing tasks
-I: Directly modifies the contents of the read file instead of the screen output

Common Command options:

P Print matching lines
A new
c instead of one line
D Delete Anchor row
s replaces some parts of a row

2.6.1sed-Example

1. Display
sed-n ' 2p ' tmp.txt show only line 2nd
sed-n ' 1,3p ' temp.txt print lines 1th through 3rd
sed-n '/mov/' p temp.txt print lines with movie
2. Delete
sed ' 2d ' tmp.txt Delete line 2nd
sed ' 3,D′TMP.TXTBy deletingExceptThe3YesToMost after a line ,

: Last line
3. Enquiry
Sed-n '/hello/p ' tmp.txt query for all lines that include the keyword Hello

4. Replace
sed ' 1c hi ' tmp.txt line 1th instead of hi
sed-n '/hello/p ' tmp.txt | Sed ' s/hello/bird/g '
5. Insert
sed-i ' $a bye ' tmp.txt insert bye in the last line

2.6.2awk Analyzer

Awk is a powerful text analysis tool that reads rows of files into each line, dividing each line into multiple fields with a space as the default delimiter.

How to use:
awk ' {pattern + action} ' filenames
What pattern to look for
What pattern to look for
The command that the action executes when it finds a match
Example:
  

Linux Scripting Technology---8

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.