当前位置:网站首页>Shell script Basics - basic grammar knowledge
Shell script Basics - basic grammar knowledge
2022-07-03 04:40:00 【Saiyujun】
List of articles
SHELL Script Basics
One 、shell Script
- Application scenarios
- Repeat the work 、 Automatic execution
- It's essentially a text file
Two 、 Output statement
1、echo
- Single quotation marks
- All characters will be displayed as normal characters
- Double quotes
- Special characters will be escaped, such as (!! Will call the last historical command )
[[email protected] ~]# echo -e "SHELL\nPython\nGOlang"
SHELL
Python
GOlang
[[email protected] ~]# echo -e "Linux\tWindows\tUNIX"
Linux Windows UNIX
[[email protected] ~]# echo "hello!!"
echo "hellocat /etc/hosts"
hellocat /etc/hosts
[[email protected] ~]# echo 'hello!!'
hello!!
2、here document
- Easy to display multi line content
[[email protected] ~]# cat << eof
> 1. Create a virtual machine
> 2. View virtual machine
> 3. Destroy virtual machine
> 4. View system resources
> eof
- Write a script
[[email protected] ~]# vim 1.sh
cat << eof 1、 Create a virtual machine 2、 View virtual machine 3、 Delete virtual machine 4、 Restore virtual machine 5、 Go back to the previous step eof
[[email protected] ~]# bash 1.sh
1、 Create a virtual machine
2、 View virtual machine
3、 Delete virtual machine
4、 Restore virtual machine
5、 Go back to the previous step
3、 ... and 、 Redirection symbol
1、 type
Output redirection
> >> 2> &>
Input redirection
<
2、 Three devices
- /dev/stdin Standard input devices keyboard 、 mouse 0
- /dev/stdout Standard output equipment Monitor 1
- /dev/stderr Standard error output device Monitor 2
3、 Output redirection
- > Cover
- Only standard output can be redirected
[[email protected] ~]# ls -ldh /etc/ > /tmp/file01
[[email protected] ~]# cat /tmp/file01
drwxr-xr-x. 76 root root 8.0K Sep 17 09:02 /etc/
[[email protected] ~]# ls -ldh /dev/ > /tmp/file01
[[email protected] ~]# cat /tmp/file01
drwxr-xr-x 20 root root 3.2K Sep 17 09:02 /dev/
- >> Additional
- Only standard redirected output can be
[[email protected] ~]# ls -ldh /dev/ > /tmp/file01
[[email protected] ~]# cat /tmp/file01
drwxr-xr-x 20 root root 3.2K Sep 17 09:02 /dev/
[[email protected] ~]#
[[email protected] ~]# ls -ldh /etc/ >> /tmp/file01
[[email protected] ~]#
[[email protected] ~]# cat /tmp/file01
drwxr-xr-x 20 root root 3.2K Sep 17 09:02 /dev/
drwxr-xr-x. 76 root root 8.0K Sep 17 09:02 /etc/
- /dev/null Black hole file
[[email protected] ~]# ls -ldh /etc/ > /dev/null
- 2>
- Can only redirect error messages
[[email protected] ~]# ls -ldh /dsjlfdsjlfdsjlf 2> /dev/null
[[email protected] ~]# ls -ldh /etc/ 2> /dev/null
drwxr-xr-x. 76 root root 8.0K Sep 17 09:02 /etc/
- &>
- Rewrite all information
4、 Input redirection
- Use the contents of the file as keyboard input
[[email protected] ~]# tr 'a-z' 'A-Z' < /tmp/file01
ABCDE
Four 、 Use of variables
1、 Variable type
- Custom variable
- environment variable
- Special variables
2、 Custom variable
- Defining variables
Variable name = value
- Canonical variable name
- Only letters can appear , Numbers , Underline
- Cannot appear with shell Keyword conflict
- Know what you know
- Call variables ( Be careful not to write the variable name wrong )
- $ Variable name
- ${ Variable name }
[[email protected] ~]# ip_address=192.168.152.10
[[email protected] ~]# echo $ip_address
192.168.152.10
[[email protected] ~]# echo $(ip_address)
[[email protected] ~]# data=server
[[email protected] ~]# echo "There are some $datas"
There are some
[[email protected] ~]# echo "There are some ${data}s"
There are some servers
1、 Define variables interactively
- # read -p “ Prompt information ” Variable name
[[email protected] ~]# read -p " user name : " name
user name : userA
[[email protected] ~]# echo $name
userA
[[email protected] ~]# vim 1.sh
read -p " user name :" user
echo " Hello! :" $user
[[email protected] ~]# bash 1.sh
user name :lijianhua
Hello! : lijianhua
2、shell All variables stored in are characters
[[email protected] ~]# a=10
[[email protected] ~]# b=20
[[email protected] ~]# c=a+b
[[email protected] ~]#
[[email protected] ~]# echo $c
a+b
[[email protected] ~]# c=$a+$b
[[email protected] ~]# echo $c
10+20
3、 Mathematical operations
- + - * %( Remainder )
- $(())
[[email protected] ~]# a=10
[[email protected] ~]# b=20
[[email protected] ~]# echo $((a+b))
30
- let keyword
[[email protected] ~]# let c=a+b
[[email protected] ~]# echo $c
30
- declare
[[email protected] ~]# declare -i c=a+b
[[email protected] ~]# echo $c
30
4、 obtain 100 Inner random number
[[email protected] ~]# let rand=$RANDOM%100
[[email protected] ~]# echo $rand
43
3、 environment variable
- Save the current operating environment of the system
1、 View environment variables
env
2、 Definition 、 Modify environment variables
- Environment variable names must be capitalized
Provisional entry into force :export environment variable = value
permanent :vim /etc/profile
export environment variable = value
Example 1 : Define the time of the historical command
export HISTTIMEFORMAT="%F_%T "
Example 2 : Define the number of command lines in the current user history , Default 1000
export HISTSIZE=10
Example 3 : Modify system language
export LANG=en_us.UTF-8
4、 Special variables
- $?
- The execution status code of the last command
- Value range 0—255
- 0 success
- Not 0 Failure
5、 ... and 、shell Script example
1、 Configure local yum Source
#!/bin/bash
#
mkdir /mnt/cdrom
sed -i '$a \/dev/sr0 /mnt/cdrom iso9660 defaults 0 0' /etc/fstab
mount -a
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/CentOS* /etc/yum.repos.d/backup/
touch /etc/yum.repos.d/yum.repo
cat << eof > /etc/yum.repos.d/yum.repo [centos] name=centos baseurl=file:///mnt/cdrom enabled=1 gpgcheck=0 eof
yum repolist
2、 Configure network parameters
#!/bin/bash
#
read -p " Manual configuration IP Address , Please enter :" ip
nmcli connection modify ens160 autoconnect yes ipv4.method manual ipv4.addresses $ip ipv4.gateway 192.168.152.2 ipv4.dns 223.5.5.5
nmcli connection reload
nmcli connection up ens160 >> /dev/null
IP=$(ifconfig ens160 | sed -n '2p' | awk '{print $2}')
ROUTE=$(route -n | sed -n '3p' | awk '{print $2}')
DNS=$(cat /etc/resolv.conf|sed -n '2p' | awk '{print $2}')
echo "=============================="
echo -e " The changed IP:$IP\n The changed gateway is :$ROUTE\n The changed DNS by :$DNS"
边栏推荐
- Handling record of electric skateboard detained by traffic police
- When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
- Matplotlib -- save graph
- Leetcode simple question: check whether the array is sorted and rotated
- 怎么用Kotlin去提高生产力:Kotlin Tips
- [software testing-6] & Test Management
- Games101 Lesson 9 shading 3 Notes
- 【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
- Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
- Small program animation realizes the running lantern and animation object
猜你喜欢
STM32 reverse entry
使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
Smart contract security audit company selection analysis and audit report resources download - domestic article
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found
Youdao cloud notes
UiPath实战(08) - 选取器(Selector)
MediaTek 2023 IC written examination approved in advance (topic)
Number of 1 in binary (simple difficulty)
随机推荐
C language series - Section 3 - functions
Joint search set: the number of points in connected blocks (the number of points in a set)
使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
【XSS绕过-防护策略】理解防护策略,更好的绕过
Matplotlib -- save graph
【SQL注入】联合查询(最简单的注入方法)
How to choose cross-border e-commerce multi merchant system
Leetcode simple problem delete an element to strictly increment the array
JS multidimensional array to one-dimensional array
Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
Basic use of continuous integration server Jenkins
使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
data2vec! New milestone of unified mode
Factor stock selection scoring model
Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found
Symbol of array element product of leetcode simple problem
"Niuke brush Verilog" part II Verilog advanced challenge
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
MySQL winter vacation self-study 2022 12 (3)