Shell script – text processing and programming principle

1. Text processing tools

wc command

wc (Word count) is used to count the number of characters in a text file

< img alt="Share a picture" src="/wp-content/uploads/images/os/unix/1626797359153.png" >

15 is the number of lines 78 is the number of characters 805 is the file size (words Section) [1 byte is equal to 8 bits]

2, parameters: -l (statistics number of lines), -w (statistics number of words), -c (statistics number of bytes)

< p>

cut (slicing) command

1, specify the separator, cut the file

2, common parameters: -d (specify the separator),- f (specify which column to extract), –output-delimiter=” (replace the output delimiter with the delimiter you want to use)

Example: cut -d” -f1 /etc /fstab (take the first column)

cut -d” ”-f1, 3 /etc/fstab (take the first and third columns)

cut -d”” -f1 -3 /etc/fstab (take the 1st to 3rd columns)

cut -d[:] -f1d /etc/fstab (specify the delimiter as a space and a colon) ([] represents any of the brackets All characters match)

3, cut cannot specify multiple separators, awk can specify multiple separators

[Limitations of cut: 1, -d specifies separators, cannot Specify multiple at the same time 2. Advanced formatted output is not allowed; so you must master the awk command]

sort command

1. The sort command is sorted according to ASCII code, not numeric Size

share picture

2. Common parameters: -r (reverse order), -f (ignore the case of characters)

      -t (specify the field separator), -n (sort by numerical value)

p>

      -u (sort to remove duplicates, duplicates are not displayed)

       -k (specify which column to sort)

uniq command

1 , Uniq command is used for deduplication, but only continuous and identical The command is considered to be repeated [so sort it before de-duplication first]

2. Common parameters: -c (count the number of repeated characters for certain characters) [Example: sort filename | uniq -c]< /p>

   -d (display only the lines that have not been repeated)

Exercise: Take out the 6th to 10th lines of the /etc/passwd file, and set the information according to the value of the third field Sort by size, and finally display only the first field of each

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

Second, programming principle

1. The limitation of mechanical code (hexadecimal): recognize the machine, various brands are not compatible

2, the machine can only recognize binary instructions

3. Program=instruction + data

4. Drive hardware devices cannot be used by default; CPU controls hardware resources, and CPU is composed of controllers and arithmetic units

5. Drivers: When instructions are required to be communicated between hardware devices of different manufacturers, we need drivers for “translation”

6. Engineers who are closer to hardware development should learn “assembly language” , But assembly language is restricted by manufacturers

7. The bottom layer of C language and C++ is assembly language

8. Operating system is used to allocate hardware resources (operating system is written in C language )

9. The current programming is based on high-level languages ​​and super high-level languages, which better allows programmers to realize programming functions. High-level languages ​​and ultra-high-level languages ​​need to be translated into computer-readable languages ​​(binary languages). Translation is divided into two types: interpreted (translate line by line, execute line by line, for example: bash -x filename (for debugging)) and compilation Type (compile once, execute all)

10. Shell and Python are interpreted languages. Most of the compilation and installation use C language, C++, C#

11. Classification of programming languages: According to The focus of services is different, and programming is divided into object-oriented programming (a language that focuses on data) and process-oriented programming (a language that focuses on instructions).

12. Programming language execution mode [line by line]: sequential execution (no logical relationship), loop execution (for (traversal, commonly used), While (loop condition, loop when True), unti (In contrast to while, it starts to loop when it is False)), choose to execute (branch if statement, case (emphasis used))

13. Shell programming introduction: shell statement is process-oriented, focusing on the instruction set ;

          The basic structure of shell statements: command, data-variable, logical relationship

                                                           Interface, this limits the function of shell scripts. Unlike Python, you can call various module interfaces.

         Advantages: Shell has better compatibility with the system, and can directly call system commands, which makes the execution more efficient.< /p>

14. The first sentence of the shell script must be written (#!/bin/bash)—->Define the script interpreter

15, /etc/shells— ——-Check the shell programs supported by the current system

echo $SHELL—–Check the shell programs of the current session

/etc/passwd—- ——Developed the shell program (/bin/nologin) supported by the user by default

16. Shell execution: 1. bash command to execute the script

          Parameters:- n View the logic errors of the shell script

/p>

17. Variables: environment variables

     Local variables (declare command–define variable types)

     Local variables (local used in functions)

18. Variable type: numeric type (float type (float), integer type (int), boolean value (bollean, 0, 1 and false, true))

     String: (common Characters and characters Strings, arrays)

19. Classification of programming languages: According to different variables, they are divided into strongly typed languages ​​(values ​​must be defined before they can be processed or calculated) and weakly typed languages ​​(programming languages ​​can be automatically identified Variable type)

20. Polymorphism: A data has multiple attributes, and the last attribute used depends on the data it is operated on.

21. Passing parameters: $? ( The execution status of the previous command, 0 means correct, 1-255 means wrong)

      $1, $2, ${10}——–After the command, the data of the script is passed in, with spaces Is the separator

      $# (statistical parameter transfer, ${$#}statistical parameter transfer)

      $* (represents all parameters, all parameters are in the form of strings Output)

     [email protected] (means that all parameters are passed, and all parameters are output as a list)

Share a picture

22. Define variable format: NAME=VALUE (no spaces in between, an equal sign is Assignment, the two equal signs are judgment)

         Use the declare command to define variable parameters (for example: declare -ia=10 int() str())

                  ), declare -i (representing an integer)

