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 script Specification and variable Application

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

preface

As Linux systems become more and more popular in enterprises, server automation management becomes increasingly important. In Linux server automation maintenance work, in addition to the setting of scheduled tasks, the application of shell scripts is also a very important part.

What is Shell Script?

Shell scripts are similar to batch processing under Windows/Dos, that is, a program file that is pre-placed into a file with various commands for one-time execution, mainly for the convenience of administrators for setting or management. But it is more powerful than batch processing under Windows and more efficient than programs edited with other programming programs, using commands under Linux/Unix.

Shell script application scenarios:

Repetitive Operations Batch Transaction Processing Automation Operation and Maintenance Service Running Status Monitoring Timed Task Execution...

Attention! Shell scripts must be executable (chmod +x...)

shell's role--command interpreter, translator

The requested URL/bin/bash was not found on this server.

Linux compressed kernel is placed in/boot/directory

Write shell script code one Linux command per line using vi text editor, written once in execution order

Executing script files

Executing the command with source will switch the path execution, and using sh will not switch the path

" . / "is executed in the same way as sh

Redirection and pipeline operations

interactive hardware device

Standard Input: Data received from the device as input by the user

Standard Output: Output data to the user through the device

Standard Error: An execution error message is reported by this device

redirection operation

Pipe operation symbol "| "

Shell variable role, type

Role of variables

Provides specific parameters for flexible management of Linux systems, with two levels of meaning Variable names: use fixed names, system default or user-defined

Variable value: can change according to user settings and system environment changes

the type of a variable

Custom variables: defined, modified, and used by the user Environment variables: maintained by the system, used to set the working environment Location variables: parameters passed to script programs through the command line Predefined variables: a class of variables built into Bash that cannot be modified directly

custom variables

Define a new variable

Variable names start with a letter or underscore, case sensitive, all caps recommended

Variable name = variable value ("=" given sign)

To view the value of a variable, use the "echo" command

Use quotation marks when assigning

Double quotes: Allow references to other variable values via the $sign Single quotes: Prohibited references to other variable values,$treated as a normal character Backprime: Command substitution, extract output after command execution

Input from keyboard assigns value to variable read [-p "prompt"] Variable name

Set the scope of the variable. The two formats can be mixed.

Format 1: export variable name

Format 2: export variable name = variable value

Operations on integer variables

expr variable 1 operator variable 2 [operator variable 3] ……

common operator

Addition: + Subtraction: -Multiplication: * Division: /Modulus (remainder): %

Special Shell Variables

environment variable

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

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

Common environment variables: PWD, PATHUSER, SHELL, HOME

The position variable is expressed as $n, where n is a number between 1 and 9

Predefined variable $#: Number of positional variables on the command line $*: Contents of all positional variables $?: The status returned after the execution of the previous command. When the returned status value is 0, it means that the execution is normal. A value other than 0 means that the execution is abnormal or an error occurs.$0: 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