shell script - Text processing and programming principles

First, the text processing tool

wc command

wc (Word count) statistics for the number of characters in a text file

 

15 is the row number 78 is the number of characters 805 is the file size (bytes) byte is 8 bits [1]

2, parameters: -l (count the number of rows), - w (count the number of words), - c (the number of bytes)

 

cut (sliced) command

1, designated delimiter, cut files

2, the common parameters: -d (designated delimiter), - f (which specifies the extraction column), - output-delimiter = '' (the output of the separator, they want to use to replace delimiter)

Example: cut -d "" -f1 / etc / fstab (taking the first column)

    cut -d "" -f1,3 / etc / fstab (1,3 take the first column)

    cut -d "" -f1-3 / etc / fstab (take the first 1-3)

    cut -d [:] -f1d / etc / fstab (designated delimiter is a colon and a space) ([] represents any character in brackets are matched)

3, cut a plurality of separators can not be specified, awk can specify multiple delimiters

 [Limitations of cut: 1, -d specified delimiter, not both a plurality of 2, the output format can not be advanced; so to grasp awk Syntax

sort command

1, sort order to sort according to the ASCII code, not the numerical size

2, commonly used parameters: -r (reverse), - f (ignore case characters)

         -t (specified field delimiter), - n (in descending order value)

         -u (deduplication sorting, repeated not shown)

       -k (specify which column to sort)

uniq command

1, uniq command is used to weight, but only continuous and repeated the same order to be considered a sort [it must first de-emphasis]

2, commonly used parameters: -c (Statistical some characters repeat of words) [Example: sort filename | uniq -c]

                -d (display not only been repeated rows)

Exercise: Remove the / etc / passwd rows 6-10 of the file, and information is sorted according to numerical values ​​of three fields, and finally only the display of each of the first field

           Head /etc/passwd | tail -5 | sort -t: -k3 -n |cut -d” “ -f1

 Second, Principles of Programming

1, the mechanical limitations of the code (hexadecimal) of: identifying the machine, is not compatible with a variety of brands

2, the machine can identify binary instructions

3, the program instructions = data +

4, the drive can not use the default hardware device; the CPU controls the hardware resources, the CPU of the arithmetic controller composed

5, driver: when an instruction is required to communicate between the different manufacturers of hardware devices, drivers we need to "translate"

6, closer to hardware development engineers to learn the "assembly language", but is limited by the factory assembly language

7, C and C ++ language for the underlying assembly language

8, the operating system used to allocate hardware resources (operating system written in C language)

9, now based on high-level language programming and ultra-high-level language, better enable programmers to implement programming. Ultra-high-level language and high-level language to be translated into computer-readable language (binary language), translated into two categories: Interpreted (progressive translation, line by line, for example: bash -x filename (easy troubleshooting)) and compiled type (once compiled, all execution)

10, Shell, Python is an interpreted language, compiled and installed mostly in C language, C ++, C #

11, classification of programming languages: the center of gravity and services according to different programming into (focus on language data) and the process-oriented programming object-oriented programming (focus on language instruction).

12, implementation programming language [line by line]: execute (no logical relationship) order loop is executed (for (traverse, commonly used), the While (loop condition, True when the cycle begins), Unti (and while the opposite, False of beginning cycle)), select the execution (branch if statements, case (emphasis use))

13, Shell programming description: shell statements are process oriented, focusing on the instruction set;

          The basic structure of shell statements: the command data - variable logic

          no shell built-in functions, there is no function library calls

            Did not call interface, which limits the functionality of a shell script, unlike Python, you can invoke various module interface

            Advantages: shell function better compatible system, you can directly call system commands, execute more efficiently

14, the first sentence must write a shell script (#! / Bin / bash) ----> custom script interpreter

15, / etc / shells ---------- view the current program supported by the system shell 

      echo $ SHELL ----- see the shell program for the current session  

      / Etc / passwd ---------- developed to support the user's default shell program (/ bin / nologin) 

16, implementation of the shell: 1, bash command to execute the script

              Parameters: -n view the logical error of shell scripts

                                  -x Progressive script execution (easy troubleshooting)

                            2, authorized chmod + x filename, and then execute the script to use the full path

17: Environment variables

        Local variables (DECLARE command - Define variable type)

        Local variables (local in functions)

18, variable types: numeric (floating point (a float), integer (int), Boolean (bollean, 0,1 and false, true))

                :( string of ordinary characters and strings, arrays)

19, classification of programming languages: The different variables, are divided into strongly typed languages ​​(the value must be defined before processing or computation), and a weakly typed language (language programming can automatically identify the type of variable)

20, polymorphism: a plurality of data attributes, the properties depend on the final use of the data and its operation

21, parameter passing: $ (execution state of a command, 0 is correct, 1-255 indicates an error)?

        $ 1, $ 2, $ {10} -------- incoming data command script later, a space for the separator

      $ # (Statistical parameter transfer amount, $ # {$} variable transmission statistics)

      $ * (Represented by all transmission parameters, all of the transmission output parameter as a string)

        $ @ (Indicate all mass participation, mass participation of all output in a list)

                 

22, define the variable format: NAME = VALUE (without spaces, an equal sign is the assignment, determining two equal sign)

         Declare variables defined by command parameters (Example: declare -ia = 10 int () str ())

          declare -a (on behalf of the array), declare -i (on behalf of integer)

23, variable naming: underlined specify the variable name

          Hump ​​naming

24, file test, conditional: in a scripting language, we need to branch statement, is to do judgments, judgment is to use the test command to achieve.

Using the format: test [option] file

                   [ Conditional statements ]

[Do not test to determine without []]

Common test options:

Comparison options: -eq (equal), - en (not equal), - gt (greater than), - ge (greater than or equal), - lt (less than), - le (or less) [for determining values, characters =,! =]

Analyzing options: -f (determining whether a regular file), - d (determined whether the directory member), - L (determines whether the linked file), - r, -w, -x permissions (read determines whether execution ))

Associated option: -o (or), - a (and), (non)!

Test String: = (equal to true), = (not equal to false), - z (string is determined whether or not there exists true, false not present), - n (with opposite -z)!

25, the logic operation: and &&: command sides are true only when the result is true

          Or ||: command on both sides, one side is true is true, and false is false

        Non-!

        [ $? -eq 0 ]  && exit 0 || exit 1

Before and after docking is a separate command exists, the command before and after the two nothing but linked by logical operators

26, arithmetic: let (1 + 1) Example: let a = 1 + 1 echo $ a 

               expr (1 ​​* 1) Example: exor 1 * 10

          $ [] [$ 1 + 10] Example: a = $ [2 + 3] echo $ a   

       $ (()) (($ 1 / $ 2)) Example: a = $ ((2/4)) echo $ a

 27, programming exercises

Within the book and an output 100

1
2
3
4
5
6
7
8
#!/bin/bash
 
declare  -i i=0
declare  -i  sum =0
while  [ $i - le  100 ] ; do
         sum +=$i
         let  i=i+1
done

Results of the:

 

Parameter passing

1
2
3
4
5
6
7
#!/bin/bash
echo  "$1 $2 ...${10}"
echo  $?
echo  $ #
echo  $*
echo  $@
echo  ${$ #} 

Results of the:

 

Determine whether a file exists

1
2
3
4
5
6
7
8
#!/bin/bash
#判断文件是否存在
 
if  [ -f $1 -a -w $1 ]; then
         echo  "$1 文件不存在"
else
         echo  "$1 文件已存在"
fi

 

Analyzing user type

 

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
#判断用户类型
 
declare  -i uid=`uid -u $1`
 
if  [ $uid - eq  0 ]; then
         echo  "这个是管理员"
else
         echo  "这是个普通用户"
fi

 

Results of the:

Guess you like

Origin www.cnblogs.com/Eucalyptusleaves/p/11422431.html
Recommended