23. Variable naming: specify the variable name by underscore

       camel case naming method

24. File test, condition Judgment: In a scripting language, we need to branch statements, that is, to make judgments, which are realized by using the test command.

Using format: test [option] file

[condition statement]

[Do not add []] when you don’t need test to make judgments

Common test options:

Comparison options: -eq (equal to), -en (not equal to), -gt (greater than), -ge (greater than or equal to), -lt (less than), -le (Less than or equal to) [Used for judging numerical values, use =, != for characters]

Judging options: -f (judge whether it is a normal file), -d (judge whether it is a directory file), -L (Judging whether it is a linked file), -r, -w, -x (determining whether it has the permission to read and write execution))

Associated options: -o (or), -a (and),! (Non)

String test: = (equal to true), != (not equal to false), -z (judge whether the string exists, if it exists, it is true, if it does not exist, it is false),- n (opposite to -z)

25. Logic operation: AND &&: the result is true when both commands are true

       or ||: the commands on both sides, one side is True is true, and false at the same time is false

        non!

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

Docking before and after They are all separate commands. The two commands before and after have nothing to do with each other. They are only connected by logical operators.

26. Arithmetic operation: let (1+1) Example: let a=1+1 echo $a 

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

      $[]   $[]   $[]  2[$1+10]  3  $3

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

27. Programming exercises

< p>Output the sum of books within 100

1
2
3
4
5
6 < /div>

7
8
#!/bin/bash
declare -ii=0
declare -i sum =0
while [$i - le 100] ; do code>
sum +=$i
let< /code> i=i+1
d one

Execution result:

Share a picture

Passing parameters

< div class="Highlighter sh-gutter">

1
2
3
4
5
6

7
< code class=" bash preprocessor bold">#!/bin/bash
echo "$1 $2 ...${10}"
echo $?
echo $ #
echo $*
echo [email protected]
echo ${$ #} 

Execution result:

Share picture

Judging whether the file exists

1
2
3
4
5
6
7
8
#!/bin/bash
#Judging whether the file exists
if [ -f $1 -a -w $1 ]; then
echo "$1 file does not exist"
else
echo "$1 file already exists"

< div class="line number8 index7 alt1"> fi

share picture

Determine the user type

1

< div class="line number2 index1 alt1"> 2

3
4
5
6
7
8
9
10
#!/bin/bash
#Judging the user type
declare -i uid=`uid -u $1`

< div class="line number6 index5 alt1"> if [ $uid - eq 0 ]; then

echo "This is an administrator"
< code class="bash keyword">else
echo "This is a normal user"
fi< /code>

Execution result:

share picture

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

1
2
3
4
5

< div class="line number6 index5 alt1"> 6

7
8
#!/bin/bash

declare -ii=0< /code>
declare -i sum =0
while [ $i - le 100] ; code> do
sum +=$i
/code > let i=i+1
done

1

2

3

4

5

6

7

8

#!/bin/bash
declare -ii=0 < /div>

declare -i sum =0
while [ $i - le 100 ] ; do
sum +=$i
let i=i+1
done

#!/bin/bash< /p>

declare -ii=0

declare -i sum =0

while [ $i - le 100] ; do

sum +=$i< /code>

let i=i+ 1

done

1
2
3
4
5
6
7
#!/bin/bash
echo< /code> "$1 $2 ...${10}"
echo $?
echo $ #
echo $*
echo [email protected]
echo ${$ #} 

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

< div class="line number6 index5 alt1"> echo [emailprotected]

echo ${$ #} 

1

2

3< /p>

4

5

6

7

< code class=" bash preprocessor bold">#!/bin/bash
echo "$1 $2 ...${10}"
echo $?
echo $ #
echo $*
echo [email protected]
echo ${$ #} 

#!/bin/bash

echo "$1 $2 ...${10}"

echo< /code> $?

echo $< /code> #

echo $*< /code>

echo [email protected]

echo ${$ #} 

1
2
3
4

5

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

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

1

2

3

4

5

6

7

8

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

#!/bin/bash

#判断文件是否存在

 

if  [ -f $1 -a -w $1 ]; then

         echo  "$1 文件不存在"

else

         echo  "$1 文件已存在"

fi

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

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

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

#!/bin/bash

#判断用户类型

 

declare  -i uid=`uid -u $1`

 

if< /code>  [ $uid - eq  0 ]; then

         echo  "这个是管理员"

else

         echo  "这是个普通用户"

fi

Leave a Comment

Your email address will not be published.