1、創建一個10G分區,并格式為ext4文件系統;
(1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;
(2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;
查看當前分區情況 [root@han ~]# fdisk -l Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000c622b Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 3851 30720000 83 Linux /dev/sda3 3851 3876 204800 82 Linux swap / Solaris 創建新分區 [root@han ~]# fdisk /dev/sda 創建擴展分區 Command (m for help): n Command action e extended p primary partition (1-4) e Selected partition 4 First cylinder (3876-5221, default 3876): Using default value 3876 Last cylinder, +cylinders or +size{K,M,G} (3876-5221, default 5221): Using default value 5221 Command (m for help): p Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000c622b Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 3851 30720000 83 Linux /dev/sda3 3851 3876 204800 82 Linux swap / Solaris /dev/sda4 3876 5221 10807058+ 5 Extended 創建邏輯分區 Command (m for help): n First cylinder (3876-5221, default 3876): Using default value 3876 Last cylinder, +cylinders or +size{K,M,G} (3876-5221, default 5221): +10G Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. 重新讀取分區表 [root@han ~]# partx -a /dev/sda BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 [root@han ~]# partx -a /dev/sda BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 BLKPG: Device or resource busy error adding partition 4 BLKPG: Device or resource busy error adding partition 5 查看系統識別的分區 [root@han ~]# cat /proc/partitions major minor #blocks name 8 0 41943040 sda 8 1 204800 sda1 8 2 30720000 sda2 8 3 204800 sda3 8 4 31 sda4 8 5 10485727 sda5 查看磁盤分區情況 [root@han ~]# fdisk -l Disk /dev/sda: 42.9 GB, 42949672960 bytes 255 heads, 63 sectors/track, 5221 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000c622b Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 3851 30720000 83 Linux /dev/sda3 3851 3876 204800 82 Linux swap / Solaris /dev/sda4 3876 5221 10807058+ 5 Extended /dev/sda5 3876 5181 10485727 83 Linux 格式化分區 [root@han ~]# mke2fs -t ext4 -b 2048 -m 2 -L MYDATA /dev/sda5 開啟默認掛載特性 [root@han ~]# tune2fs -o acl /dev/sda5 掛載分區 [root@han ~]# mount -o noexec,noatime /dev/sda5 /data/mydata
2、創建一個大小為1G的swap分區,并創建好文件系統,并啟用之;
創建1G的分區 [root@han ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x24980e07. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): Value out of range. Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x24980e07 Device Boot Start End Blocks Id System /dev/sdb1 1 132 1060258+ 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. 重新讀取分區表 [root@han ~]# partx -a /dev/sdb BLKPG: Device or resource busy error adding partition 1 [root@han ~]# cat /proc/partitions major minor #blocks name 8 0 41943040 sda 8 1 204800 sda1 8 2 30720000 sda2 8 3 204800 sda3 8 4 1 sda4 8 5 10485727 sda5 8 16 20971520 sdb 8 17 1060258 sdb1 格式化swap分區 [root@han ~]# mkswap /dev/sdb1 Setting up swapspace version 1, size = 1060252 KiB no label, UUID=7597f39c-fe1a-4dea-957e-dc051b2a9be8 查看分區使用情況 [root@han ~]# free -m total used free shared buffers cached Mem: 980 163 817 0 15 56 -/+ buffers/cache: 90 889 Swap: 199 0 199 啟動swap分區 [root@han ~]# swapon /dev/sdb1 [root@han ~]# free -m total used free shared buffers cached Mem: 980 163 816 0 15 56 -/+ buffers/cache: 91 889 Swap: 1235 0 1235 查看分區uuid和類型 [root@han ~]# blkid /dev/sda5: LABEL="MYDATA" UUID="4721b08d-3ed8-4ad8-a4cf-215cc15b1a3a" TYPE="ext4" /dev/sda1: UUID="5576bf88-a991-4af9-b8a7-ff55f6f5a67f" TYPE="ext4" /dev/sda2: UUID="1cc68cb8-0de5-4cbf-ad1c-b6ed129b1177" TYPE="ext4" /dev/sda3: UUID="9ee78e6e-d386-4af2-90e7-17bd0f594d06" TYPE="swap" /dev/sdb1: UUID="7597f39c-fe1a-4dea-957e-dc051b2a9be8" TYPE="swap"
3、寫一個腳本
(1)、獲取并列出當前系統上的所有磁盤設備;
(2)、顯示每個磁盤設備上每個分區相關的空間使用信息;
#!/bin/bah # 顯示當前系統所有磁盤分區的使用情況 disk=$(fdisk -l | grep "^Disk /dev/sd[a-z]" | awk -F [:" "] '{print $2}') echo -e "磁盤設備有:\n$disk" echo "磁盤的分區使用情況如下:" df 執行結果: [root@han scripts]# bash diskinfo.sh 磁盤設備有: /dev/sda /dev/sdb 磁盤的分區使用情況如下: Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 30106576 3515944 25054632 13% / tmpfs 502068 0 502068 0% /dev/shm /dev/sda1 194241 34123 149878 19% /boot
4、總結RAID的各個級別及其組合方式和性能的不同;
RAID-0: 將幾塊磁盤并行組織起來,當作一塊磁盤使用 raid控制器將數據分成若干chunck,再平均存儲到不同的磁盤上去耐用性下降,出錯幾率上升, 存儲非關鍵性數據 讀、寫性能提升; 可用空間:N*min(S1,S2,...) 無容錯能力 最少磁盤數:2, 2+ RAID-1: 讀性能提升、寫性能略有下降; 可用空間:1*min(S1,S2,...) 有冗余能力 最少磁盤數:2, 2+ RAID-4: 最少磁盤數: 3+ 其中一塊盤存校驗數據(做亦或運算) 風險高, 校驗盤有性能瓶頸 RAID-5: 讀、寫性能提升 可用空間:(N-1)*min(S1,S2,...) 有容錯能力:1塊磁盤 最少磁盤數:3, 3+ 輪流做校驗盤 RAID-6:不常用 讀、寫性能提升 可用空間:(N-2)*min(S1,S2,...) 有容錯能力:2塊磁盤 最少磁盤數:4, 4+ 校驗碼存兩次 混合類型 RAID-10: 讀、寫性能提升 可用空間:N*min(S1,S2,...)/2 有容錯能力:每組鏡像最多只能壞一塊; 最少磁盤數:4, 4+ RAID-01: 不常用 風險比raid10大 每組壞一個,數據丟失 JBOD:Just a Bunch Of Disks 功能:將多塊磁盤的空間合并一個大的連續空間使用; 可用空間:sum(S1,S2,...) 常用級別:RAID-0, RAID-1, RAID-5, RAID-10, RAID-50, JBOD
5、創建一個大小為10G的RAID1,要求有一個空閑盤,而且CHUNK大小為128k;
(1)創建分區 [root@lab1 ~]# fdisk /dev/sda 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): p Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0003bc7d Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 207888383 103431168 8e Linux LVM Command (m for help): n Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): e Partition number (3,4, default 3): 3 First sector (207888384-419430399, default 207888384): Using default value 207888384 Last sector, +sectors or +size{K,M,G} (207888384-419430399, default 419430399): Using default value 419430399 Partition 3 of type Extended and of size 100.9 GiB is set Command (m for help): p Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0003bc7d Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 207888383 103431168 8e Linux LVM /dev/sda3 207888384 419430399 105771008 5 Extended Command (m for help): n Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) Select (default p): l Adding logical partition 5 First sector (207890432-419430399, default 207890432): Using default value 207890432 Last sector, +sectors or +size{K,M,G} (207890432-419430399, default 419430399): +10G Partition 5 of type Linux and of size 10 GiB is set Command (m for help): n Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) Select (default p): l Adding logical partition 6 First sector (228864000-419430399, default 228864000): Using default value 228864000 Last sector, +sectors or +size{K,M,G} (228864000-419430399, default 419430399): +10G Partition 6 of type Linux and of size 10 GiB is set Command (m for help): p Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0003bc7d Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 207888383 103431168 8e Linux LVM /dev/sda3 207888384 419430399 105771008 5 Extended /dev/sda5 207890432 228861951 10485760 83 Linux /dev/sda6 228864000 249835519 10485760 83 Linux Command (m for help): t Partition number (1-3,5,6, default 6): 5 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): t Partition number (1-3,5,6, default 6): 6 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): p Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0003bc7d Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 207888383 103431168 8e Linux LVM /dev/sda3 207888384 419430399 105771008 5 Extended /dev/sda5 207890432 228861951 10485760 fd Linux raid autodetect /dev/sda6 228864000 249835519 10485760 fd Linux raid autodetect 漏了一個分區,下面補上 Command (m for help): n Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) Select (default p): l Adding logical partition 7 First sector (249837568-419430399, default 249837568): Using default value 249837568 Last sector, +sectors or +size{K,M,G} (249837568-419430399, default 419430399): +10G Partition 7 of type Linux and of size 10 GiB is set Command (m for help): p Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0003bc7d Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 207888383 103431168 8e Linux LVM /dev/sda3 207888384 419430399 105771008 5 Extended /dev/sda5 207890432 228861951 10485760 fd Linux raid autodetect /dev/sda6 228864000 249835519 10485760 fd Linux raid autodetect /dev/sda7 249837568 270809087 10485760 83 Linux Command (m for help): t Partition number (1-3,5-7, default 7): 7 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): p Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0003bc7d Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 207888383 103431168 8e Linux LVM /dev/sda3 207888384 419430399 105771008 5 Extended /dev/sda5 207890432 228861951 10485760 fd Linux raid autodetect /dev/sda6 228864000 249835519 10485760 fd Linux raid autodetect /dev/sda7 249837568 270809087 10485760 fd Linux raid autodetect Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. 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) [root@lab1 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-2 [root@lab1 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-3 partx: /dev/sda: error adding partitions 5-7 [root@lab1 ~]# cat /proc/partitions major minor #blocks name 11 0 4228096 sr0 8 0 209715200 sda 8 1 512000 sda1 8 2 103431168 sda2 8 3 1 sda3 8 5 10485760 sda5 8 6 10485760 sda6 8 7 10485760 sda7 253 0 102400000 dm-0 253 1 1024000 dm-1 (2) 創建raid [root@lab1 ~]# yum -y install mdadm [root@lab1 ~]# mdadm -C /dev/md1 -a yes -l 1 -c 128 -n 2 -x 1 /dev/sda{5,6,7} mdadm: Note: this array has metadata at the start and may not be suitable as a boot device. If you plan to store '/boot' on this device please ensure that your boot-loader understands md/v1.x metadata, or use --metadata=0.90 Continue creating array? Continue creating array? (y/n) y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started.
6、創建一個大小為4G的RAID5設備,chunk大小為256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能;
(1)創建3個2G的分區 [root@lab1 ~]# fdisk /dev/sda 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 Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) Select (default p): l Adding logical partition 8 First sector (270811136-419430399, default 270811136): Using default value 270811136 Last sector, +sectors or +size{K,M,G} (270811136-419430399, default 419430399): +2G Partition 8 of type Linux and of size 2 GiB is set Command (m for help): n Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) Select (default p): l Adding logical partition 9 First sector (275007488-419430399, default 275007488): Using default value 275007488 Last sector, +sectors or +size{K,M,G} (275007488-419430399, default 419430399): +2G Partition 9 of type Linux and of size 2 GiB is set Command (m for help): n Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) Select (default p): l Adding logical partition 10 First sector (279203840-419430399, default 279203840): Using default value 279203840 Last sector, +sectors or +size{K,M,G} (279203840-419430399, default 419430399): +2G Partition 10 of type Linux and of size 2 GiB is set Command (m for help): p Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0003bc7d Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 207888383 103431168 8e Linux LVM /dev/sda3 207888384 419430399 105771008 5 Extended /dev/sda5 207890432 228861951 10485760 fd Linux raid autodetect /dev/sda6 228864000 249835519 10485760 fd Linux raid autodetect /dev/sda7 249837568 270809087 10485760 fd Linux raid autodetect /dev/sda8 270811136 275005439 2097152 83 Linux /dev/sda9 275007488 279201791 2097152 83 Linux /dev/sda10 279203840 283398143 2097152 83 Linux Command (m for help): t Partition number (1-3,5-10, default 10): 8 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): t Partition number (1-3,5-10, default 10): 9 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): t Partition number (1-3,5-10, default 10): 10 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. 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) Syncing disks. [root@lab1 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-3 partx: /dev/sda: error adding partitions 5-7 [root@lab1 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-3 partx: /dev/sda: error adding partitions 5-10 (2)創建raid [root@lab1 ~]# mdadm -C /dev/md2 -a yes -l 5 -c 256 -n 3 /dev/sda{8,9,10} mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md2 started. (3)格式化并掛載 [root@lab1 ~]# mkfs.ext4 /dev/md2 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=64 blocks, Stripe width=128 blocks 262144 inodes, 1047552 blocks 52377 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1073741824 32 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done [root@lab1 ~]# mdadm -D /dev/md2 /dev/md2: Version : 1.2 Creation Time : Tue Feb 21 05:15:06 2017 Raid Level : raid5 Array Size : 4190208 (4.00 GiB 4.29 GB) Used Dev Size : 2095104 (2046.00 MiB 2145.39 MB) Raid Devices : 3 Total Devices : 3 Persistence : Superblock is persistent Update Time : Tue Feb 21 05:17:54 2017 State : clean Active Devices : 3 Working Devices : 3 Failed Devices : 0 Spare Devices : 0 Layout : left-symmetric Chunk Size : 256K Name : lab1.tfedu.net:2 (local to host lab1.tfedu.net) UUID : 21b997cd:3ad38c23:3838adc4:0a70bce8 Events : 18 Number Major Minor RaidDevice State 0 8 8 0 active sync /dev/sda8 1 8 9 1 active sync /dev/sda9 3 8 10 2 active sync /dev/sda10 [root@lab1 ~]# mkdir /backup [root@lab1 ~]# vim /etc/fstab /dev/md2 /backup ext4 defaults,acl,noatime 0 0 [root@lab1 ~]# mount -a [root@lab1 ~]# mount | grep md2 /dev/md2 on /backup type ext4 (rw,noatime,seclabel,stripe=128,data=ordered)
7、寫一個腳本
(1) 接受一個以上文件路徑作為參數;
(2) 顯示每個文件擁有的行數;
(3) 總結說明本次共為幾個文件統計了其行數;
#!/bin/bash # 統計傳入文件的行數 if [ $# -lt 1 ];then echo "請輸入至少一個參數" exit 1 fi files=0 file_num=$# while [ $file_num -gt 0 ];do if [ -f $1 ];then lines=$(cat $1 | wc -l) let files+=1 echo "$1有${lines}行" else echo "$1 不存在" fi let file_num-=1 shift done echo "總共有${files}個文件統計了其行數" 執行結果: [root@localhost ~]# bash files.sh /etc/issue /etc/fstab /etc/issue有3行 /etc/fstab有12行 總共有2個文件統計了其行數
8、寫一個腳本
(1) 傳遞兩個以上字符串當作用戶名;
(2) 創建這些用戶;且密碼同用戶名;
(3) 總結說明共創建了幾個用戶;
#!/bin/bash # 根據傳入的字符串創建用戶 if [ $# -lt 3 ];then echo "請輸入至少3個參數" exit 1 fi num=0 file_num=$# while [ $file_num -gt 0 ];do if ! id $1 &> /dev/null;then useradd $1 echo "$1" | passwd --stdin $1 &> /dev/null let num+=1 echo "$1創建成功" else echo "$1已經存在" fi let file_num-=1 shift done echo "總共創建了${num}個用戶" 執行結果: [root@localhost ~]# bash createuser.sh zhang 請輸入至少3個參數 [root@localhost ~]# bash createuser.sh zhang wang sun zhang創建成功 wang創建成功 sun創建成功 總共創建了3個用戶 [root@localhost ~]# bash createuser.sh zhang wang sun zhang已經存在 wang已經存在 sun已經存在 總共創建了0個用戶
9、寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的ID之和;
#!/bin/bash #創建visitor1-visitor20,并計算id之和 idsum=0 for i in {1..20};do if ! id visitor${i} &> /dev/null;then useradd visitor${i} let idsum+=$(id -u visitor${i}) else echo "用戶visitor${i}已存在" fi done echo "創建的用戶id之和為: $idsum" 執行結果: [root@localhost ~]# bash createuser2.sh 創建的用戶id之和為: 20270
原創文章,作者:hansj,如若轉載,請注明出處:http://www.www58058.com/69825
贊~寫的比較認真~繼續加油!