当前位置:网站首页>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"
边栏推荐
- Crazy scientist
- MySQL winter vacation self-study 2022 12 (3)
- Number of 1 in binary (simple difficulty)
- FFMpeg filter
- data2vec! New milestone of unified mode
- [PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
- JS multidimensional array to one-dimensional array
- When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
- The least operation of leetcode simple problem makes the array increment
- 逆袭大学生的职业规划
猜你喜欢
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
How to retrieve the password for opening word files
After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
Leetcode simple question: check whether the array is sorted and rotated
Leetcode simple problem delete an element to strictly increment the array
Triangular rasterization
Handling record of electric skateboard detained by traffic police
关于开学的准备与专业认知
随机推荐
FISCO bcos zero knowledge proof Fiat Shamir instance source code
Leetcode simple question: check whether the string is an array prefix
Design and implementation of JSP logistics center storage information management system
[set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
[luatos sensor] 1 light sensing bh1750
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
Reptile exercise 02
Leetcode simple question: the key with the longest key duration
怎么用Kotlin去提高生产力:Kotlin Tips
Number of uniform strings of leetcode simple problem
Joint search set: the number of points in connected blocks (the number of points in a set)
Reptile exercise 03
Learning practice: comprehensive application of cycle and branch structure (I)
4 years of experience to interview test development, 10 minutes to end, ask too
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
Kingbasees plug-in KDB of Jincang database_ date_ function
智能合约安全审计公司选型分析和审计报告资源下载---国内篇
第十九届浙江省 I. Barbecue
【SQL注入】联合查询(最简单的注入方法)