The most detailed tutorial on Linux

Custom Linux virtual machine installation

Click on New Virtual Machine
Insert image description here
Insert image description here

Insert image description here

Insert image description here
If the C drive has an SSD, it is recommended to install the virtual machine on the C drive because it will be faster to open the virtual machine after installation.
At the same time, it is recommended to create a new directory to store the virtual machine.
Insert image description here

Insert image description here

It is recommended that the virtual machine have at least 1024 MB of memory
Insert image description here

Insert image description here

Insert image description here

Insert image description here
Insert image description here
It is recommended to be above 100GB here, because big data needs to be run.
But what we need to understand is that filling in 100GB local disk here will not immediately allocate such a large memory space to the virtual machine disk,
but stipulates that the virtual machine disk can use the c drive Maximum capacity in case of emergency

Insert image description here

Virtual machine disk file storage directory
Insert image description here

Insert image description here

The specified disc image file has been shared to Baidu Cloud

Link: https://pan.baidu.com/s/1AVY_tOEbL-TYGJ7cpQ0eIw
Extraction code: md77
Copy this content and open the Baidu Netdisk mobile app for more convenient operation.
Insert image description here
Note:
The Linux version is selected according to your own Linux version. For example: I am using CentOS-6.5-x86_64-minimal.iso,
so when selecting the Linux version, select Centos 64-bit
to add the ISO image file, and then start the virtual machine.

Wait for the initialization page to load to complete the virtual machine installation and configuration (if there are special instructions, just select Continue/OK/Next) to skip the disk check, otherwise the disk check will take a long time (confirm with the left and right keys of the keyboard + Enter). It is recommended to select the language. English
Insert image description here


Insert image description here

Insert image description here

Insert image description here

Insert image description here
Insert image description here
For time zone selection, it is recommended to choose Asia/Shanghai. If you select the time zone here, the time in the virtual machine will be synchronized with our current time, which will
bring great convenience to us in the future. Believe me, I am experienced!!!
Insert image description here

Insert image description here

Insert image description here

Carry out disk partitioning. Three partitions are required here.

Insert image description here

Create the first partition

Insert image description here
Create a second partition

Insert image description here

Create a third partition

Insert image description here

After the three partitions are configured, the effect is as follows, click next
Insert image description here

Insert image description here
Insert image description here
After the file is downloaded, continue Next

Insert image description here
Restart, then enter the username and password. After entering ls, the following three files appear, which means the installation is successful.

Insert image description here

Network Configuration

1.node1 network configuration

.Configure static IP
vi /etc/sysconfig/network-scripts/ifcfg-eth0
edit content

# ifcfg-eth0 网卡配置,使用打开后编辑,
# ONBOOT=on 开机自启 
# BOOTPROTO=static 表示 IP 使用静态IP
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.179.140  #这里需要根据自己的网段填写
NETMASK=255.255.255.0
GATEWAY=192.168.170.2
DNS1=144.144.144.144
DNS2=8.8.8.8

   
   
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

View the network segment. From the picture below, you can see that my network segment is 192.168.179.0
, so it can be configured between 192.168.179.3-192.168.179.254
Insert image description here
. Delete the mac address mapping file.

cd /etc/udev/rules.d
rm 70-persistent-net.rules -f

   
   
    
    
  • 1
  • 2

Insert image description here
Check whether your IP configuration is normal

ifconfig

   
   
    
    
  • 1

Insert image description here

Finally ping Baidu

Insert image description here

2. Clone a virtual machine through a snapshot

Shut down the virtual machine poweroffand create a snapshot
Insert image description here

Clone the virtual machine through the snapshot
Insert image description here
Insert image description here
Insert image description here
and create two more in this way, for a total of four.
The cloned files are exactly the same, which means that the login account and password are the same as our first virtual machine.

3. Configure the other three node virtual machines

1. Modify the IP of each virtual machine (IP address + 1 in sequence),
2. Delete the mac mapping file (.rules file, as shown in Figure 1),
3. Modify the host name (vi /etc/sysconfig/network), and restart the command It will take effect (Figure 2)
Note: Modify the host name of the Alibaba Cloud server hostnamectl set-hostname 主机名and then restart it

Figure 1
Insert image description here
Figure 2: Modify the host name (node2, node3, node4 in order)
Insert image description here

Linux simple commands

Schematic diagram of shell command operation

Insert image description here

1. Shutdown and restart

name Order
Shut down poweroff / init 0
Restart reboot / init 6

2. Judge the order of the order

type 具体命令: Command type (external command & internal command)
help: Internal command help help: Internal command list, with syntax format, description
help 具体内部命令Introduction to specific commands
man 具体命令: Help manual, more powerful than help ( yum install man man-pages -y)
whereis 具体命令: Locate command location
file 具体文件: View file details ( ELF represents binary file)
echo: print to standard output;: $PATHenvironment variables: path, $LANGlanguage

Insert image description here

3.Commonly used function commands

lsDisplay directory content ll --i
pwdDisplay the absolute path of the current directory
cdSwitch directory
mkdirFolder -p {}
cpCopy -r
mvMove
touchCreate folders and files
typeCommand name View the storage location of the file specified by the command External command & Internal command
catView file content
ps -feprocess list (Analyze ps command type ps man ps)
echo $$PID of current shell

4. File system commands

df -h:View disk partitions
mount/unmoutand mount/unmount disk files

Insert image description here

Insert image description here

File System Hierarchy Standard

Located in the Linux root directory, you can directly access it: cd /root, cd /var

file name effect
/boot System startup-related files, such as kernel, initrd, and grub (bootloader)
/dev device file
/etc Configuration file
/home The user's home directory. Each user's home directory usually defaults to /home/USERNAME.
/root Administrator's home directory
/lib Library file
/media Mount point directory, mobile device
/mnt Mount point directory, additional temporary file system
/opt Optional directory, the installation directory of third-party programs
/proc Pseudo file system, kernel map file
/tmp Temporary Files
/was changeable file
/bin executable file, user command
/sbin Administrative commands

System startup requires:

