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)
JLJL
上一篇 2017-02-22
下一篇 2017-02-22

相关推荐

  • 学习标准I/O和管道的心得体会

    重定向、标准输出、标准输入、标准错误、管道的命令整理

    2017-11-20
  • grub知识与故障排除

    知识点回顾 at 任务的存放位置:/var/spool/at/ crond 任务存放位置:/var/spool/cron/username 查看服务有没有运行 centos6:  service  atd  status    chkconfig –list atd chkconfig atd o…

    Linux干货 2016-09-13
  • 自制Linux系统

                          自制Linux系统 1、环境准备:         在centos6上添加一块新硬盘,并分区格式化。 &n…

    系统运维 2016-09-21
  • Linux之bash shell脚本编程入门篇(二)以及文件查找和压缩的使用

    bash shell脚本编程入门篇(二) read命令的使用 作用:     使用read来把输入值分配给一个或多个shell变量:         -p指定要显示的提示      &nbs…

    Linux干货 2016-08-18
  • M20 – 1- 第三周博客(3):Linux上文本处理三剑客grep

    Grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。 1、作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用 权限是…

    Linux干货 2016-08-08
  • SecureCRT 8.0的安装及Linux基本命令介绍

    在CentOS虚拟中安装并打开图形界面后,可以在图形界面和命令行界面之间任意切换。在打开终端后输入init3可以切换至命令行界面,输入init5返回到图形界面。 这里我们用SecureCRT 8.0来远程的对虚拟机进行操作,对于SecureCRT 8.0的安装步骤就不详细介绍,简单介绍下如何连接到CentOS。 1. 2.点击完成。   &nbsp…

    2017-07-13

评论列表(1条)

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

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