当前位置:网站首页>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"
边栏推荐
- MediaTek 2023 IC written examination approved in advance (topic)
- 【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
- Matplotlib -- save graph
- Ffmpeg tanscoding transcoding
- 2022-02-14 (394. String decoding)
- [Thesis Writing] how to write the overall design of JSP tourism network
- SSM based campus part-time platform for College Students
- 消息队列(MQ)介绍
- [XSS bypass - protection strategy] understand the protection strategy and better bypass
- Prefix and (continuously updated)
猜你喜欢

X-ray normal based contour rendering

2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank

Number of 1 in binary (simple difficulty)

Employee attendance management system based on SSM

2022 P cylinder filling test content and P cylinder filling simulation test questions

联发科技2023届提前批IC笔试(题目)

stm32逆向入门

使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found

FuncS sh file not found when using the benchmarksql tool to test kingbases

Joint set search: merge intervals and ask whether two numbers are in the same set
随机推荐
Introduction to message queuing (MQ)
Reptile exercise 02
Library management system based on SSM
金仓数据库KingbaseES 插件kdb_date_function
Dive into deep learning - 2.1 data operation & Exercise
2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
Sdl2 + OpenGL glsl practice (Continued)
stm32逆向入门
怎么用Kotlin去提高生产力:Kotlin Tips
Introduction to JVM principle
智能合约安全审计公司选型分析和审计报告资源下载---国内篇
Leetcode simple question: check whether the array is sorted and rotated
The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
Joint search set: the number of points in connected blocks (the number of points in a set)
2022 P cylinder filling test content and P cylinder filling simulation test questions
Design and implementation of JSP logistics center storage information management system
金仓数据库KingbaseES 插件kdb_exists_expand
A outsourcing boy's mid-2022 summary
逆袭大学生的职业规划