/boot: Stores the kernel files used when starting Linux, including connection files and image files.
/etc: Stores all configuration files and subdirectory lists required by the system. Changing files in the directory may cause the system to fail to start.
/lib: Stores basic code libraries (such as c++ libraries), its function is similar to DLL files in Windows. Almost all applications require these shared libraries.
/sys: This is a big change in the linux2.6 kernel. A new file system sysfs that appeared in the 2.6 kernel is installed in this directory. The sysfs file system integrates information from the following three file systems: the proc file system for process information, the devfs file system for devices, and the devpts file system for pseudo terminals. The file system is a visual reflection of the kernel device tree. When a kernel object is created, the corresponding files and directories are also in the kernel object subsystem

Instruction set:

/bin: stores the most commonly used programs and commands
/sbin: programs and commands that only system administrators can use.

External file management:

/dev: The abbreviation of Device (device), which stores Linux external devices. Note: Accessing devices and files in Linux is the same.
/media:
Other Windows-like devices, such as U disks, CD-ROM drives, etc. After recognition, Linux will place the devices in this directory.
/mnt: To temporarily mount other file systems, we can mount the optical drive on /mnt/, and then enter this directory to view the contents of the optical drive.

Temporary Files:

/run: is a temporary file system that stores information since the system started. When the system restarts, files in this directory should be deleted or cleared. If you have a /var/run directory on your system, it should point to run.
/lost+found: It is usually empty. After the system is illegally shut down, some files are stored here.
/tmp: This directory is used to store some temporary files.

Account:

/root: The system administrator’s user home directory.
/home: The user's home directory, named after the user's account.
/usr: Many user applications and files are placed in this directory, similar to the program files directory under Windows.
/usr/bin: Applications and commands used by system users.
/usr/sbin: More advanced management programs and system daemons used by super users.
/usr/src: The default placement directory for kernel source code.

During operation, use:

/var: Stores frequently modified data, such as log files for program running (under the /var/log directory).
/proc: The virtual directory that manages the memory space is a mapping of system memory. We can directly access this directory to obtain system information. The contents of this directory are not on the hard disk but in memory. We can also directly modify some files in it to make changes.

For extension:

/opt: It is empty by default. If we install additional software, we can put it in this.
/srv: Stores the data that needs to be extracted after the service is started** (it will be empty if the server is not used)**

5. Text operation commands

basic commands

Order effect
cat Show all text file contents
more Display text file content page by page
less Read the entire file content at once. Compared with more, you can look back.
head Display the contents of the first n lines
tail Display the content of the next n lines
| pipeline

Display the number of line 6 of the profile file through the pipeline (can be used for data reading in the future)

Insert image description here

vi full screen text editor

Turn on support for vim

yum install  -y vim

 
 
  
  
  • 1

Full screen editor mode

Edit mode: The button has the function of editing text: it is opened by default to enter the edit mode.
Input mode: The meaning of the button itself.
Last line mode: Accepts user command input.

1.Open the file

vim /path/to/somefile

 
 
  
  
  • 1

vim +#:Open the file and locate at line #
vim +: Open the file and locate at the last line
vim +/PATTERN: Open the file and locate at the beginning of the line matched by PATTERN for the first time

2. Close the file

Last line mode
:q Exit without touching the file
:wqSave and exit after touching it, no regrets
:q!Save and exit without touching the file, but regret it
:wSave
:w!Forced save
:wq<==>:x

3.Edit mode

Move cursor

Character : left; : bottom; : top; : right
hjkl

Word
w: Move to the beginning of the next word
e: Jump to the end of the current or next word
b: Jump to the beginning of the current or previous word

Inline
0: Absolute start of line : First non-whitespace character at start of line : Absolute end of line
^
$

Between lines : End of article : Line 3 : Beginning of article
G
3G
gg

Turn the screen : next page previous page
ctrl f
ctrl b

Delete & replace a single character
x: Delete the character at the cursor position : Delete the 3 characters
3xstarting from the cursor : Replace the character at the cursor position Delete command: Delete 3 lines of data Delete a word (delete word) Delete a line
r

d3
dw
dd

Copy, paste & cut (p is equivalent to the paste key)
yw +pCopy a word
yy +pCopy a line of data

Undo & Redo Undo Redo the undone operation Repeat the previous step
u
ctrl+r
.

4.Last line mode

set: Set to
set nudisplay line number,
set nonuhide line number,
set readonlyset to read-only

Find
:/afterFind the same word as after + n,Njump to the next one

sfind and replace

# 语法
s/str1/str2/gi

#Replace search from the current cursor to the end of the file
eg: ., $s /str1/str2/gi

  • 1
  • 2
  • 3
  • 4
  • 5

/: The first character adjacent to the s command is a boundary character: /, @, #
g: Replace all in the current line (if not selected, replace the first replacement in the current line)
i: Ignore case
Range
n: Line number
.: Current cursor line
+n: offset n lines
$: last line, $-3
%: full text

Last line mode techniques
:Gd Clear content
:.,$dDelete all lines from the current cursor to the end
:n,mdDelete all content from line n to line m
:n,myCopy all content from line n to line m

regular expression

