Fortune Telling Collection - Fortune-telling birth date - Linuxshell programming

Linuxshell programming

LinuxShell programming is a scripting language under Linux system, which allows users to realize some automatic operations by writing scripts, thus improving work efficiency. This paper will introduce the basic knowledge and common operations of LinuxShell programming to help readers quickly master Shell programming skills and create efficient automation tools.

First, the basics of Shell programming

The basic format of 1 Command process

The basic format of a Shell script is as follows:

```

! /bin/bash

This is a note.

Echo "HelloWorld!"

```

The first line specifies the interpreter of the script, generally/bin/bash; The second line is a comment explaining the role of the script; The third line is the execution command, which is used to output "HelloWorld!" .

2. Shell variables

Shell variables are used to store data and can be defined in the following ways:

```

Name= "Tom"

echo$name

```

Where name is the variable name, Tom is the variable value, and $name is the way to call the variable.

3. Shell operators

Shell supports a variety of operators, such as arithmetic operators, relational operators, logical operators and so on. Here are some commonly used operators:

```

arithmetic operator

a= 10

b=20

Echo' expression $ a+$ b' 30

Relational operator

a= 10

b=20

If [$a-eq$b]

then

Echo "A equals B"

other

Echo "A is not equal to B"

The ship does not bear the loading fee.

Logical operator

a= 10

b=20

if[$a-gt0-a$b-gt0]

then

Echo "A and B are both greater than 0"

other

Echo "A and B are not both greater than 0"

The ship does not bear the loading fee.

```

4. Shell Process Control

Shell process control is used to control the execution of scripts. Commonly used process control statements are if-else, for, while, etc. Here is an example:

```

Foreign exchange USD (seq 1 10)

do

Echo $i

finished

```

The above code will output numbers from 1 to 10.

Second, Shell programming in actual combat

1. Implement file backup

File backup is a common operation, which can be realized by Shell script. The following is a simple file backup script:

```

! /bin/bash

backup file

tar-czvfbackup.tar.gz/home/user

```

The above script backs up files in the /home/user directory to backup.tar.gz files.

2. Realize the timing task

Timed task is a very practical function, which can be realized by Shell script. The following is a scheduled backup script:

```

! /bin/bash

Schedule backup files

whiletrue

do

tar-czvfbackup.tar.gz/home/user

Sleep 3600 犆 犆 犆 犆 犆 犆 犆 犆 犆 犆 犆 犆 犆 犆 犆 犆 犆 犆 犆 犆

finished

```

The above script will back up the files in the /home/user directory every hour.

3. Realize system monitoring

System monitoring is a very important task, which can be realized by Shell script. The following is a simple system monitoring script:

```

! /bin/bash

System monitoring

whiletrue

do

Cpu = ` top-b-n 1 | grep " Cpu " | awk ' { print $ 2+$ 4 } ' '

mem=`free-m|awk'/Mem/{print$3} ' '

Echo"CPU utilization: $cpu% "

Echo "Memory usage: $memMB"

Sleep60, what's wrong?

finished

```

The above script will monitor the usage of CPU and memory once every minute.