| [root@centos8 ~]# vim disk_used.sh #!/bin/bash # #********************************************************************************************** #Author:        Raymond #QQ:            88563128 #Date:          2020-12-04 #FileName:      disk_used.sh #URL:           raymond.blog.csdn.net #Description:   The test script #Copyright (C): 2021 All rights reserved #********************************************************************************************* disk=`lsblk | sed -nr 's/(^sd[[:lower:]]).*/\1/p'` disk_num=`lsblk | sed -nr 's/(^sd[[:lower:]]).*/\1/p'| wc -l` disk_end=`lsblk | sed -nr 's/(^sd[[:lower:]]).*/\1/p' | sed -n '$p'` if [ $disk_num -eq 1 ];then     df | grep "/dev/$disk" else     df | grep "/dev/$disk_end" fi :wq [root@centos8 ~]# bash disk_used.sh /dev/sda2      104806400 4868684  99937716   5% / /dev/sda3       52403200  398400  52004800   1% /data /dev/sda1         999320  192580    737928  21% /boot #添加两块硬盘 [root@centos8 ~]# lsblk NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT sda      8:0    0  200G  0 disk ├─sda1   8:1    0    1G  0 part /boot ├─sda2   8:2    0  100G  0 part / ├─sda3   8:3    0   50G  0 part /data ├─sda4   8:4    0    1K  0 part └─sda5   8:5    0    4G  0 part [SWAP] sdb      8:16   0    5G  0 disk sdc      8:32   0    5G  0 disk sr0     11:0    1  7.7G  0 rom  [root@centos8 ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.32.1). 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. Created a new DOS disklabel with disk identifier 0x2e83806e. Command (m for help): n Partition type    p   primary (0 primary, 0 extended, 4 free)    e   extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): First sector (2048-10485759, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +2G Created a new partition 1 of type 'Linux' and of size 2 GiB. Command (m for help): p Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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 Disklabel type: dos Disk identifier: 0x2e83806e Device     Boot Start     End Sectors Size Id Type /dev/sdb1        2048 4196351 4194304   2G 83 Linux Command (m for help): n Partition type    p   primary (1 primary, 0 extended, 3 free)    e   extended (container for logical partitions) Select (default p): p Partition number (2-4, default 2): First sector (4196352-10485759, default 4196352): Last sector, +sectors or +size{K,M,G,T,P} (4196352-10485759, default 10485759): Created a new partition 2 of type 'Linux' and of size 3 GiB. Command (m for help): p Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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 Disklabel type: dos Disk identifier: 0x2e83806e Device     Boot   Start      End Sectors Size Id Type /dev/sdb1          2048  4196351 4194304   2G 83 Linux /dev/sdb2       4196352 10485759 6289408   3G 83 Linux Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. [root@centos8 ~]# lsblk NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT sda      8:0    0  200G  0 disk ├─sda1   8:1    0    1G  0 part /boot ├─sda2   8:2    0  100G  0 part / ├─sda3   8:3    0   50G  0 part /data ├─sda4   8:4    0    1K  0 part └─sda5   8:5    0    4G  0 part [SWAP] sdb      8:16   0    5G  0 disk ├─sdb1   8:17   0    2G  0 part └─sdb2   8:18   0    3G  0 part sdc      8:32   0    5G  0 disk sr0     11:0    1  7.7G  0 rom  [root@centos8 ~]# mkfs.ext4 /dev/sdb1 mke2fs 1.45.4 (23-Sep-2019) Creating filesystem with 524288 4k blocks and 131072 inodes Filesystem UUID: c520fbef-2726-4ab2-b54f-b654bd6c6a1f Superblock backups stored on blocks:     32768, 98304, 163840, 229376, 294912 Allocating group tables: done                            Writing inode tables: done                            Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done [root@centos8 ~]# mkfs.ext4 /dev/sdb2 mke2fs 1.45.4 (23-Sep-2019) Creating filesystem with 786176 4k blocks and 196608 inodes Filesystem UUID: b5efe116-aec1-41e7-80a2-3791b3ee5669 Superblock backups stored on blocks:     32768, 98304, 163840, 229376, 294912 Allocating group tables: done                            Writing inode tables: done                            Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done [root@centos8 ~]# mkdir /mnt/sdb{1,2} [root@centos8 ~]# mount /dev/sdb1 /mnt/sdb1 [root@centos8 ~]# mount /dev/sdb2 /mnt/sdb2 [root@centos8 ~]# df Filesystem     1K-blocks    Used Available Use% Mounted on devtmpfs          893740       0    893740   0% /dev tmpfs             921916       0    921916   0% /dev/shm tmpfs             921916    9528    912388   2% /run tmpfs             921916       0    921916   0% /sys/fs/cgroup /dev/sda2      104806400 4868932  99937468   5% / /dev/sda3       52403200  398400  52004800   1% /data /dev/sda1         999320  192580    737928  21% /boot tmpfs             184380    1168    183212   1% /run/user/42 tmpfs             184380       4    184376   1% /run/user/0 /dev/sdb1        1998672    6144   1871288   1% /mnt/sdb1 /dev/sdb2        3029776    9216   2846944   1% /mnt/sdb2 [root@centos8 ~]# lsblk NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT sda      8:0    0  200G  0 disk ├─sda1   8:1    0    1G  0 part /boot ├─sda2   8:2    0  100G  0 part / ├─sda3   8:3    0   50G  0 part /data ├─sda4   8:4    0    1K  0 part └─sda5   8:5    0    4G  0 part [SWAP] sdb      8:16   0    5G  0 disk ├─sdb1   8:17   0    2G  0 part /mnt/sdb1 └─sdb2   8:18   0    3G  0 part /mnt/sdb2 sdc      8:32   0    5G  0 disk sr0     11:0    1  7.7G  0 rom  [root@centos8 ~]# fdisk /dev/sdc Welcome to fdisk (util-linux 2.32.1). 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. Created a new DOS disklabel with disk identifier 0x2885d32c. Command (m for help): n Partition type    p   primary (0 primary, 0 extended, 4 free)    e   extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): First sector (2048-10485759, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +3G Created a new partition 1 of type 'Linux' and of size 3 GiB. Command (m for help): p Disk /dev/sdc: 5 GiB, 5368709120 bytes, 10485760 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 Disklabel type: dos Disk identifier: 0x2885d32c Device     Boot Start     End Sectors Size Id Type /dev/sdc1        2048 6293503 6291456   3G 83 Linux Command (m for help): n Partition type    p   primary (1 primary, 0 extended, 3 free)    e   extended (container for logical partitions) Select (default p): p Partition number (2-4, default 2): First sector (6293504-10485759, default 6293504): Last sector, +sectors or +size{K,M,G,T,P} (6293504-10485759, default 10485759): Created a new partition 2 of type 'Linux' and of size 2 GiB. Command (m for help): p Disk /dev/sdc: 5 GiB, 5368709120 bytes, 10485760 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 Disklabel type: dos Disk identifier: 0x2885d32c Device     Boot   Start      End Sectors Size Id Type /dev/sdc1          2048  6293503 6291456   3G 83 Linux /dev/sdc2       6293504 10485759 4192256   2G 83 Linux Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. [root@centos8 ~]# lsblk NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT sda      8:0    0  200G  0 disk ├─sda1   8:1    0    1G  0 part /boot ├─sda2   8:2    0  100G  0 part / ├─sda3   8:3    0   50G  0 part /data ├─sda4   8:4    0    1K  0 part └─sda5   8:5    0    4G  0 part [SWAP] sdb      8:16   0    5G  0 disk ├─sdb1   8:17   0    2G  0 part /mnt/sdb1 └─sdb2   8:18   0    3G  0 part /mnt/sdb2 sdc      8:32   0    5G  0 disk ├─sdc1   8:33   0    3G  0 part └─sdc2   8:34   0    2G  0 part sr0     11:0    1  7.7G  0 rom  [root@centos8 ~]# mkfs.ext4 /dev/sdc1 mke2fs 1.45.4 (23-Sep-2019) Creating filesystem with 786432 4k blocks and 196608 inodes Filesystem UUID: e4eac13f-f7aa-4cb1-9c72-d492f16231b9 Superblock backups stored on blocks:     32768, 98304, 163840, 229376, 294912 Allocating group tables: done                            Writing inode tables: done                            Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done [root@centos8 ~]# mkfs.ext4 /dev/sdc2 mke2fs 1.45.4 (23-Sep-2019) Creating filesystem with 524032 4k blocks and 131072 inodes Filesystem UUID: 91996752-51a6-42c3-9398-fdc9c58aa39f Superblock backups stored on blocks:     32768, 98304, 163840, 229376, 294912 Allocating group tables: done                            Writing inode tables: done                            Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done [root@centos8 ~]# mkdir /mnt/sdc{1,2} [root@centos8 ~]# mount /dev/sdc1 /mnt/sdc1 [root@centos8 ~]# mount /dev/sdc2 /mnt/sdc2 [root@centos8 ~]# df Filesystem     1K-blocks    Used Available Use% Mounted on devtmpfs          893740       0    893740   0% /dev tmpfs             921916       0    921916   0% /dev/shm tmpfs             921916    9536    912380   2% /run tmpfs             921916       0    921916   0% /sys/fs/cgroup /dev/sda2      104806400 4868932  99937468   5% / /dev/sda3       52403200  398400  52004800   1% /data /dev/sda1         999320  192580    737928  21% /boot tmpfs             184380    1168    183212   1% /run/user/42 tmpfs             184380       4    184376   1% /run/user/0 /dev/sdb1        1998672    6144   1871288   1% /mnt/sdb1 /dev/sdb2        3029776    9216   2846944   1% /mnt/sdb2 /dev/sdc1        3030800    9216   2847916   1% /mnt/sdc1 /dev/sdc2        2030416    6144   1903084   1% /mnt/sdc2 [root@centos8 ~]# bash disk_used.sh /dev/sdc1        3030800    9216   2847916   1% /mnt/sdc1 /dev/sdc2        2030416    6144   1903084   1% /mnt/sdc2 |