当前位置:网站首页>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"
边栏推荐
- MC Layer Target
- Contents of welder (primary) examination and welder (primary) examination in 2022
- [tools run SQL blind note]
- STM32 reverse entry
- FFMpeg filter
- General undergraduate college life pit avoidance Guide
- MediaTek 2023 IC written examination approved in advance (topic)
- What functions need to be set after the mall system is built
- Dive into deep learning - 2.1 data operation & Exercise
- 文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
猜你喜欢

Golang -- realize file transfer

2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis

stm32逆向入门

I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional

Smart contract security audit company selection analysis and audit report resources download - domestic article

FISCO bcos zero knowledge proof Fiat Shamir instance source code

data2vec! New milestone of unified mode

The simple problem of leetcode: dismantling bombs

Internationalization and localization, dark mode and dark mode in compose

Leetcode simple question: the key with the longest key duration
随机推荐
[free completion] development of course guidance platform (source code +lunwen)
AWS VPC
JVM原理简介
[set theory] binary relation (example of binary relation on a | binary relation on a)
Integration of Android high-frequency interview questions (including reference answers)
Web - Information Collection
Summary of training competition (Lao Li's collection of questions)
How to choose cross-border e-commerce multi merchant system
Factor stock selection scoring model
金仓数据库KingbaseES 插件kdb_exists_expand
雇佣收银员(差分约束)
data2vec! New milestone of unified mode
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
Pyqt control part (II)
RSRS index timing and large and small disc rotation
What functions need to be set after the mall system is built
Arthas watch grabs a field / attribute of the input parameter
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
[tools run SQL blind note]
Learning practice: comprehensive application of cycle and branch structure (I)