The purpose of this chapter is to learn programming specifications, scripting operations and knowledge of variables.
I. Overview of Shell scripts
II. The Role of Shell
Write the first Shell script
1. Write a script (go to the BOOT directory, view the current location, and display all files starting with VML in a long format friendly)
[root@localhost~]# VIM aaa.sh:"Write an empty file at will and end with. sh" / bin/bash "Opening Statement, Fixed Format" A script "Descriptive information may or may not be added" cd /boot/ pwd ls -lh vml* ~ ~
2. Add execution privileges to our script and see the results
[root@localhost ~]# chmod +x aaa.sh [root@localhost ~]# . / aaa.sh // executing scripts is the most common way we use them.
/boot -rwxr-xr-x. 1 root root 5.7M 8 Month 1000:26 vmlinuz-0-rescue-cc65aecf945d4dde800fe3e29ee6edbc -rwxr-xr-x. 1 root root 5.7M 8 Month 232017 vmlinuz-3.10.0-693.el7.x86_64
3. Redirection Symbol ">" Pipeline Symbol "|"
" > " : Redirect the command results on the left to another file or directory [root@localhost opt]# touch abc.tt abd.txt [root@localhost opt]# ls abc.tt abd.txt rh [root@localhost opt]# tar czvf test.tar.gz *.txt > test2.txt [root@localhost opt]# ls abc.tt abd.txt rh test2.txt test.tar.gz [root@localhost opt]# cat test2.txt abd.txt [root@localhost opt]#
"|": The result of the command on the left is treated as the object of the command on the right.
Statistical occupancy of all disk partitions [root@localhost opt]# df -hT //File System Type Capacity Used Available% Mount Points /dev/sda2 xfs 10G 3.8G 6.3G 38% / devtmpfs devtmpfs 898M 0 898M 0% /dev tmpfs tmpfs 912M 0 912M 0% /dev/shm tmpfs tmpfs 912M 9.1M 903M 1% /run tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sdb1 xfs 20G 33M 20G 1% /mnt/sdb /dev/sda5 xfs 10G 37M 10G 1% /home /dev/sda1 xfs 6.0G 174M 5.9G 3% /boot tmpfs tmpfs 183M 12K 183M 1% /run/user/42 tmpfs tmpfs 183M 0 183M 0% /run/user/0 //Disk occupancy in the first column is in the fifth column [root@localhost opt]# df -h | grep "dev/sd*" | awk '{print $1,$5 }' /dev/sda2 38% tmpfs 0% /dev/sdb1 1% /dev/sda5 1% /dev/sda1 3% [root@localhost opt]#
4. Functions and types of variables
Predefined variables: Variable name Effect $0 Name of the current script $n A parameter passed to a script or function, n denotes the number of parameters $# Number of parameters passed to a script or function $* All parameters passed to a script or function $@ All parameters passed to a script or function $$ PID of the current shell script process $? Function return value, or exit status of last command
5. Define a new variable to see the value of the variable
Variable name = variable value
Variable names begin with letters or underscores, case-sensitive
[root@localhost opt]# Producht=Python [root@localhost opt]# Version=2.7.13 [root@localhost opt]# echo $Producht Python [root@localhost opt]# echo $Producht $Version Python 2.7.13 [root@localhost opt]#
6. Assignment uses quotation marks and keyboard input assigns variables
[root@localhost ~]# vim c.sh #!/bin/bash Read-p "Please enter an integer" num1 Read-p "Enter the second integer" num2 ~ [root@localhost ~]# chmod +x c.sh [root@localhost ~]# ./c.sh Please enter the first integer 21 Please enter two integers 232
7. Operation of Integer Variables
8. Location variables
[root@localhost ~]# vim .sss.sh
#!/bin/bash Read-p "The first location variable $1" Read-p "The second positional variable $2" sum=`expr $1 + $2` echo: "The sum of integers is $sum" ~ [root@localhost ~]# ./.sss.sh 23 45 The first position variable 23 The second position variable 45 The sum of integers is 68 [root@localhost ~]# ./.sss.sh 64 120 The first position variable 64 The second position variable 120 The sum of integers is 18
9. Environmental variables
[root@localhost ~]# echo $PWD /root [root@localhost ~]# echo $HOME /root [root@localhost ~]# echo $USER root [root@localhost ~]# echo $SHELL /bin/bash
10. Predefined variables
#!/bin/bash read -p "The first position variable $1" read -p "The second position variable $2" sum=`expr $1 + $2` echo "The sum of integers is $sum" echo "Script name $0" echo "Detailed figures $*" echo "Number of parameters $#" ~ ~
[root@localhost ~]# ./.sss.sh 64 120 The first position variable 64 The second position variable 120 The sum of integers is 184 The script name. /.sss.sh Detailed figure 64 120 Number of parameters 2