Linux课程第十三天学习笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux课程第十三天学习笔记相关的知识,希望对你有一定的参考价值。
#########################
####### 6.shell脚本命令 #######
#########################
####################1.diff####################
diff 参数 file1 file2 ##比较两个文件的不同
-c file1 file2 ##显示周围的行
-u file1 file2 ##按照统一输出格式生成补丁
-r westos1 westos2 ##比较两个目录中文件的不同
patch 参数 file file.path ##打补丁
-b file file.path ##备份原文件
####################2.grep####################
grep 参数 关键字 文件|目录 ##在文件或目录中查找含有关键字的行
-i ##忽略大小写
-n ##显示关键字所在行
-c ##显示过滤结果的行的计数
-v ##反向过滤
-E "关键字1|关键字2" ##过滤多个关键字
-r 目录 ##在目录中查找含有关键字的文件
注意: ^关键字 ##以关键字开头
关键字$ ##以关键字结尾
grep的用法还有很多,可以百度“grep 正则表达式”
####################3.cut####################
cut ##截取字符
cut -d 分隔符 ##指定分隔符
cut -f 1,7 ##显示指定的列
cut -c 1-4 ##显示指定的字符
####################4.sort####################
sort 参数 文件
-n ##纯数字排序
-u ##去除冗余
|uniq -c ##去除冗余并统计冗余次数
-t ##指定分隔符(默认为空格)
-k ##指定列
####################5.uniq####################
sort file |uniq -d ##显示冗余行
-u ##显示唯一行
-c ##去除冗余并统计冗余次数
####################6.tr####################
tr ##字符转换
####################
[[email protected] mnt]# vim file
--------------------------------------------------
hello world
HELLO WORLD
--------------------------------------------------
[[email protected] mnt]# tr ‘a-z‘ ‘A-Z‘ < file ##等同于"cat file | tr ‘a-z‘ ‘A-Z‘"
HELLO WORLD
HELLO WORLD
[[email protected] mnt]# tr ‘A-Z‘ ‘a-z‘ < file
hello world
hello world
[[email protected] mnt]# tr -d ‘o,O‘ < file
hell wrld
HELL WRLD
[[email protected] mnt]# echo "thissss is a text linnnnnnne." | tr -s ‘s,n‘
this is a text line.
####################
####################7.sed####################
sed ‘s/原字符/替换字符/g‘ file
sed -e ‘策略1‘ -e ‘策略2‘ file
sed -i file ##直接修改读取文件的内容,而不是输出到终端
sed ‘3,5s/原字符/替换字符/g‘ ##3-5行替换
sed xd ##屏蔽指定行
sed xp ##复制指定行
sed -n xp ##只显示指定行
####################8.awk####################
[[email protected] mnt]# ifconfig eth0 | grep netmask | awk -F ‘ ‘ ‘{print $0}‘
inet 172.25.50.100 netmask 255.255.255.0 broadcast 172.25.50.255
[[email protected] mnt]# ifconfig eth0 | grep netmask | awk ‘{print $0}‘
inet 172.25.50.100 netmask 255.255.255.0 broadcast 172.25.50.255
[[email protected] mnt]# ifconfig eth0 | grep netmask | awk ‘{print $2}‘
172.25.50.100
"-F"指定域分隔符,默认域分隔符是"空格键" 或 "[tab]键"
$0则表示所有域,$1表示第一个域,$n表示第n个域
####################课堂练习####################
[[email protected] mnt]# for i in {1..5};do echo $i;done
1
2
3
4
5
[[email protected] mnt]# for i in $(seq 1 5);do echo $i;done
1
2
3
4
5
[[email protected] mnt]# for i in $(seq 1 2 5);do echo $i;done ##从1开始,每隔2位进行输出
1
3
5
[[email protected] mnt]# for i in $(seq 2 2 5);do echo $i;done ##从2开始,每隔2位进行输出
2
4
参考以上命令,制作一个脚本:
提供的信息如下:
[[email protected] mnt]# ls
passwdfile userfile
[[email protected] mnt]# cat userfile
user1
user2
user3
user4
user5
[[email protected] mnt]# cat passwdfile
westos1
westos2
westos3
westos4
westos5
脚本的功能如下:
把userfile文件里的内容创建为用户,用户密码是对应的passwdfile文件里的内容
以上userfile和passwdfile里的内容都只有5行,但是不管多少行,只要行数一致
都可以通过脚本创建用户和密码,不需要指定数量
标准脚本:
vim create_user.sh
--------------------------------------------------
#!/bin/bash
MAX=$( wc -l $1 | cut -d " " -f 1 )
for NUM in $( seq $MAX )
do
USERNAME=$( sed -n ${NUM}p $1 )
PASSWD=$( sed -n ${NUM}p $2 )
useradd $USERNAME
echo $PASSWD | passwd --stdin $USERNAME
done
--------------------------------------------------
sh create_user.sh userfile passwdfile
命令方式完成:
for i in $(seq 1 `wc -l userfile | cut -d " " -f 1`);do useradd `sed -n "$i"p userfile`;done
for i in $(seq 1 `wc -l userfile | cut -d " " -f 1`);do echo `sed -n "$i"p passwdfile` | passwd --stdin `sed -n "$i"p userfile`;done
[[email protected] mnt]# man passwd
------------------------------------------------------------
--stdin
This option is used to indicate that passwd should read the new
password from standard input, which can be a pipe.
------------------------------------------------------------
###############################
####### 4.管理系统存储 #######
###############################
####################1.分区划分####################
[[email protected] ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x0d123a0c.
Command (m for help): m ##帮助
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition ##删除分区
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types ##列出系统可用的分区类型
m print this menu
n add a new partition ##新建分区
o create a new empty DOS partition table
p print the partition table ##显示分区
q quit without saving changes ##退出
s create a new empty Sun disklabel
t change a partition‘s system id ##修改分区功能id
u change display/entry units
v verify the partition table
w write table to disk and exit ##保存更改到分区表中
x extra functionality (experts only)
Command (m for help): n ##新建分区
Partition type:
p primary (0 primary, 0 extended, 4 free) ##分区类型为主分区
e extended ##分区类型为扩展分区
Select (default p): ##默认为主分区
Using default response p
Partition number (1-4, default 1): ##主分区id
First sector (2048-20971519, default 2048): ##此分区起始位置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M ##分区大小
Partition 1 of type Linux and of size 100 MiB is set
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0d123a0c
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
Command (m for help): wq ##保存退出,如果按q表示放弃更改退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# partprobe ##同步分区表
[[email protected] ~]# mkfs.xfs /dev/vdb1 ##格式化
meta-data=/dev/vdb1 isize=256 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[[email protected] ~]# mount /dev/vdb1 /mnt ##挂载
注意:"partprobe"并不是必须的,但是当使用"fdisk"命令划分磁盘出现如下报错的时候
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
必须使用"partprobe"命令同步分区表,否则系统无法识别
partprobe同步硬盘上的分区表到系统当中
fdisk -l ##系统真实存在的设备
blkid ##系统能够使用的设备
cat /proc/partitions ##系统能够识别的设备
mbr主引导记录,一个分区最大支持2T容量
mpt主分区表64个字节,64/16=4,最多划分4个主分区
gpt另外一种分区方式,支持大于2T的分区容量(百度百科:9.4ZB或18E个512字节)
主分区表只能记录四个主分区,如果还想划分更多的分区,就必须把其中的一个主分区重新划分为扩展分区。
扩展分区相当于一个容器,把剩余空间都放在这个容器里,再从容器里取出一部分来划分逻辑分区。
最多可以划分15个分区,超过15个分区以后系统不识别(cat /proc/partitions最多看多15个分区)
"fdisk /dev/vdb1"错误用法,分区里面不能再划分区
文件系统,就是系统里对文件进行管理的软件
fs是文件系统,包括:
vfat,ntfs(链式读取,没有inode id)
ext(日志文件系统,最大支持的分区大小:32T)
xfs(企业七用的这个;最大支持的分区容量:18eb,1eb=1024t;每秒吞吐速度8G,当然还得看硬盘速度能否跟的上)
=====查看mpt主分区表的16进制信息=====
[[email protected] mnt]# hexdump -C data
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00000200
[[email protected] mnt]# hexdump -C data1
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000001b0 00 00 00 00 00 00 00 00 8a de 71 8d 00 00 00 00 |..........q.....|
000001c0 21 02 83 03 13 cd 00 08 00 00 00 20 03 00 00 00 |!.......... ....|
000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
00000200
[[email protected] mnt]# hexdump -C data2
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000001b0 00 00 00 00 00 00 00 00 8a de 71 8d 00 00 00 00 |..........q.....|
000001c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
00000200
hexdump -C ##标准十六进制 + ASCII显示
/百度"hexdump"
hexdump命令一般用来查看“二进制”文件的十六进制编码
"-C"表示将结果分为三列,分别是:文件偏移量,字节的十六进制,ASCII字符
输出"*"表示显示结果全部为0,省略
--mbr的范围--
000001b0表示偏移量为:
第三位"1"=16*16=256
第二位"b"=11*16=176
256+176=432
在偏移量"000001b0"这一行从左向右数14个字节
432+14=446,即mbr的大小
另外:
在偏移量"000001b0"这一行中的"8a de 71 8d"
就是fdisk -l /dev/vdb中的
Disk identifier: 0x8d71de8a
--mpt的范围--
从偏移量"000001b0"的最后两个字节到偏移量"000001f0"的前八个字节,共64个字节
64/4=16,即每16个字节为一个主分区表
--硬盘有效性标识的范围--
偏移量"000001b0"的最后两个字节,"55aa"
446+64+2=512,这就是使用"dd"命令截取512字节的含义
####################
####################2.swap####################
[[email protected] ~]# swapon -s ##查看交换分区的使用情况
[[email protected] ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0679fc70
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
/dev/vdb3 411648 616447 102400 83 Linux
/dev/vdb4 616448 20971519 10177536 5 Extended
[[email protected] ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (618496-20971519, default 618496):
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-20971519, default 20971519): +1G
Partition 5 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0679fc70
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
/dev/vdb3 411648 616447 102400 83 Linux
/dev/vdb4 616448 20971519 10177536 5 Extended
/dev/vdb5 618496 2715647 1048576 83 Linux
Command (m for help): t ##修改分区功能id
Partition number (1-5, default 5):
Hex code (type L to list all codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext‘d (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 82
Changed type of partition ‘Linux‘ to ‘Linux swap / Solaris‘
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0679fc70
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
/dev/vdb3 411648 616447 102400 83 Linux
/dev/vdb4 616448 20971519 10177536 5 Extended
/dev/vdb5 618496 2715647 1048576 82 Linux swap / Solaris
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 102400 vdb1
253 18 102400 vdb2
253 19 102400 vdb3
253 20 1 vdb4
253 21 1048576 vdb5
[[email protected] ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
[[email protected] ~]# swapon -a /dev/vdb5 ##将/dev/vdb5挂载到交换分区
swapon: /dev/vdb5: read swap header failed: Invalid argument
[[email protected] ~]# mkswap /dev/vdb5
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=550f2705-76c4-4fb5-92b6-40af5df2db3f
[[email protected] ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb5: UUID="550f2705-76c4-4fb5-92b6-40af5df2db3f" TYPE="swap"
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb5 partition 1048572 0 -1
[[email protected] ~]# swapoff /dev/vdb5 ##将/dev/vdb5从交换分区解除挂载
[[email protected] ~]# swapon -s
=====编辑"/etc/fstab"实现永久挂载到交换分区=====
[[email protected] ~]# vim /etc/fstab
--------------------------------------------------
再最后添加:
/dev/vdb5 swap swap defaults 0 0 ##这里一旦写错,将导致系统启不起来
:wq
--------------------------------------------------
[[email protected] ~]# swapon -s
[[email protected] ~]# swapon -a ##将/etc/fstab中记录的swap设备,全部挂载到交换分区
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb5 partition 1048572 0 -1
[[email protected] ~]# reboot
等待重启
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb5 partition 1048572 0 -1
"man 5 fstab"查看每一列的说明
--------------------------------------------------
第一列 设备
第二列 挂载点
第三列 文件类型
第四列 挂载参数
第五列 是否备份
第六列 是否检测
--------------------------------------------------
=====如果当前没有可用的分区,可以使用文件的方式挂载到交换分区=====
[[email protected] ~]# dd if=/dev/zero of=/mnt/swapfile bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 21.7355 s, 48.2 MB/s
[[email protected] ~]# mkswap /mnt/swapfile
mkswap: /mnt/swapfile: warning: wiping old xfs signature.
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=c7fa31df-2d09-4a62-8180-734fca49fd32
[[email protected] ~]# swapon -a /mnt/swapfile
swapon: /mnt/swapfile: insecure permissions 0644, 0600 suggested.
[[email protected] ~]# chmod 600 /mnt/swapfile
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb5 partition 1048572 0 -1
/mnt/swapfile file 1023996 0 -2
[[email protected] ~]# dd if=/dev/zero of=/mnt/swapfile1 bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 17.5209 s, 59.8 MB/s
[[email protected] ~]# mkswap /mnt/swapfile1
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=0ab4753e-5949-4cb2-8050-26231e18f2b1
[[email protected] ~]# swapon -a /mnt/swapfile1 -p 1 ##"-p"表示优先级
swapon: /mnt/swapfile1: insecure permissions 0644, 0600 suggested.
[[email protected] ~]# chmod 600 /mnt/swapfile1
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb5 partition 1048572 0 -1
/mnt/swapfile file 1023996 0 -2
/mnt/swapfile1 file 1023996 0 1
[[email protected] ~]# man swapon
--------------------------------------------------
-p, --priority priority
Specify the priority of the swap device. priority is a value
between -1 and 32767. Higher numbers indicate higher priority.
See swapon(2) for a full description of swap priorities. Add
pri=value to the option field of /etc/fstab for use with swapon
-a. When priority is not defined it defaults to -1.
--------------------------------------------------
/使用"-p"参数设定的范围是0到32767
注意:使用文件的方式挂载到交换分区只是一个临时办法,当有可用分区的时候,还是尽量使用可用分区挂载到交换分区
重启以后,文件丢失文件系统,需要重新格式化,并且重新挂载
本文出自 “施超Linux学习笔记” 博客,谢绝转载!
以上是关于Linux课程第十三天学习笔记的主要内容,如果未能解决你的问题,请参考以下文章