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-programming specifications and variables (1)

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

Share

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

Overview of Shell script

The concept of shell scripts:

Save the commands to be executed to a text file in order

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

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

Shell script application scenarios:

Repetitive operation

Batch transaction processing

Automatic operation and maintenance

Service running status monitoring

Scheduled task execution

...

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

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

Login shell of the user

The default shell program used after login, usually / bin/bash

The internal instructions and running environment of different shell will be different.

Write the first Shell script

Write script code

Use the vim text editor

One Linux command per line, written in order of execution

[root@localhost ~] # vim first.sh Edit the first script #! / bin/bash shell statement # first test comment cd / boot/ command line pwdls-lh vml*

Give executable permissions

Make a script executable

Chmod + x first.sh assigns execution attributes

Execute script file

Execute permission is required to execute, but do not change the location

. / first.sh

No execution permission is required, but change the location

. First.sh

Source first.sh

No execution permission is required and the location will not be changed.

Sh first.sh

Better script composition

Script declaration

Comment information

Executable statement

The current directory of "#! / bin/bash#first testcd / boot/echo" is located in "pwdecho" where the files that begin with vml include: "ls-lh vml*"

Redirection and pipe operation

Interactive hardware equipment

Standard input: receives data entered by the user from this device

Standard output: output data to the user through this device

Standard error: an execution error message is reported through this device

Type device file description number default device standard input / dev/stdin0

Keyboard

Standard output / dev/stdout1 display Standard error output / dev/stderr2 display

Redirect operation

Type operator usage redirection input saves the output to the specified file (overwrites the original) > > appends the output to the specified file

Standard error output 2 > Save error information to specified Wen Jiaan (overwrite original content) 2 > append error information to specified file mixed output & > Save standard output and standard error contents to the same file

Pipe operation symbol "|"

Output the result of the command on the left as the object of the command on the right

The function, type of shell variable

The role of variables

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

Variable name: use a fixed name, preset by the system or user defined

Variable value: can be changed according to user settings and changes in the system environment

Type of variable

Custom variables: defined, modified and used by the user

Environment variables: system maintenance, 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 variabl

Define a new variable

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

Variable name = variable value

View the value of a variable

Echo $variable name

Use quotation marks when assigning values

Double quotation marks: allows other variable values to be referenced by the $symbol

Single quotation marks: reference to other variable values is prohibited, and $is regarded as a normal character

Reverse apostrophe: replace the command and extract the output after the execution of the command

Assign a value to a variable from keyboard input

Read [- p "prompt message"] variable name

Set the scope of the variable

Format 1:export variable name. Format 2:export variable name = variable value.

The two formats can be mixed.

Operation of integer variables

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

Commonly used operators

Addition operation: +

Subtraction operation:-

Multiplication:\ *

Division operation: /

Modular (remainder) operation:%

Small script example: enter two numbers and find their sum

#! / bin/bash# enter two integers to find their sum # enter the first number read-p "Please enter an integer" num1# enter the second number read-p "Please enter an integer" num2# for addition operation sum= `expr $num1 + $num2` # shows the sum of two numbers echo "the sum of the two numbers is: $expr"

Special shell variable

Environment variable

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,SHELL,HOME

Location variable

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

#! / bin/bashecho "summation: `expr $1 + $2`"

Predefined variable

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

$*: contents of all location variables

$?: the status returned after the last command was executed. A status value of 0 indicates normal execution, while a non-zero value indicates an exception or error.

$0: currently executed process / program number

#! / bin/bashecho "summation is: `expr $1 + $2`" echo "script name: $0" echo "number of variables on the command line: $#" echo "contents of all location variables: $*"

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