当前位置:网站首页>Shell programming specifications and variables
Shell programming specifications and variables
2022-07-29 00:36:00 【Blue moon Champions League King】
Catalog
(1)Shell The concept of script
(2)Shell Script application scenarios
3、 ... and , Pipe operator operation
(1) Interactive hardware devices
5、 ... and , Classification of variables
1, Classification of variables
One .shell
Shell It's a special application , It lies between the operating system kernel and the user , Acting as a “ command interpreter ” Role , Be responsible for receiving the operation instructions input by the user ( command ) And explain , Pass the operation to be performed to the kernel for execution , And output the execution result .
Bash(/bin/bash) It's most at the moment common shell There are many interpreter programs , Use different shell when , Its internal instructions , There are some differences in command line prompt and so on . adopt /etc/shells File understand what the current system supports shell Types of scripts .Linux The default version is Shell.

1.shell Script Overview
(1)Shell The concept of script
1 Save the commands to be executed in order to a text file
2 Give the file executable permissions
3 It can combine all kinds of Shell Control statements to perform more complex operations
(2)Shell Script application scenarios
1 Repetitive operations
2 Interactive tasks
3 Batch transactions
4 Service running status monitoring
5 Scheduled task execution
Two ,,shell Script execution
1, Method 1 : Command to specify the path , It is required that the document must have x jurisdiction

2 Method 2 : Appoint shell To explain the script , It is not required that the document must have x jurisdiction
sh Script path :sh first..sh
source Script path : .first.sh perhaps source first.sh: perhaps bash first.sh
[[email protected] opt]# chmod -x nxx.sh
[[email protected] opt]# ./hhd.sh
-bash: ./nxx.sh: Not enough permissions
[[email protected] opt]# . hhd.sh
Who comes in? Who's brother :
[[email protected] opt]# source hhd.sh
Who comes in? Who's brother :
[[email protected] opt]#
[[email protected] opt]# bash hhd.sh
Who comes in? Who's brother
[[email protected] opt]#
3、 ... and , Pipe operator operation
The pipe symbol "|" The command output on the left , As input to the command on the right ( Deal with people ), Multiple pipes can be used in the same command line .
stay shell Script , Pipeline operation is usually used to filter the required customs information .
Example

Example 2, combination awk Print using spaces for segmentation , Intercept the status of the running socket
[[email protected] opt]# netstat -natp |awk '{print $6}'
established)
Foreign
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
ESTABLISHED
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
[[email protected] opt]#
Four . Redirect
(1) Interactive hardware devices
| type | Device file | Document description number | Default device |
| The standard input | /dev/stdin | 0 | keyboard |
| standard output | /dev/stdout | 1 | Monitor |
| Standard error output | /dev/stderr | 2 | Monitor |
(2) Redirection operation
| type | The operator | purpose |
| Redirect input | < | Read data from the specified file |
| Redirect output | > | Save the standard output results to the specified file , And cover the original content |
| >> | Appends the standard output result to the end of the specified file , Don't cover the original content | |
| Standard error output | 2> | Save the error message to the specified file , And cover the original content |
| 2>> | Append the error message to the end of the specified file , Don't cover the original content | |
| Mixed output | &> | Standard output 、 Standard errors are saved to the same file |
| 2>&1 | Redirect standard error output to standard output |
example 1 ">" ">>"
[[email protected] opt]# echo "zzzzz" > z.txt
[[email protected] opt]# cat z.txt
zzzzz
[[email protected] opt]# echo "pppppp" >> z.txt
[[email protected] opt]# cat z.txt
zzzzz
pppppp
[[email protected] opt]# echo "dfwfwf" > z.txt
[[email protected] opt]# cat z.txt
dfwfwf
[[email protected] opt]#
> Indicates coverage
>> Indicates append
echo " Custom content " > Custom filename
passwd --stdin user name < Custom filename
ls -lh > log.txt 2>&1 Equate to ls -lh &> log.txt
Originally 1→ The screen (1 Point to the screen )
perform >log after , 1→log.txt (1 Point to log.txt)
perform 2>&1 after , 2→1 (2 Point to 1, and 1 Point to log.txt, therefore 2 It also points to log.txt)


