N25-week7 作业

1. 创建一个10G的分区,并格式为ext4文件系统
  (1) 要求block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
  (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳

#创建文件系统,首先使用fdisk对磁盘进行分区操作

[root@localhost ~]# fdisk /dev/sdb

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').
#fdisk下n命令创建新的分区
     Command (m for help): n
#选择p创建主分区
Command action
  e   extended
     p   primary partition (1-4)
#选择1创建1号分区
Partition number (1-4): 1
#起始柱面默认选1
First cylinder (1-261, default 1):
Using default value 1
#结尾柱面默认为最后一个柱面
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261):
Using default value 261
#使用t命令来修改分区的system id
Command (m for help): t
Selected partition 1
#83为ext系列文件系统的system id
Hex code (type L to list codes): 83
#使用w命令将操作保存
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

#之后使用重读分区表

[root@localhost ~]# partx -a /dev/sdb

#查看/proc/partitions文件

[root@localhost ~]# cat /proc/partitions
major minor  #blocks  name

   8        0   20971520 sda
   8        1     512000 sda1
   8        2   20458496 sda2
   8       16    2097152 sdb
   8       17    2096451 sdb1
   8       32    1048576 sdc
 253        0   18423808 dm-0
 253        1    2031616 dm-1

#到此,分区创建完毕,下面来对分区进行ext文件系统的格式化

#使用mkfs.ext4命令来格式化/dev/sdb1

[root@localhost ~]# mkfs.ext4 -b 2048 -m 2 -L MYDATA /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=MYDATA
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 1048224 blocks
20964 blocks (2.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=537919488
64 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816

Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

#然后创建/data/mydata目录

[root@localhost ~]# mkdir -p /data/mydata

#之后使用mount命令将/dev/sdb1挂载到/data/mydata上

[root@localhost ~]# mount -a -t ext4  -o noexec -o noatime -o acl /dev/sdb1 /dat
[root@localhost ~]# cd /data/mydata/
[root@localhost mydata]# ll
total 16
drwx------. 2 root root 16384 Feb 18 15:39 lost+found
[root@localhost mydata]#

2. 创建一个大小为10G的swap分区,并创建好文件系统,并启用之;
#使用fdisk命令在sdc硬盘上新建分区

[root@localhost ~]# fdisk /dev/sdc

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): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130):
Using default value 130

#调整system type为82(swap)
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap / Solaris)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

#partx /dev/sdc重读分区表

[root@localhost ~]# partx /dev/sdc
# 1:        63-  2088449 (  2088387 sectors,   1069 MB)
# 2:         0-       -1 (        0 sectors,      0 MB)
# 3:         0-       -1 (        0 sectors,      0 MB)
# 4:         0-       -1 (        0 sectors,      0 MB)

#查看系统是否认出/dev/sdc1分区

[root@localhost ~]# cat /proc/partitions
major minor  #blocks  name

   8        0   20971520 sda
   8        1     512000 sda1
   8        2   20458496 sda2
   8       16    2097152 sdb
   8       17    2096451 sdb1
   8       32    1048576 sdc
   8       33    1044193 sdc1
 253        0   18423808 dm-0
 253        1    2031616 dm-1
[root@localhost ~]#

#为/dev/sdc1创建swap文件系统

[root@localhost ~]# mkswap /dev/sdc1
Setting up swapspace version 1, size = 1044188 KiB
no label, UUID=80f20b38-b6e2-48b1-902c-995f3cbad9da

#使用free命令查看当前swap文件系统的大小