basic grammar

	grep:	显示匹配行
	v:		反显示
	e:		使用扩展正则表达式(如果不不开启, 需要在 "(",")"等操作符前加反斜杠
匹配操作符
\                  		转义字符
<span class="token keyword">.</span> 	                 	匹配任意单个字符
<span class="token punctuation">[</span>1249a<span class="token punctuation">]</span>,<span class="token punctuation">[</span>^12<span class="token punctuation">]</span>,<span class="token punctuation">[</span>a-k<span class="token punctuation">]</span>  	字符序列单字符占位
^                 		行首<span class="token punctuation">(</span>在中括号外<span class="token punctuation">)</span>,在<span class="token punctuation">[</span>^<span class="token punctuation">]</span> 表示取反 eg: <span class="token punctuation">[</span>^0-9<span class="token punctuation">]</span> 第一个字符不为数字
$                  		行尾
\<span class="token operator">&lt;</span>,\<span class="token operator">&gt;</span>:\<span class="token operator">&lt;</span>abc           	单词首尾边界
<span class="token operator">|</span>                  		连接操作符
<span class="token punctuation">(</span>,<span class="token punctuation">)</span>              		选择操作符
\n    	     			反向引用

重复操作符:
?      	匹配0到1次
*      	匹配0到多次
+     	匹配1到多次
<span class="token punctuation">{</span>n<span class="token punctuation">}</span>   	匹配n次
<span class="token punctuation">{</span>n,<span class="token punctuation">}</span>  	匹配n到多次
<span class="token punctuation">{</span>n,m<span class="token punctuation">}</span>   匹配n到m次

与扩展正则表达式的区别:grep basic
\?, \+, \<span class="token punctuation">{</span>, \<span class="token operator">|</span>, \<span class="token punctuation">(</span>, and \<span class="token punctuation">)</span>
匹配任意字符
.*
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

Example 1

#在未扩展正则表达式时,若匹配规则中含有 ?,+,{,},|,(,) ,都需要通过 \ 转义,否则匹配不到
#查询a.txt中包括4位整数的行,
grep "\(^[0-9]\|[^0-9][0-9]\)[0-9]\{2\}\([0-9][^0-9]\|[0-9]$\)" a.txt

#Query the content of the word you in the file a.txt
grep "<you>" a.txt

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Example 2

#创建并编辑test文件
vim test

#文件内容
• aaabbcaaa
• aa bbc aaa
• bb bbc bbb
• asgodssgoodsssagodssgood
• asgodssgoodsssagoodssgod
• sdlkjflskdjf3slkdjfdksl
• slkdjf2lskdjfkldsjl

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
#查询含有a的行
grep "a" test

#a Line where a appears 3 times
grep "a{3}" test

#Line starting with aaa
grep "<aaa" test

#The word line aaa appears, and if aaa represents a word, there needs to be a space before and after it, that is, space aaa space
grep "<aaa>" test

#The line where b appears
grep "b" test

# Lines where b appears 2-3 times
grep “b{2,3}” test

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Text processing commands

cut command (text segmentation)

cut:显示切割的行数据
d:自定义分隔符
s:不显示没有分隔符的行
f:选择显示的列

 
 
  
  
  • 1
  • 2
  • 3
  • 4

Display row data with column numbers 1-3 separated by spaces.
Insert image description here
Insert image description here

Display row data with space delimiters and columns 1-3, optimization of the above command
Insert image description here

Separated by colon, get the row data of the first column after splitting
Insert image description here

Sort by first letter of each row
Insert image description here

sort command (text sorting)

sort:排序文件的行
	n:按数值排序
	r:倒序
	t:自定义分隔符
	k:选择排序列
	u:合并相同行
	f:忽略大小写

 
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Sort the second column of data separated by the space delimiter
(that is, sort the following numbers, but instead of comparing the numerical values , just compare the first number, if it is the same, then compare the next number...and so on )

Insert image description here
Sort the data in the second column separated by the space delimiter (sort the numerical value, optimization of the previous step)
Insert image description here
Sort the data in the second column separated by the space delimiter (sort the numerical value in reverse order)
Insert image description here

wc command: statistics

word count: includes the number of lines, words, and bytes (including spaces)

Insert image description here

Learn command skills: man 具体命令Query how to use the command through

eg: man wc

Insert image description here

Remove filename information via pipe
Insert image description here

line editor

sed command

# 查看用法
man sed

 
 
  
  
  • 1
  • 2

Insert image description here

Chinese explanation (you can turn on the Chinese explanation of the man command)

sed [options] 'AddressCommand' file ...
	-n: 静默模式,不再默认显示模式空间中的内容
	-i: 直接修改原文件
	-e SCRIPT -e SCRIPT:可以同时执行多个脚本
	-f /PATH/TO/SED_SCRIPT
	-r: 表示使用扩展正则表达式

d: Delete the lines that meet the conditions;
p: Display the lines that meet the conditions;
a \string: Append a new line after the specified line, the content is string
\n: Can be used to wrap
i \string: Add a new line before the specified line line, the content is string
r FILE: Add the content of the specified file to the line that meets the conditions
w FILE: Save the lines in the range specified by the address to the specified file ;
s/pattern/string/modifier: Find and Replacement, by default only replaces the first string matched by the pattern in each line *
g: In-line global replacement
i: Ignore character case
s///: s ###, s@@@
<span class="token punctuation">(<span class="token punctuation">), \1, \2

sed: Line editor Address
can be found without
a given range
to find the specified line /str/

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

Copy the second row of data.
Insert image description here
Copy the second row of data. Only the copied content will be displayed. The content in the pattern space will no longer be displayed by default.
Insert image description here

Delete the third line of data. It should be noted that now only the preview after execution is displayed
1. If you want this command to take effect, you need to add -i (equivalent to commit in the database)
2. "3d" is equivalent to 3line delete
3. In When developing, we need to use it flexibly. Don't use -i (operation submission) first, but preview it first and then submit it.

Insert image description here


Preview adds new line a: after after line 2
Insert image description here


Execute and insert a line of characters sxt -i before line 2
: Execute command i\string: Preview the line before specifying the line
Insert image description here
. Edit and replace the string "sxt" with "hello".

Insert image description here

Match IP addresses through regular expressions and replace the last host number

#预览
sed "s/\(IPADDR=\(\([0-9]\|[1-9][0-9]\|1[0-9][0-9]\|2[0-4][0-9]\|25[0-5]\)\.\)\{3\}\).*/\1144/" ifcfg-eth0
#提交
sed "s/\(IPADDR=\(\([0-9]\|[1-9][0-9]\|1[0-9][0-9]\|2[0-4][0-4]\|25[0-5]\)\.\)\{3\}\).*/\1$num/"  -i ifcfg-eth0 

 
 
  
  
  • 1
  • 2
  • 3
  • 4

Insert image description here

awk command (text analysis tool command)

awk is a powerful text analysis tool.
Compared with grep's search and sed's editing, awk is particularly powerful when it analyzes data and generates reports.
To put it simply, awk reads the file line by line, slices each line into slices (space, tab) as the default delimiter, and then performs various analysis and processing on the cut parts.

awk -F '{pattern + action}' {
    
    filenames}
支持自定义分隔符(-F"自定义分隔符")
支持正则表达式匹配
支持自定义变量,数组  a[1]  a[tom]  map(key)
支持内置变量
ARGC               命令行参数个数
ARGV               命令行参数排列
ENVIRON            支持队列中系统环境变量的使用
FILENAME           awk浏览的文件名
FNR                浏览文件的记录数
FS                 设置输入域分隔符,等价于命令行 -F选项
NF                 浏览记录的域的个数*
NR                 已读的记录数*
OFS                输出域分隔符
ORS                输出记录分隔符
RS                 控制记录分隔符

Support functions
print, split, substr, sub, gsub

Support flow control statements, C-like language
if, while, do/while, for, break, continue

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Call the print function to output the data in the first column separated by the delimiter
F. Specify the delimiter.

只是显示/etc/passwd的账户:CUT
awk -F':' '{print $1}' passwd

It only displays the account of /etc/passwd and the shell corresponding to the account, and the account and shell are separated by commas, and the column name name, shell is added before the beginning of all lines, and "blue,/bin/nosh" is added to the last line ( cut, sed)
awk -F ':' 'BEGIN{print "name,shell"} {print $1 "," $7 } END{print "blue,/bin/nosh"}' passwd

Search /etc/passwd for all lines with the root keyword
awk '/root/ { print $0 }' passwd

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Insert image description here
Equivalent to the following cut command
Insert image description here

Statistical report: Total monthly salary per person, 0: manager, 1: worker

man awk

/file #Find the next line, you can see that awk not only supports commands, but also supports script files

q #Exit introduction

  • 1
  • 2
  • 3
  • 4
  • 5

Insert image description here
Idea: first write a script and run the report file through the script

# 报表内容
Tom	 	 0   2012-12-11      car     3000
John	 1   2013-01-13      bike    1000
vivi	 1   2013-01-18      car     2800
Tom	     0   2013-01-20      car     2500
John	 1   2013-01-28      bike    3500

 
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Script

Call split to split the data in column 3 and split based on "-".
If the month is equal to 01,
use the first column (name) as the array subscript
name[$1]+=$5. Sum the values ​​of the same array subscript ancestors
END method Traverse the name array and output the result

{
    
    
split($3,date,"-");
if(date[2]=="01"){
    
    
name[$1]+=$5
}
}
END{
    
    
for(i in name){
    
    
print i "\t" name[i]
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Query report files through script text

# -f 脚本文件名
awk -f awk.sh awk.txt 

 
 
  
  
  • 1
  • 2

Insert image description here

Users and permissions

Basic usage

id				打印输出有效用户的自身的ID 和 所在组的 ID
useradd			添加用户
userdel			删除用户
groupadd		添加组
groupdel		删除组
passwd			设置或修改密码
sudo 			提升权限
su				切换用户

 
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

ReadMe:

  1. After adding a user, a mailbox file will be automatically added to the user.
    If you delete a file, you not only need to /homedelete the user under , but also /var/spool/mail/delete the mailbox file with the same name in .
  2. Change the password. The root user passwdcan change the password of the root user; passwd 用户名change the password of the specified user; ordinary users passwdcan only change their own password.
  3. ssh 用户名@用户所在ipAfter creating the role and password, you can log in through xshell
    . Moreover, these created ordinary users can only perform simple file viewing functions. The operation of user groups is introduced here, and the distribution of permissions is completed through user groups

User group operations

Add user group and view each user ID

groupadd 新建用户组名

 
 
  
  
  • 1

Insert image description here

Add two ordinary users to the user group share, and check the user ID again to confirm whether the assignment is successful.

usermod -G 用户组名

 
 
  
  
  • 1

Insert image description here

Change permissions and assign specified groups to files

#chown 文件当前所属原用户:当前所属用户组 文件名 
chown  root:share share/

# Add the read permissions of the user group to the specified file
# The first group of rwx represents the file owner permissions, the second group of rx represents the user group permissions, and the third group of rx represents other user permissions. # Permission classification : r : Read permission w: Write
permission Change the owner of a file chown -R chy share/



# Withdraw read, write and execute permissions from other users
# Note that after modifying the permissions here, you need to log in to the user group again to take effect
chmod or –wx share/

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Knowledge of linux role permissions

1. The function of the parameters in each column in the figure below:
file permissions, number of connections, file owner, user group, file size, modification date, file name
2. The meaning of the first column parameters:
the first group of rwx represents the file owner permissions, and the second group of rx represents the user group permissions. , the third group rx represents other user permissions.
3. Permission classification:
r: Read permission w: Write permission x: Execute permission (Read permission is also required to open a folder)
4. Role classification
a: All roles u: File owner g: User group o: Others

Learn more about related For knowledge please go to
Insert image description here

Permission operation

Order effect
r Read permission r=4
w Write permission w=2
x Executable permission x=1
usermod Modify user permissions
chown Modify the owner and group of a file
chmod Modify file operation permissions (RWX) augo

Note that
there are two ways to express permissions, one is alphabetical: r: read permission w: write permission x: execute permission (read permission is also required to open a folder) and the other is
numerical: r4w2x1, we can use numbers at the same time Grant permissions to the file owner_user group_other users
, but all modifications made are in vain for the root user (root cowhide!!!)

For example:

# 赋予所有用户读权限
chmod 444 filename/ chmod a+x filename
#赋予所有用户所有权限
chmod 777 filename/ chmod a+wrx filename

 
 
  
  
  • 1
  • 2
  • 3
  • 4

Linux software installation

Compile and install

Mainly understand the difference between ./confirm, make, make install

# 1. 检查操作系统 ,检查编译环境
yum install gcc
# pcre 依赖库
yum search pcre
yum install pcre-devel
# openssl
yum install opssl-devel  

# 2. Upload and decompress
tar -zxf nginx-1.8.0.tar.gz

# 3. Generate compilation dependencies in the decompression
directory./configure

# 4. Compile and install
make & make install

#5. 进入nginx的命令管理目录,启动
cd /user/sxt/nginx/sbin
./nginx

# 7.浏览器访问测试
输入虚拟机地址即可

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

rpm安装

Redhat提供了rpm管理体系
已经编译的软件包:针对不同的平台系统编译目标软件包
操作系统维护安装信息 ,软件包包含依赖检查,但还需人为解决

# rpm安装:
-ivh  filename
--prefix
# rpm升级:
-Uvh
-Fvh
rpm卸载:
-e PACKAGE_NAME

# rpm查询 ,配合管道一起使用 ,例如 rpn -qa |grep jdk
rpm -qa : 查询已经安装的所有包

rpm -q PACKAGE_NAME: 查询指定的包是否已经安装
rpm -qi PACKAGE_NAME: 查询指定包的说明信息
rpm -ql PACKAGE_NAME: 查询指定包安装后生成的文件列表
rpm -qc PACEAGE_NEME:查询指定包安装的配置文件
rpm -qd PACKAGE_NAME: 查询指定包安装的帮助文件
rpm -q scripts PACKAGE_NAME: 查询指定包中包含的脚本

查询文件是由哪个rpm包安装生成的(type(查看命令所在目录)命令一起使用)

rpm -qf /path/to/somefile

如果某rpm包尚未安装,需查询其说明信息、安装以后会生成的文件

rpm -qpi /PATH/TO/PACKAGE_FILE
rpm -qpl

卸载安装好的软件 ,例如jdk

[root@node1 local]# rpm -qa | grep jdk
jdk-1.7.0_67-fcs.x86_64
[root@node1 local]# yum -y remove jdk-1.7.0_67-fcs.x86_64

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

小技巧 : 见到别人的命令很好用,自己如何安装

例如 ifconfig命令

1.查看该命令所在文件夹 type ifconfig
ifconfig is /sbin/ifconfig

2.查询该目录下的 jar包 rpm -qf /sbin/ifconfig
net-tools-1.60-110.el6_2.x86_64

3.搜索 yum search net-tools 不要加版本
================================ N/S Matched: net-tools =================================
net-tools.x86_64 : Basic networking tools

4.安装 yum install net-tools
5.使用

yum安装

基本命令

# yum命令:
yum repolist
yum clean all
yum makecache
yum update

# 查询:
yum list
yum search
yum info

# 安装&卸载:
yum install
remove|erase

# yum命令:分组
yum grouplist
yum groupinfo
yum groupinstall
yum groupremove
yum groupupdate

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

阿里云CentOS的yum源

# 1.进入镜像仓库
cd /etc/yum.repos.d/

# 2.创建用于备份的文件夹(更换源失败可以从备份文件获取)
mkdir backup

# 3.将镜像文件移动到备份文件中
mv CentOS-* backup/

# 4.下载wget命令支持包
yum install -y wget

# 5. 下载阿里镜像,下载以后 yum.repos.d文件下回多出一个文件CentOS-Base.repo(图1)
# 阿里云镜像站地址 https://opsx.alibaba.com/mirror
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
# 我们可以查看这个文件 ,这个文件中存放了许多安装地址
cat CentOS-Base.repo

# 6.清除本地缓存的jar包依赖关系(图2)
yum clean all

# 7.重新添加阿里的依赖关系缓存(图3)
yum makecache

如果安装出现如下错误http://mirrors.aliyuncs.com/centos/6/updates/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 56 - “Failure when receiving data from the peer”
需要根据repo文件变量的变化,可以修改repo文件把所有KaTeX parse error: Expected 'EOF', got '#' at position 52: …token comment">#̲ 执行替换操作</span> …releasever/6/’ CentOS-Base.repo

# 清理并重新生成yum缓存
yum clean all

# 重新yum makecache, 出现图4说明安装成功
yum makecache

#如果还出现错误 ,运行如下sed语句 ,清空缓存 ,并重新生成缓存
sed -i ‘/mirrorlist/d’ CentOS-Base.repo
sed -i ‘s/<span class=“token variable”>KaTeX parse error: Can't use function '\]' in math mode at position 129: …ng">'/\ [addons\̲]̲/,/^/d’ CentOS-Base.repo
sed -i ‘s/RPM-GPG-KEY-CentOS-5/RPM-GPG-KEY-CentOS-6/’ CentOS-Base.repo

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

图1
Insert image description here
图2
Insert image description here
图3
Insert image description here

本地 yum 源

1.为当前虚拟机挂载镜像文件

Insert image description here

# 2.挂载 cdrom (mount 被挂载文件 挂载文件)
mount /dev/cdrom /mnt

# 3.备份CentOS-Base.repo文件
cd /etc/yum.repos.d/
cp CentOS-Base.repo CentOS-Base.repo.backupAliYuan

# 4.修改CentOS-Base.repo,文件只保留下面内容(图1)
#[base]
name=local
failovermethod=priority
baseurl=file:///mnt
#gpgchkeck= 有1和0两个选择,分别代表是否是否进行gpg校验,如果没有这一项,默认是检查的。
gpgcheck=1
#当某个软件仓库被配置成 enabled=0 时,yum 在安装或升级软件包时不会将该仓库做为软件包提供源。使用这个选项,可以启用或禁用软件仓库。
enable=1

# 5. 重新安装依赖缓存(图2)
yum makecache

# 6. 安装想使用的软件,例如 mysql(无需联网)
yum install mysql-server

重启后, 在使用本地源时,建议查看镜像文件是否被挂载(图3)
df -h

如果没有被挂载 ,重新挂载一下( 取消挂载命令是umount )
mount /dev/cdrom /mnt

# 如果下载不上,建议首先查看挂载设置连接状态图4 ,然后重新挂载一下,如果实在没有就更换为阿里数据源
步骤:
取消本地挂载,
删除本地CentOS-Base.repo ,
放开/重新下载阿里的CentOS-Base.repo,
清除并重新下载依赖关系

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

图1
Insert image description here
图2
Insert image description here
图3
Insert image description here
图4
Insert image description here

中文显示,查看帮助中文文档

中文显示
# yum 的 repo 变成aliyun  || 本地DVD

# 1. 如果使用本地源, 查看列表是否含有中文包
yum grouplist | grep Chinese Support

# 2.如果存在直接下载(使用阿里源的直接执行这一步)
# 注意: 存在空格的必须使用 " "引上
yum groupinstall “Chinese Support”

#3 .查询当前语言, 如果显示的是en_US.UTF-8
echo $LANG

# 4. 更换语言为中文,可以通过ll查看是否配置成功(下图)
LANG=zh_CN.UTF-8

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Insert image description here

中文帮助文档设置

前提 :执行上面的中文显示的支持

# 1. 访问阿里镜像站, 下载epel源 ,网站地址 https://opsx.alibaba.com/mirror?lang=zh-CN
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

# 2. 下载中文支持下的依赖
yum repolist

# 3. 依次执行如下命令
yum clean all
yum makecache
yum search man-pages
yum install man man-pages man-pages-zh-CN

# 4. 测试帮助命令是否变为中文(如下图)
man bash

# 5. 注意 :每次重启后 ,如果中文显示不出来了, 需要执行如下命令 ,
# 因为LANG命令是对每一个bash而言的(一个窗口对应一个bash)
LANG=zh_CN.UTF-8

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Insert image description here
注意 : 即使安装了中文包, 也不是每个命令都会被翻译成中文

文本流和重定向

# 文件描述符fd
cd /proc/$$/fd  +tab键 ( $$就会转换成进程当前shell的id)

# 查看文件描述符fd目录(图1)
ll
0 标准输入
1 标准输出
2 错误输出

# 利用管道与进程id查看当前进程新消息, 再由当前进程寻找他的父进程(图2)
# 也就是说我们每次发起请求, 都会在服务端开启一个虚拟终端
ps -ef | grep 6564
ps -ef | grep 6560

# 我们可以进入父进程所在目录查看虚拟终端(图3)
# 如果创建一个窗口执行第一个操作 ,虚拟终端id就会从0加1, 一次类推
# 在Linux中. 一切皆文件!!!
# 所有命令, 功能都可以映射成文件 ,都有012这种输入输出方式
cd dev pts

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

图1
Insert image description here
图2
Insert image description here
图3
Insert image description here

重定向

输出重定向

#输入重定向
# 见图1
1>xxx	标准输出覆盖重定向
1>>xxx	标准输出追加重定向

见图2

2>xxx 错误覆盖重定向
2>>xxx 错误追加重定向

在一个文件中如果同时输出标准输出内容和错误内容

1.使用普通重定向 ,会发现不会出现错误音效(图3) ,原因是在执行ls时, 首先会扫描文件是否存在 ,然后输出文件信息将原来的错误信息

2.因此, 如果使用追加重定向时 ,无论顺序如何 ,都会首先打印错误信息(图4)

3. 文件描述符+">&"+重定向符号详细形式

注意: 左边文件描述符(1,2)与重定向符号之间是不能有空格的 ,但是右边可以, 所以建议都不要留空格

ls / /error 2>&1 1>&eee
ls / /error 1>&fff 2>&1

4.特例(简写形式) :>&或者&> xxx 向 xxx 文件添加错误和标准输出信息(图5)

ls / /error >& ggg
ls / /error &> hhh

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

图1
Insert image description here
图2
Insert image description here

图3
Insert image description here
图4
Insert image description here
图5
Insert image description here

图6
Insert image description here
输入重定向

# 输出重定向
#语法
<<<	从字符串中读取输入
<<E	从键盘中读取输入,E表示结束符
<	从文件中读取输入

[root@node01 input]# read aaa <<<“HelloWord”
[root@node01 input]# echo $aaa
HelloWord
[root@node01 input]# read aaa<<GG
> ljasf
> ajlsdfj;la
> ;ajdfkl
> GG #跟 GG 一样,表示输入退出

# 输出aaa第一行数据
[root@node01 input]# echo $aaa
ljasf

# input.sh 脚本
cat <<AABB
print error info
AABB

echo “you know me …”

# 执行
source input.sh

[root@node01 input]# cat 0< /etc/in
init/ init.d/ inittab inputrc
[root@node01 input]# cat 0< /etc/inittab

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

全重定向

# 全重定向:socket案例
# 创建文件描述符8
exec 8<> /dev/tcp/www.baidu.com/80

# 发送请求, 将信息写入8中
echo -e “GET / HTTP/1.0\n” >& 8

# 输出8的信息(图下图)
cat <& 8

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Insert image description here

shell 脚本编程

Bash

linux的发布版本之一——Redhat/CentOS——系统默认安装的shell叫做bash,即Bourne Again Shell,它是sh(Bourne Shell)的增强版本。Bourn Shell 是最早行起来的一个shell,创始人叫Steven Bourne,为了纪念他所以叫做Bourn Shell,检称sh。

含有shell命令的文件三种运行方式

第一种 source 文件名(图2)
第二种 . 文件名 (图3)
第三种 赋予该文件执行权限 chmod -R 777 文件名 , 执行该文件 ./文件名 (图4)

注意:
一定要写成 ./test.sh,而不是 test.sh,运行其它二进制的程序也一样,
直接写 test.sh,linux 系统会去 PATH 里寻找有没有叫 test.sh 的,而只有 /bin, /sbin, /usr/bin,/usr/sbin 等在 PATH 里,
你的当前目录通常不在 PATH 里,所以写成 test.sh 是会找不到命令的,要用 ./test.sh 告诉系统说,就在当前目录找。
想要具体了解shell编程可通过这里进行学习

图1 :含有脚本的文件 sh01
Insert image description here
图2
Insert image description here

图3
Insert image description here
图4
Insert image description here

变量

# 语法
# 本地:(图1)
	当前shell拥有
	生命周期随shell
# 局部:
	只能local用于函数
# 位置:(图2)
	$1,$2,${11} 从脚本文件后,读取参数
# 特殊
	$#: 位置参数个数(图3,图4,图5)
	$*: 参数列表,双引号引用为一个字符串
	$@: 参数列表,双引号引用为单独的字符串
$$:当前 shell 的 PID:接收者<span class="token punctuation">(</span>图6<span class="token punctuation">)</span>
<span class="token variable">$BASHPID</span>:真实
管道 *
<span class="token variable">$?</span>:上一个命令退出状态
- 0 成功
-other: 失败

# 变量
export 定义环境变量: 不管开启了多个字bash,都会引用系统变量
导出到子 shell
fork() Copy On Write 时间复杂度 O(1)
适用用于函数
unset: 取消变量
set: 查看shell 的变量

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

图1
Insert image description here

# 创建shell文件
vim test.sh
# 编辑内容
echo $1
echo $2
# 发现a,b自动被shell文件接收并输出(图2)
source test.sh a b

 
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

图2
Insert image description here

图3
Insert image description here
Insert image description here

图4
在管道里创建子bash时 , 它会继承父bash
Insert image description here
图5
但是在文件中执行子bash时, 他不会继承父bash的值, 只有export ,他才会继承他的值
而且 ,不能跨bash
Insert image description here
Insert image description here
图6
Insert image description here

引用

双引号:弱引用,参数扩展
单引号:强引用,不可嵌套
花括号扩展不能被引用
命令执行前删除引用

[root@node01 sh]# kk=12
[root@node01 sh]# echo "$kk"
12
[root@node01 sh]# echo '$kk'
$kk
# 花括号扩展不能被引用
[root@node01 b]# cp  "/etc/{passwd,inittab}" ./
cp: cannot stat `/etc/{
    
    passwd,inittab}': No such file or directory

 
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

命令替换

# 基本语法
反引号:`ls -l /`
$(ls -l /)
可以嵌套

[root@node01 sh]# echo “echo 123
123

[root@node01 sh]# abc=$(echo $(echo “sxt”))
[root@node01 sh]# echo $abc
sxt

[root@node01 sh]# abc=$(ls -l /)
[root@node01 sh]# echo $abc
total 98 drwxrw-r-x. 3 root root 4096 Sep 22 13:53 abc dr-xr-xr-x. 2 root root 4096 Sep 22 21:34 bin dr-xr-xr-x. 5 root root 1024 Sep 21 19:29 boot drwxr-xr-x. 18 root root 3680 Sep 22 21:45 dev drwxr-xr-x. 76 root root 4096 Sep 22 21:45 etc drwxr-xr-x. 4 root root 4096 Sep 22 13:04 home dr-xr-xr-x. 8 root root 4096 Sep 22 14:16 lib dr-xr-xr-x. 10 root root 12288 Sep 22 21:34 lib64 drwx------. 2 root root 16384 Sep 21 19:28 lost+found drwxr-xr-x. 2 root root 4096 Sep 23 2011 media drwxr-xr-x. 2 root root 4096 Sep 23 2011 mnt drwxr-xr-x. 2 root root 4096 Sep 23 2011 opt dr-xr-xr-x. 87 root root 0 Sep 22 21:45 proc dr-xr-x—. 5 root root 4096 Sep 23 03:26 root dr-xr-xr-x. 2 root root 12288 Sep 22 17:58 sbin drwxr-xr-x. 7 root root 0 Sep 22 21:45 selinux drwxrwx—. 2 root share 4096 Sep 22 13:51 share drwxr-xr-x. 2 root root 4096 Sep 23 2011 srv drwxr-xr-x. 13 root root 0 Sep 22 21:45 sys drwxrwxrwt. 3 root root 4096 Sep 23 02:02 tmp drwxr-xr-x. 14 root root 4096 Sep 22 17:50 usr drwxr-xr-x. 17 root root 4096 Sep 21 19:28 var

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

表达式

# 算术表达式(图1,图2)
let  算术运算表达式
let  C=$A+$B
$[算术表达式]
C =$[$A+$B]
$((算术表达式))
C=$(($A+$B))
expr  算术表达式  
注意:表达式中各操作数及运算符之间要有空格。而且要使用命令引用
C=`expr $A + $B`
help let

# 条件表达式(图3)
[ expression ]
test expression
[[ expression ]]
help test

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

图1
Insert image description here
图2
Insert image description here
图3

Insert image description here

编写shell 脚本案例——添加用户脚本

明确需求

添加用户
用户密码同用户名
静默运行脚本
避免捕获用户接口
程序自定义输出

脚本内容

第二行判断参数,
第三行判断用户是否存在,
第四行添加用户密码并解决数据回显,
第五行解决权限不足问题

#! /bin/bash
[ ! $# -eq 1 ] && echo "args error!" && exit 2
id $1 >& /dev/null && echo "user exist!" && exit 3
useradd $1 >& /dev/null $$  echo $1 | passwd --stdin $1 >& /dev/null && echo "add user successful!!!" && exit 4
echo "user add fail for no why" && exit 5

 
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

总结 :
脚本使用的意义就是实现项目启动自动化 ,在大型分布式项目部署的时候 ,可能有几十甚至上百个服务需要启动 ,
这个时候, 脚本自动化实现就显得尤为重要了

逻辑判断

# 判断命令是否被执行? 返回值为0代表执行 ,非0代表执行失败
# 在Linux中的条件判断中 ,返回0代表是true ,非0代表false ,区别java
echo $? 

# 逻辑判断
command1 && command2
command1 || command2

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

流程控制

if选择语句

man if
NAME
       if - 有条件的执行脚本

总览 SYNOPSIS
if expr1 ?then? body1 elseif expr2 ?then? body2 elseif . ?else? ?bodyN?


描述 DESCRIPTION
if 命令把 expr1 作为一个表达式来求值(用与 expr 求值它的参数相同的方 式)
。 这个表达式的值必须式一个 boolean 值(一个数值值,这里 0 是假而任何其他
数值都是真;或者是一个字符串值,比如 trueyes 是真而 false 或 no 是
) ;如果它是真通过把 body1 传递给 Tcl 解释器来执行它。否则把 expr2 作
为一个表达式来求值并且如果它是真则执行 body2,以此类推。如果没有表达式 被
求值为真则执行 bodyN 。then 和 else 是可选的“噪音词”用来使命令易读。可
以有任意数目的 elseif 子句,包括零个。BodyN 与 else 可同时省略。命 令
的 返 回 值是被执行的那个脚本的返回值,如果没有表达式是非零并且没有 bodyN
则返回空串。

参见 SEE ALSO
expr(n), for(n), foreach(n)

关键字 KEYWORDS
boolean, conditional, else, false, if, true

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

Insert image description here

while循环语句

man while
NAME
       while - 在条件满足时重复的执行脚本

总览
while test body

描述
while 命令把 test 作为一个表达式来求值(用与 expr 求它的参数的值相同的方
)。这个表达式的值必须是一个正确的 boolean 值;如果它是真值则把 body 传
递给 Tcl 解释器来执行它。一旦执行了 body 则再次求值 test ,并重复处理直到
最终 test 求值出一个假 boolean 值。可以在 body 中执行 Continue 命令来终止
这 个循环的当前重复操作(iteration),并可以在 body 中执行 break 命令来导致
while 命令立即终止。while 命令总是返回一个空串。

   注意: <span class="token function">test</span> 应该总是包围在花括号中。如果不是,在 <span class="token keyword">while</span> 命令开始执行之前 将
   进 行变量替换,这意味着循环体所做的变量变更将不考虑在这个表达式中。这将很
   可能导致无限循环。如果 <span class="token function">test</span> 被包围在花括号中,变量替换被推迟到求值这个 表
   达 式的时候<span class="token punctuation">(</span>在每次循环重复操作之前<span class="token punctuation">)</span>,所以变量的变化将是可见的。例如,尝试
   下列脚本并在 <span class="token variable">$x</span><span class="token operator">&lt;</span>10 两边分别加上和不加花括号:

   <span class="token keyword">set</span> x 0
   <span class="token keyword">while</span> <span class="token punctuation">{</span><span class="token variable">$x</span><span class="token operator">&lt;</span>10<span class="token punctuation">}</span> <span class="token punctuation">{</span>
        puts <span class="token string">"x is <span class="token variable">$x</span>"</span>
        incr x
   <span class="token punctuation">}</span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

Insert image description here

for循环语句

man for
________________________________________________________________________________

NAME
for - ‘‘For’’ 循环

总览 SYNOPSIS
for start test next body


描述 DESCRIPTION
For 是一个循环命令,在结构上类似与 C 语言的 for语句。start、next、和body
参数必须是 Tcl 命令串,而 test 是一个表达式串。for 命令首先调用 Tcl 解释
器来执行 start。接着它重复的把 test作为一个表达式来求值;如果结果是非零则
它在 body 上调用 Tcl 解释器,接着在 next 上调用 Tcl 解释器,接着重复这 个
循环。在 test 被求值为 0 的时候命令终止。如果body 中调用了 continue
令则在 body 的当前执行中的所有剩余的命令都被跳过;处理继续,在 next 上调
用 Tcl 解释器,接着对 test 求值。 等等. 如果在 body 或 next 中调用 break
命令,则 for 命令将立即返回。break 和 continue 命令的操作类似于在 C 语
言中相应的语句。For 返回一个空串。

   注意: <span class="token function">test</span> 应当总是在花括号中包围着。如果不是这样,在 <span class="token keyword">for</span>  命令开始之前就
   作了变量替换,这意味着在循环体中做的变量变更在表达式中将不被考虑。将导 致
   一 个 无限循环。如果 <span class="token function">test</span>  被包围在花括号中,变量替换将延迟,直到表达式求
   值<span class="token punctuation">(</span>在每次循环重复之前<span class="token punctuation">)</span>,所以变量的变更将是可见的。例如,尝试在 <span class="token variable">$x</span><span class="token operator">&lt;</span>10 周围
   有和没有一对花括号的下列脚本:
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

Insert image description here

反引号就是esc下面的那个按键!!!
Insert image description here

编写shell 脚本案例——判断目录下最大文件

思路

用户给定路径
输出文件大小最大的文件
递归子目录

脚本内容

#! /bin/bash
oldIFS=$IFS
IFS=$'\n'
for i in `du -a $1 | sort -nr `;
do filename=`echo $i | awk '{print $2}'`;
   if [ -f $filename ];
   then echo $filename break;fi
done
IFS=$oldIFS

 
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

运行测试
Insert image description here

编写shell 脚本案例——递归子目录

被遍历的文件file.txt

aaa 111
bbb 222
ccc 333

 
 
  
  
  • 1
  • 2
  • 3
#! /bin/bash
oldIFS=$IFS
IFS=$'\n'
echo "--------加强for循环---------"
num=0;
        for i in `cat file.txt`;
        do echo $i;
        ((num++))
done
echo "line:$num"
IFS=$oldIFS

echo “--------普通for循环----------”
line=0;
lines=</span><span class="token function">cat</span> file.txt <span class="token operator">|</span> <span class="token function">wc</span> -l<span class="token variable">
for ((i=1;i<=lines;i++));
do echo </span><span class="token function">head</span> -$i file.txt <span class="token operator">|</span> <span class="token function">tail</span> -1<span class="token variable">
((line++))
done
echo “line:$line

echo “--------while循环-------------”
num=0
while read line;
do echo KaTeX parse error: Expected 'EOF', got '&' at position 242: …oken operator">&̲lt;</span>file.…num"

echo “---------使用管道的两个命令都位于子bash,其他位于父bash----------”
num=0
cat file.txt | while read line;
do echo l i n e < / s p a n > < s p a n c l a s s = " t o k e n v a r i a b l e " > < s p a n c l a s s = " t o k e n p u n c t u a t i o n " > ( ( < / s p a n > n u m < s p a n c l a s s = " t o k e n o p e r a t o r " > + + < / s p a n > < s p a n c l a s s = " t o k e n p u n c t u a t i o n " > ) ) < / s p a n > < / s p a n > < s p a n c l a s s = " t o k e n k e y w o r d " > d o n e < / s p a n > < s p a n c l a s s = " t o k e n k e y w o r d " > e c h o < / s p a n > < s p a n c l a s s = " t o k e n s t r i n g " > " n u m : < s p a n c l a s s = " t o k e n v a r i a b l e " > line</span> <span class="token variable"><span class="token punctuation">((</span>num<span class="token operator">++</span><span class="token punctuation">))</span></span> <span class="token keyword">done</span> <span class="token keyword">echo</span> <span class="token string">"num:<span class="token variable"> line</span><spanclass="tokenvariable"><spanclass="tokenpunctuation">((</span>num<spanclass="tokenoperator">++</span><spanclass="tokenpunctuation">))</span></span><spanclass="tokenkeyword">done</span><spanclass="tokenkeyword">echo</span><spanclass="tokenstring">"num:<spanclass="tokenvariable">num"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

Run tests
Insert image description here

Note: Remember to like this good article! ! !

Guess you like

Origin blog.csdn.net/qq_42264264/article/details/105772489