当前位置:网站首页>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"
边栏推荐
- Two drawing interfaces - 1 Matlab style interface
- Why should programmers learn microservice architecture if they want to enter a large factory?
- Introduction to message queuing (MQ)
- 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
- Kingbasees plug-in KDB of Jincang database_ exists_ expand
- C Primer Plus Chapter 10, question 14 3 × 5 array
- [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)
- 一名外包仔的2022年中总结
- 文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
- 2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
猜你喜欢
![[Thesis Writing] how to write the overall design of JSP tourism network](/img/02/841e8870c2ef871c182b9bb8252a83.jpg)
[Thesis Writing] how to write the overall design of JSP tourism network

data2vec! New milestone of unified mode

Learning practice: comprehensive application of cycle and branch structure (I)

Uipath practice (08) - selector

The reason why the entity class in the database is changed into hump naming

2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills

7. Integrated learning
![[fxcg] market analysis today](/img/ac/294368e3496a5b808b38833053ee81.jpg)
[fxcg] market analysis today

Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute

Web security - CSRF (token)
随机推荐
C language series - Section 3 - functions
怎么用Kotlin去提高生产力:Kotlin Tips
Jincang KFS data bidirectional synchronization scenario deployment
2022-02-13 (347. Top k high frequency elements)
[free completion] development of course guidance platform (source code +lunwen)
[Thesis Writing] how to write the overall design of JSP tourism network
Youdao cloud notes
Two drawing interfaces - 1 Matlab style interface
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
What functions need to be set after the mall system is built
[tools run SQL blind note]
[set theory] Cartesian product (concept of Cartesian product | examples of Cartesian product | properties of Cartesian product | non commutativity | non associativity | distribution law | ordered pair
The least operation of leetcode simple problem makes the array increment
Leetcode simple question: check whether the string is an array prefix
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
联发科技2023届提前批IC笔试(题目)
【XSS绕过-防护策略】理解防护策略,更好的绕过
Symbol of array element product of leetcode simple problem
[set theory] binary relationship (definition field | value field | inverse operation | inverse synthesis operation | restriction | image | single root | single value | nature of synthesis operation)
General undergraduate college life pit avoidance Guide