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

SHELL programming specifications and variables

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Overview of Shell programming Specification and variable Sheel script

Save the commands to be executed sequentially to a text document

If the file is given executable permission, it can be run

Various Shell control statements can be combined to accomplish more complex operations

Scenarios where Shell scripts should be shipped

Repetitive operation

Batch transaction processing

Automatic operation and maintenance

Service Health Monitoring (zabbix)

Scheduled task execution

The role of Shell-- command interpreter, "translator"

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

User's login Shell default Shell program used after login, generally / bin/bash different Shell internal instructions, running environment, etc. will be different to write script code.

Use the vi text editor

One Linux command per line, written in order of execution

Give enforceable power

Make the script executable

[root@localhost~] # chmod + x first.sh execution script file

Method 1: script file path (absolute path and relative path)

[root@localhost~] #. / first.sh

Execution permission is required, and the location will not be changed after the execution is completed.

Method 2: sh script file path

[root@localhost~] # sh / first.sh

You don't need permission to execute, but you won't change the location.

Method 3: source script file path

[root@localhost~] # source / first.sh

Does not require permission to execute, but will change the location of the directory

Method 4: "." The use of

[root@localhost~] #. First.sh

The execution can also be executed successfully. X execution permission is not required. Change the location.

Better script composition

(1) script declaration

#! / bin/bash

(2) comment information

Echo "current directory is located in"

(3) executable statement

Ls-ln vml*

As shown below:

Interactive hardware device for programming

(1) Standard input: receives user input data from this device / dev/stdin 0 keyboard

(2) Standard output: output data / dev/stdout 1 display to the user through this device

(3) Standard error: error message / dev/stderr 2 display is reported by this device.

Redirect and pipe operation redirect operation type operator usage redirection input save output to specified folder (overwrite original content) redirect output > append output information to specified file standard error output 2 > save error information to specified file (overwrite original content) standard error output 2 > append error information to specified file mixed output & > standard output, The contents of standard errors are saved to the same file as pipe operation symbols.

"|"

Output the result of the command on the left. As the processing object of the command on the right

Cmd1 | cmd2 [... | cmdn] the function of Shell variable and type variable

Providing specific parameters for flexible management of Linux systems has two meanings

(1) variable name: use a fixed name, preset by the system or user-defined

(2) variable value: can be changed by user settings and changes in the system environment.

Type of variable

(1) Custom variables: defined, modified and used by the user

(2) Environment variables: maintained by the system and used to set up the working environment

(3) position variable: pass parameters to the script through the command line

(4) predefined variables: a class of variables built into Bash that cannot be modified directly

Custom variable defines a new variable

Variable names begin with a letter or underscore and are case-sensitive. All uppercase is recommended.

Variable name = variable value View variable value # # title echo $variable name

Use quotation marks when assigning values: allow references to other variable values through the $symbol single quotation marks: prohibit referencing other variable values, $as ordinary character reverse apostrophe (`): command replacement, extract the output result after the command from the keyboard input to assign the variable read [- p "prompt message"] variable name set variable scope format 1:export variable name format 1:export variable name = variable value

The two formats can be mixed.

Operation of integer variables expr variable 1 operator variable 2 [operator variable 3] common operators

(1) addition operation: +

(2) subtraction:-

(3) multiplication: *

(4) Division operation: /

(5) Modulus (remainder) operation:%

Special Shell variables environment variables

Created in advance by the system to set up the user's working environment

Configuration file: / etc/profile, ~ / .bash_profile

Common environmental variables

PWD 、 PATH 、 USER 、 HOME 、 SHELL

Location variable

Expressed as a number between $n and n = 1 to 9

[root@localhost ~] #. / myprog.sh one two three four five sixone: $1, first position parameter two:$2, second location parameter tsix:$6, sixth position parameter predefined variable (1) $#: number of location variables on the command line (2) $*: contents of all location variables (3) $? : the status returned after the last command is executed. When the return value is 0, the execution is normal. Non-zero only indicates an exception or error. (4) $0: the name of the currently executed process / program.

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