[root@localhost ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          980M       240M       740M       204K        22M        92M
-/+ buffers/cache:       126M       854M
Swap:         1.9G         0B       1.9G

#使用swapon命令启用/dev/sdc1 swap分区

[root@localhost ~]# swapon /dev/sdc1

#再使用free命名查看swap文件系统的大小, 由1.9G增加到2.9G, 说明/dev/sdc1 swap分区成功启用

[root@localhost ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          980M       240M       739M       204K        22M        92M
-/+ buffers/cache:       126M       854M
Swap:         2.9G         0B       2.9G

3. 写一个脚本
(1) 获取并列出当前系统上的所有磁盘设备
(2) 显示每个磁盘设备上每个分区相关的空间使用信息

#脚本内容

[root@localhost ~]# cat week7.sh
#!/bin/bash

#获取并列出当前系统上的所有磁盘设备
#显示每个磁盘设备上每个分区相关的空间使用信息

declare i #声明变量i, 用于控制for循环体
declare disk_list #声明变量 disk_lis,保存系统上磁盘设备列表

disk_list=$(ls /dev/{hd,sd}* 2> /dev/null)
echo -e  "System disk device list:\n$disk_list"

echo -e  "\nDetails of Used/Free space:"

for i in $disk_list ; do
        df -h -T $i
done

#脚本执行结果

[root@localhost ~]# bash week7.sh
System disk device list:
/dev/sda
/dev/sda1
/dev/sda2
/dev/sdb
/dev/sdb1
/dev/sdc
/dev/sdc1

Details of Used/Free space:
Filesystem     Type  Size  Used Avail Use% Mounted on
-              -     480M  200K  480M   1% /dev
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sda1      ext4  477M   28M  425M   7% /boot
Filesystem     Type  Size  Used Avail Use% Mounted on
-              -     480M  200K  480M   1% /dev
Filesystem     Type  Size  Used Avail Use% Mounted on
-              -     480M  200K  480M   1% /dev
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sdb1      ext4  2.0G  9.1M  1.9G   1% /data/mydata
Filesystem     Type  Size  Used Avail Use% Mounted on
-              -     480M  200K  480M   1% /dev
Filesystem     Type  Size  Used Avail Use% Mounted on
-              -     480M  200K  480M   1% /dev

4. 总结RAID的各个级别及其组合方式和性能的不同

5. 创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为 128K

6. 创建一个大小为4G的RAID5设备,chunk大小为256K,格式化为ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能

7. 写一个脚本
(1) 接受一个以上文件路径作为参数
(2) 显示每个文件拥有的行数
(3) 总结说明本次共为几个文件统计了其行数

#脚本内容
#!/bin/bash

#接受一个以上的文件路径作为参数
#显示每个文件拥有的行数
#总结说明本次共为几个文件统计了行数

#声明变量i,用于控制for循环体
declare i
declare File_List
declare File_Count=0

read -p "Please input file name here, this script could count lines number of the file you typed. Warnning:you should type at least 1 file:" File_List

if [[ -z $File_List ]]; then
   echo "At least provide one file to run this script"
   exit 2
else
   for i in $File_List; do
        if [ ! -e $i ];then
             echo "No such file: $i"
        elif [ ! -f $i ];then
             echo "File $i is not a normal file and canot count lines number"
        else
             echo "line number of file $i is $(wc -l $i|cut -f1 -d' ')"
             let File_Count=$File_Count+1
        fi
   done
fi

echo "Totaly count line number of $File_Count files."

unset i
unset File_List
unset File_Count

#运行结果
#(1)未给出参数的运行结果

[root@localhost ~]# bash week7_2.sh
Please input file name here, this script could count lines number of the file you typed. Warnning:you should type at least 1 file:
At least provide one file to run this script
[root@localhost ~]#

#(2)给出1个不存在的文件,一个设备文件,一个普通文件的运行结果

[root@localhost ~]# bash week7_2.sh
Please input file name here, this script could count lines number of the file you typed. Warnning:you should type at least 1 file:a
No such file: a
Totaly count line number of 0 files.
[root@localhost ~]#

#(3)给出N个普通文件的运行结果

[root@localhost ~]# !b
bash week7_2.sh
Please input file name here, this script could count lines number of the file you typed. Warnning:you should type at least 1 file:/etc/fstab /etc/rc.d/rc.sysinit /proc/partitins /root/week7.sh /root/week7_2.sh
line number of file /etc/fstab is 16
line number of file /etc/rc.d/rc.sysinit is 691
No such file: /proc/partitins
line number of file /root/week7.sh is 19
line number of file /root/week7_2.sh is 34
Totaly count line number of 4 files.

#(4)给出的文件中没有普通文件的运行结果

[root@localhost ~]# !b
bash week7_2.sh
Please input file name here, this script could count lines number of the file you typed. Warnning:you should type at least 1 file:/roott/week7.sh /ect/fstab /dev/sda /dev/sdc1
No such file: /roott/week7.sh
No such file: /ect/fstab
File /dev/sda is not a normal file and canot count lines number
File /dev/sdc1 is not a normal file and canot count lines number
Totaly count line number of 0 files.
[root@localhost ~]#

8 写一个脚本
(1) 传递两个以上的字符串当做用户名
(2) 创建这些用户,且密码同用户名
(3) 总结说明共创建了几个用户
#脚本内容

[root@localhost ~]# cat week7_3.sh
#!/bin/bash

#传递两个以上的字符串当做用户名
#创建这些用户,且密码同用户名
#总结说明共创建了几个用户

declare i #用于控制for循环体
declare user_list
declare user_count

read -p "please type username which need to be created:" user_list
echo $user_list
if [[ -z $user_list ]]; then
   echo "Invaild parameter"
   exit 2
else
   for i in $user_list; do
      if id $i &> /dev/null;then
         echo "$i exists"
      elif [[ $(echo ${#i}) -le 2 ]];then
         echo "Length of username should be more than 2 charactors"
      else
         useradd $i &> /dev/null && echo $i | passwd $i --stdin &> /dev/null && echo "add user $i sucessfully. Password is the same as username"
         let user_count=$user_count+1
      fi
   done
fi

echo "Totally add $user_count users."

unset i
unset user_list
unset user_count
[root@localhost ~]#

#执行结果
#(1) 未给出参数的执行结果

[root@localhost ~]# bash week7_3.sh
please type username which need to be created:

Invaild parameter
[root@localhost ~]#

#(2) 传递的参数字符数小于2的执行结果

[root@localhost ~]# !b
bash week7_3.sh
please type username which need to be created:a b cd de c1
a b cd de c1
Length of username should be more than 2 charactors
Length of username should be more than 2 charactors
Length of username should be more than 2 charactors
Length of username should be more than 2 charactors
Length of username should be more than 2 charactors
Totally add 0 users.
[root@localhost ~]#

#(3) 传递的参数已经存在于系统的执行结果

[root@localhost ~]# !b
bash week7_3.sh
please type username which need to be created:root bin daemon adm lp
root bin daemon adm lp
root exists
bin exists
daemon exists
adm exists
lp exists
Totally add 0 users.
[root@localhost ~]#

#(4) 正常添加用户名的执行结果

[root@localhost ~]# !b
bash week7_3.sh
please type username which need to be created:week7 week7_1 week7_3
week7 week7_1 week7_3
add user week7 sucessfully. Password is the same as username
add user week7_1 sucessfully. Password is the same as username
add user week7_3 sucessfully. Password is the same as username
Totally add 3 users.
[root@localhost ~]#

9. 写一个脚本,新建20个用户visitor1-vistor20, 计算他们的ID之和

#脚本内容

[root@localhost ~]# cat week7_4.sh
#! /bin/bash

#写一个脚本,新建20个用户visitor1-vissitor20,计算他们的ID之和

declare i
declare user_count=0
declare id_sum=0

for i in {1..20}; do
   if id visitor$i &> /dev/null;then
      echo "user visitor$i exists."
   else
      useradd visitor$i &> /dev/null && echo "Add visitor$i" && let id_sum=$(id -u visitor$i)+$id_sum && let user_count=$user_count+1
   fi
done

echo "Total add $user_count users and sum id number is $id_sum"

unset i
unset user_count
unset id_sum
[root@localhost ~]#

#执行结果
#(1) 第一次执行结果,visitor1-visitor20全部添加,并计算出id之和

[root@localhost ~]# bash week7_4.sh
Add visitor1
Add visitor2
Add visitor3
Add visitor4
Add visitor5
Add visitor6
Add visitor7
Add visitor8
Add visitor9
Add visitor10
Add visitor11
Add visitor12
Add visitor13
Add visitor14
Add visitor15
Add visitor16
Add visitor17
Add visitor18
Add visitor19
Add visitor20
Total add 20 users and sum id number is 10210
[root@localhost ~]#

#(2) 第二次执行结果,visitor1-visitor20全部已经存在,没有添加用户,没有计算id之和

[root@localhost ~]# bash week7_4.sh
user visitor1 exists.
user visitor2 exists.
user visitor3 exists.
user visitor4 exists.
user visitor5 exists.
user visitor6 exists.
user visitor7 exists.
user visitor8 exists.
user visitor9 exists.
user visitor10 exists.
user visitor11 exists.
user visitor12 exists.
user visitor13 exists.
user visitor14 exists.
user visitor15 exists.
user visitor16 exists.
user visitor17 exists.
user visitor18 exists.
user visitor19 exists.
user visitor20 exists.
Total add 0 users and sum id number is 0
[root@localhost ~]#

#(3) 第三次执行结果,删除奇数结尾的visitor用户之后,再次执行,添加了10个用户,并计算了id之和

[root@localhost ~]# for i in $(seq 1 2 20);do userdel -r visitor$i
> done
[root@localhost ~]# tail /etc/passwd
visitor2:x:502:502::/home/visitor2:/bin/bash
visitor4:x:504:504::/home/visitor4:/bin/bash
visitor6:x:506:506::/home/visitor6:/bin/bash
visitor8:x:508:508::/home/visitor8:/bin/bash
visitor10:x:510:510::/home/visitor10:/bin/bash
visitor12:x:512:512::/home/visitor12:/bin/bash
visitor14:x:514:514::/home/visitor14:/bin/bash
visitor16:x:516:516::/home/visitor16:/bin/bash
visitor18:x:518:518::/home/visitor18:/bin/bash
visitor20:x:520:520::/home/visitor20:/bin/bash
[root@localhost ~]# bash week7_4.sh
Add visitor1
user visitor2 exists.
Add visitor3
user visitor4 exists.
Add visitor5
user visitor6 exists.
Add visitor7
user visitor8 exists.
Add visitor9
user visitor10 exists.
Add visitor11
user visitor12 exists.
Add visitor13
user visitor14 exists.
Add visitor15
user visitor16 exists.
Add visitor17
user visitor18 exists.
Add visitor19
user visitor20 exists.
Total add 10 users and sum id number is 5255
[root@localhost ~]#

原创文章,作者:JL,如若转载,请注明出处:http://www.178linux.com/69800

(0)
上一篇 2017-02-22 14:45
下一篇 2017-02-22 21:00

相关推荐

  • Python 数据结构

    python 内置数据结构python 内置数据结构数字的处理函数: round() 五舍六入       2.5  2     2.6    3 floor() 向下取整       2.5  2     2.6    2 ceil() 向上取整       2.5  3     2.6    3 min() 取最小的       1,2,3      1…

    Linux干货 2017-09-23
  • N25-第一周作业

    1、描述计算机的组成及其功能:   (1)按硬件来分,硬件系统主要有:运算器、控制器、存储器、输入设备和输出设备;    ① 控制器是整个计算机的中枢神经,其功能是对程序规定的控制信息进行解释,根据其要求进行控制,调度程序、数据、地址,协调计算机各部分工作及内存与外设的访问等;    ② 运算器的功…

    Linux干货 2016-12-05
  • 马哥教育网络班21期+第2周课程练习

    一、Linux文件管理类命令汇总 1、文件的复制命令:cp 复制命令:cp     cp [OPTION]… [-T] SOURCE DEST     cp [OPTION]… SOURCE… DI…

    Linux干货 2016-07-29
  • MySQL or MariaDB 简介

    DBMS:数据库管理系统 RDBMS:关系型数据库管理系统    总之:他们都是一个数据管理程序;大多都是CS架构,都有专门的通信协议进行数据交换 关系模型:               表(行或者列):二维关系 设计范式:       &…

    Linux干货 2017-01-12
  • 脚本作业–函数练习

    1、编写服务脚本/root/bin/testsrv.sh,完成如下要求(1) 脚本可接受参数:start, stop, restart, status(2) 如果参数非此四者之一,提示使用格式后报错退出(3) 如是start:则创建/var/lock/subsys/SCRIPT_NAME, 并显示“启动成功”考虑:如果事先已经启动过一次,该如何处理?(4) …

    Linux干货 2016-08-24
  • 软件包管理工具的使用及yum源仓库的配置

    我们知道CentOS,本质上是RedHat的社区版,其无论是功能还是软件配置上都与RedHat保持一致,因此CentOs系列的软件包管理主要的也是rpm和yum工具。 RPM包管理 rpm包管理工具可以说是历史悠久、名声斐然了。rpm起初是RedHat公司研发的软件包管理工具,即RedHat Package Managemer。但由于其性能突出,在开源界得到…

    Linux干货 2016-11-06

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-15 01:28

    赞~给出详细的实现方式也给出了实验结果~比较不错~继续加油!