5、 ... and , Classification of variables
1, Classification of variables
- It is used to store specific parameters that the system and users need to use
- environment variable : System maintenance , Application settings working environment
- Positional variable : Pass parameters to the script through the command line
- Custom variable : Defined by the user , Modify and use
- Predefined variables :bash A class of variables built into , Cannot be modified directly
2, Define a new variable
Format : Variable name = A variable's value
Variable naming rule : Start with a letter or underline , Case sensitive
[[email protected] opt]# zz=shagou
[[email protected] opt]# bb=caiji
[[email protected] opt]# echo $zz
xiaoyang
[[email protected] opt]# echo $bb
shini
[[email protected] opt]#
3, Custom variable
Bash The variable operation in is relatively simple , Unlike other high-level programming languages (( Such as c/C++、Java etc. ) So complicated . When defining a new variable , In general, you don't need
Make a statement in advance , Instead, specify the variable name directly and assign it to the initial value ( Content ) that will do
■ Format : Variable name = A variable's value
■ Variable name : A place where data is temporarily stored
A variable's value : Temporary changeable data
■ There is no space on either side of the equal sign . Variable names should start with letters or underscores , Do not include special characters in the name ( Such as +、 One 、*、/、.、 ?、8、&、# etc. )
■ use echo View and reference the values of variables
By adding a leading symbol before the variable name "$”, You can reference the value of a variable , Use echo Command can view variables , Can be in one echo View multiple variable values at the same time in the command
example 1
[[email protected] opt]# unset zz
[[email protected] opt]# unset bb
[[email protected] opt]# zz=aaa
[[email protected] opt]# bb=cxk
[[email protected] opt]# echo $zz$bb
aaacxk
[[email protected] opt]#
When variable names are easily confused with other characters immediately following them , Need to add “{}” Cover up , Otherwise, the correct variable name cannot be determined , For undefined variables , Will display as empty .
example 2,
[[email protected] opt]# unset zz
[[email protected] opt]# unset bb
[[email protected] opt]# zz=aaa
[[email protected] opt]# bb=cxk
[[email protected] opt]# echo $zz$bb
aaacxk
[[email protected] opt]# echo ${zz} ${bb}
aaa cxk
[[email protected] opt]#
Use quotation marks when assigning values
Double quotes : Allowed to pass through $ The symbol references other variable values
Single quotation marks : Do not reference other variable values ,$ Treat as normal characters
Apostrophe : Command substitution , Extract the output of the command after execution .`..` and $(..) The same effect
Apostrophe
[[email protected] opt]# vim zz.sh
[[email protected] opt]# cat zz.sh
#!/bin/bash
let i+=10
echo "$i"
[[email protected] opt]# bash zz.sh
10
[r[email protected] opt]# num =`bash zz.sh`
bash: num: Command not found ...
[[email protected] opt]# num=`bash zz.sh`
[[email protected] opt]# echo "$num"
10
[[email protected] opt]#
**
Extract the output result after the command is executed through the apostrophe
Backhand cannot be nested
边栏推荐
- ORACLE not available如何解决
- There is a span tag. If you want to do click events on it, how can you expand the click area
- What are the skills of API interface optimization?
- Newscenter, advanced area of attack and defense world web masters
- Application and principle of distributed current limiting redistribution rratelimiter
- Oracle实例无法启动的问题如何解决
- 刷题的第三十天
- Immutable x officially opens IMX token pledge detailed IMX pledge introduction optimistic about the development prospect of IMX
- MySQL transaction (this is enough...)
- Introduction and solution of common security vulnerabilities in Web System SQL injection
猜你喜欢

递归/回溯刷题(下)

Dynamic programming problem (6)

Still writing a lot of if to judge? A rule executor kills all if judgments in the project

面试被问到了String相关的几道题,你能答上来吗?

Web系统常见安全漏洞介绍及解决方案-sql注入

Attack and defense world web master advanced area PHP_ rce

PTA (one question per day) 7-76 ratio

Idea connection database

MySQL transaction (this is enough...)

What are the skills of API interface optimization?
随机推荐
刷题的第三十天
Camera Hal OEM module ---- CMR_ preview.c
What are the skills of API interface optimization?
Anti shake and throttling
“吃货联盟定餐系统”
The difference between {} and ${}
How to solve the problem that the Oracle instance cannot be started
ORACLE not available如何解决
Camera Hal OEM模块 ---- cmr_preview.c
What does WGet mean
Newscenter, advanced area of attack and defense world web masters
Do like and in indexes in MySQL go
CDN mode uses vant components, and components cannot be called after they are introduced
Google browser, no installation required
MySQL事务(transaction) (有这篇就足够了..)
PTA (daily question) 7-71 character trapezoid
Nftscan and nftplay have reached strategic cooperation in the field of NFT data
vulnhub:BTRSys2
Multimodal model sketch (1)
MySQL transaction (this is enough...)