面试习题:分区及bash脚本
1、创建一个10G分区,并格式化为ext4文件系统,要求:
1 ~]# fdisk /dev/sdb 2 Command (m for help): n 3 Partition type: 4 p primary (0 primary, 0 extended, 4 free) 5 e extended 6 Select (default p): p 7 Partition number (1-4, default 1): 8 First sector (2048-41943039, default 2048): 9 Using default value 2048 10 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G 11 Partition 1 of type Linux and of size 10 GiB is set 12 13 Command (m for help): l 14 15 Command (m for help): t 16 Selected partition 1 17 Hex code (type L to list all codes): 83 18 19 ~]# mkfs.ext4 /dev/sdb1
(1)block大小为2048,预留空间为20%,标卷为MYDATA;
~]# mke2fs -L MYDATA -m 20 -b 2048 -t ext4 /dev/sdb1
(2)挂载至/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
~]# mount -o noatime -o noexec /dev/sdb1 /mydata/
(3)可开机自动挂载;
~]# vim /etc/fstab LABEL='MYDATA' /mydata ext4 noexec,noatime 0 0
2、创建一个大小为10G 的swap分区,并启用;
~]# fdisk /dev/sdb Command (m for help): n Command (m for help): t Partition number (1,2, default 2): 82 ~]# mkswap /dev/sdb2 ~]# swapon /dev/sdb2
3、编写一个脚本计算/etc/passwd文件中第10个用户和第20个用户的ID之和;
#!/bin/bash # user10=$(head -10 /etc/passwd | tail -1 | cut -d: -f3) user20=$(head -20 /etc/passwd | tail -1 | cut -d: -f3) sum=$[$user10+$user20] echo "the user 10 20 ID sum is:$sum"
4、当前主机保存至hostname变量中,主机如果为空,或者为localhost。localdomain则将设置为www.datieli.com
1 [root@localhost script]# cat ch_host_na.sh 2 #!/bin/bash 3 # 4 hostName=$(hostname) 5 6 [ -z "$hostName" -o "$hostName" == "localhost.localdomain" -o "$hostName" == "localhost" ] && hostname www.datieli.com 7 [root@localhost script]# hostname 8 www.datieli.com
5、编写脚本,通过命令行参数传入一个用户,则判断ID号是偶数还是奇数。
1 #!/bin/bash 2 #判断用户ID的奇偶性 3 # 4 id=$(id -u $1) 5 if [ $(expr $id % 2) -eq 0 ];then 6 echo "the userID is even number " 7 else 8 echo "the userID is uneven number" 9 fi