Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A preliminary View of Shell script-- Theory (1)

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

A preliminary view of Shell script-- the theory part (1) Shell script concept:

Save the commands to be executed in a file in order

As long as the file is given execute permission, it can be run (using the chmod command)

Various shell control statements can be combined to complete complex operations.

Shell script application scenarios:

Repetitive operation

Batch processing

Automatic operation and maintenance

Service status monitoring

Scheduled task execution

The role of Shell script:

Act as a command interpreter

Between the system kernel and the user, responsible for interpreting the command line

Write basic script code

1. Use the vim text editor

2. One Linux command per line, written in the order of execution.

Example of operation:

[root@localhost ~] # vim first.sh / / create a blank shell script cd / boot/pwd / / display path ls-lh vml* / / display all files at the beginning of vml to give executable permissions to the script

Make scripts executable-use the chmod command

Example of operation:

[root@localhost ~] # chmod + x first.sh& [root@localhost ~] # chmod 755 first.sh execution script file method 1:

". / script file path"

Note: this method does not change its location when executing the script file.

[root@localhost ~] #. / first.sh / / must have the permission to execute method 2:

Sh script File path

Note: this method does not change its location when executing the script file.

[root@localhost ~] # sh first.sh / / method 3 can be executed without execution permission:

Source script File path

Note: this method will change its position according to the content of the script when it is executed.

[root@localhost ~] # source first.sh / / method 4 can be executed without execution permission:

". Script file path"

Note: this method will change its position according to the content of the script when it is executed.

[root@localhost] #. First.sh / / script can be executed without permission.

1. Script declaration (declare specific running environment-must have)

2. Comment information (explain the content of the script, but not add it)

3. Executable statement

Variable-- the most important role of Shell script

Provide specific parameters for flexible management of Linux systems

1. Variable name: use a fixed name, with system presets or user-defined

2. Variable value: it can be changed according to user settings and system environment.

Types

Variables are mainly divided into four types:

Custom variables: defined, modified and used by users

Environment variables: maintained by the system and used to set up the work environment

Position variables: passing parameters to the script from the command line

Predefined variables: a class of variables built into Bash that cannot be modified directly

Custom variable

​ variable names begin with letters or underscores, are case-sensitive, and are recommended to be all uppercase.

Variable name = variable value

​ looks at the value of a variable

Echo $variable name

You can use quotation marks when assigning values to custom variables:

Double quotation marks: allows you to reference other variables by using the $symbol

Single quotation marks: treat $as a common symbol and prohibit references to other variables

Reverse apostrophe (`): replace the command to extract the result after the command is executed-the reverse apostrophe is in the middle of the command

When assigning a value, you can use the read command to use keyboard input as a variable

Read [- p "prompt"] variable name / / enter the manually entered value or string as a variable

Use custom variables for integer arithmetic

Format:

Expr variable 1 operator variable 2 [operator variable 3].

Common operators:

Addition operation: +

Subtraction operation:-

Multiplication:\

(\ as an escape character, only the number represents a wildcard)

Division operation: /

Remainder operation:%

Environment variable

​ is created in advance by the system to set up the user's working environment.

​ configuration file: / etc/profile (global configuration), ~ / .bash_profile (for specific accounts)

Common environmental variables

PWD 、 PATH

USER 、 SHELL 、 HOME

Location variable

Predefined variable

$#: the number of location variables on the command line

* $: * * contents of all location variables

$?: status after the last command was executed, displayed as 0 means true; is displayed as 1 indicates false

$0: currently executed process / program name

Unfinished to be continued ~

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report