1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;[[email protected] ~]# vim usershell.sh

#!/bin/bash

#

declare -i nologin_num=0

declare -i login_num=0

for i in $(cut -d: -f7 /etc/passwd);do

if [ "$i" == "/sbin/nologin" ];then

let nologin_num++

else

let login_num++

fi

done

echo "The total number of user shell that can‘t login is :$nologin_num"

echo "The total number of user shell that can login is :$login_num"

脚本测试[[email protected] ~]# bash usershell.sh

The total number of user shell that can‘t login is :33

The total number of user shell that can login is :5

[[email protected] ~]# grep -o /sbin/nologin /etc/passwd | wc -l

33

[[email protected] ~]# grep -v /sbin/nologin /etc/passwd | wc -l

5

2、写一个脚本

(1) 获取当前主机的主机名,保存于hostname变量中;

(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;

(3) 否则,则显示当前主机名;vim hostnametest.sh

#!/bin/bash

#

hostname=$(hostname)

if [ "$hostname" == "localhost" ];then

hostname www.magedu.com

echo "Hostname has changed to www.magedu.com"

else

echo "Current hostname is $hostname"

fi

脚本测试[[email protected] ~]# hostname

localhost

[[email protected] ~]# bash hostnametest.sh

Hostname has changed to www.magedu.com

[[email protected] ~]# hostname

www.magedu.com

[[email protected] ~]# bash hostnametest.sh

Current hostname is www.magedu.com

3、写一个脚本,完成如下功能

(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;

(2) 如果存在,则显示此设备上的所有分区信息;vim devicetest.sh

#!/bin/bash

#

read -p "Please input a device path:" devicepath

if [ -z $devicepath ];then

echo "Usage: Please input a device path"

exit 1

fi

if [ -b $devicepath ];then

fdisk -l $devicepath

else

echo "No such device"

fi

脚本测试[[email protected] ~]# bash devicetest.sh

Please input a device path:

Usage: Please input a device path

[[email protected] ~]# bash devicetest.sh

Please input a device path:/dev/sdb

No such device

[[email protected] ~]# bash devicetest.sh

Please input a device path:/dev/sda

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000a0ae7

Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048     1026047      512000   83  Linux

/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

4、写一个脚本,完成如下功能

脚本能够接受一个参数;

(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;

(2) 如果参数1为yes,则显示继续执行脚本;

(3) 否则,参数1为其它任意值,均执行非正常退出;vim parametertest.sh

#!/bin/bash

#

read -p "Please input a word(quit|yes):" parameter

while true;do

case $parameter in

quit)

echo "exit the script"

exit 0

;;

yes)

echo "continue to excute the script"

read -p "Please input a word(quit|yes):" parameter

;;

*)

echo "error exit"

exit 1

;;

esac

done

脚本测试[[email protected] ~]# bash parametertest.sh

Please input a word(quit|yes):quit

exit the script

[[email protected] ~]# bash parametertest.sh

Please input a word(quit|yes):yes

continue to excute the script

Please input a word(quit|yes):quit

exit the script

[[email protected] ~]# bash parametertest.sh

Please input a word(quit|yes):

error exit

[[email protected] ~]# bash parametertest.sh

Please input a word(quit|yes):yes

continue to excute the script

Please input a word(quit|yes):

error exit

5、写一个脚本,完成如下功能

传递一个参数给脚本,此参数为gzip、bzip2或者xz三者之一;

(1) 如果参数1的值为gzip,则使用tar和gzip归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.gz;

(2) 如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.bz2;

(3) 如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.xz;

(4) 其它任意值,则显示错误压缩工具,并执行非正常退出;vim compresstest.sh

#!/bin/bash

#

if [ ! -e /backups ];then

mkdir /backups

fi

read -p "Please choose a format of compression(gzip|bzip2|xz):" zip

case $zip in

gzip)

tar -zcf /backups/etc-$(date +%Y%m%d).tar.gz /etc

;;

bzip2)

tar -jcf /backups/etc-$(date +%Y%m%d).tar.bz2 /etc

;;

xz)

tar -Jcf /backups/etc-$(date +%Y%m%d).tar.xz /etc

;;

*)

echo "error compression format"

exit 1

;;

esac

脚本测试[[email protected] ~]# ls /backups

ls: cannot access /backups: No such file or directory

[[email protected] ~]# bash compresstest.sh

Please choose a format of compression(gzip|bzip2|xz):gzip

[[email protected] ~]# bash compresstest.sh

Please choose a format of compression(gzip|bzip2|xz):bzip2

[[email protected] ~]# bash compresstest.sh

Please choose a format of compression(gzip|bzip2|xz):xz

[[email protected] ~]# bash compresstest.sh

Please choose a format of compression(gzip|bzip2|xz):

error compression format

[[email protected] ~]# ls /backups/

etc-20170624.tar.bz2  etc-20170624.tar.gz  etc-20170624.tar.xz

6、写一个脚本,接受一个路径参数:

(1) 如果为普通文件,则说明其可被正常访问;

(2) 如果是目录文件,则说明可对其使用cd命令;

(3) 如果为符号链接文件,则说明是个访问路径;

(4) 其它为无法判断;vim pathtest.sh

#!/bin/bash

#

read -p "Please input a path:" path

if [ -f $path ];then

echo "${path} can be visited"

cat $path

elif [ -d $path ];then

echo "${path} can use ‘cd‘ command"

elif [ -h $path ];then

echo "${path} is a access path"

ls -l $path

else

echo "unknown file"

exit 1

fi

脚本测试[[email protected] ~]# bash pathtest.sh

Please input a path:/etc/fstab

/etc/fstab can be visited

#

# /etc/fstab

# Created by anaconda on Tue Aug  2 21:31:19 2016

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

/dev/mapper/rhel-root   /                       xfs     defaults        1 1

UUID=ce40e87b-854d-42dc-ac50-e1309101c43d /boot                   xfs     defaults        1 2

/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

/dev/cdrom/media/cdromiso9660defaults0 0

[[email protected] ~]# bash pathtest.sh

Please input a path:/dev

/dev can use ‘cd‘ command

[[email protected] ~]# bash pathtest.sh

Please input a path:/dev/cdrom

/dev/cdrom is a access path

lrwxrwxrwx. 1 root root 3 Aug  3  2016 /dev/cdrom -> sr0

[[email protected] ~]# bash pathtest.sh

Please input a path:/hello

unknown file

7、写一个脚本,取得当前主机的主机名,判断

(1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;

(2) 否则,显示现有的主机名即可;vim hostnametest2.sh

#!/bin/bash

#

hostname=$(hostname)

if [ -z $hostname -o $hostname == "localhost" -o $hostname == "none" ];then

hostname "mail.magedu.com" && hostname

else

echo "Current hostname is $hostname"

fi

脚本测试[[email protected] ~]# hostname

mail.magedu.com

[[email protected] ~]# bash hostnametest2.sh

Current hostname is mail.magedu.com

[[email protected] ~]# hostname localhost

[[email protected] ~]# bash hostnametest2.sh

mail.magedu.com

[[email protected] ~]# hostname none

[[email protected] ~]# bash hostnametest2.sh

mail.magedu.com

8、写一脚本,接受一个用户名为参数;

(1) 如果用户的id号为0,则显示其为管理员;

(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;

(3) 否则,则显示其为普通用户;vim usernametest.sh

#!/bin/bash

#

read -p "Please input a username:" username

if [ -z $username ];then

echo "Usage: Please input a username"

exit 1

fi

if  ! id $username &>/dev/null;then

echo "user doesn‘t exist"

else

userid=$(id -u $username)

if [ $userid -eq 0 ];then

echo "$username is a administrator"

elif [ $userid -gt 0 -a $userid -lt 500 ];then

echo "$username is a system user"

else

echo "$username is a normal user"

fi

fi

脚本测试[[email protected] ~]# bash usernametest.sh

Please input a username:

Usage: Please input a username

[[email protected] ~]# bash usernametest.sh

Please input a username:jack

user doesn‘t exist

[[email protected] ~]# bash usernametest.sh

Please input a username:root

root is a administrator

[[email protected] ~]# bash usernametest.sh

Please input a username:postfix

postfix is a system user

[[email protected] ~]# useradd yuki

[[email protected] ~]# bash usernametest.sh

Please input a username:yuki

yuki is a normal user

代做linux作业,Linux系统管理第七周作业【Linux微职位】(示例代码)相关推荐

  1. Linux系统管理第七周作业【Linux微职位】

    1.写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin):分别这两类用户的个数:通过字符串比较来实现: [root@localhos ...

  2. 厚基础Linux——第七周作业

    文章目录 厚基础Linux--第七周作业 1.自建yum仓库,分别为网络源和本地源 环境规划 技术要求 需求分析 server端 client端 操作步骤 系统安装 server操作步骤 clinet ...

  3. 2019年春季学期第七周作业

    A Q 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 2019春第七周作业 我在这个课程的目标是 尽可能了解并且应用指针及文件和数组的知识 这个作业在那个具体方面帮助我实现目标 大致掌 ...

  4. 20189200余超 2018-2019-2 移动平台应用开发实践第七周作业

    20189200余超 2018-2019-2 移动平台应用开发实践第七周作业 布局 在这一节中首先学习了java的页面布局,在此基础之上来进行了编程. 图片如下: 代码如下: *** 使用代码进行登录 ...

  5. 学号20189220余超 2018-2019-2 《密码与安全新技术专题》第七周作业

    学号20189220 余超 2018-2019-2 <密码与安全新技术专题>第七周作业--论文学习及报告总结 课程:<密码与安全新技术专题> 班级: 1892 姓名: 余超 学 ...

  6. 2017-2018-2 20179215《密码与安全新技术》第七周作业

    2017-2018-2 20179215 <密码与安全新技术> 第七周作业 课程:<密码与安全新技术> 班级: 201792 姓名: 袁琳 学号:20179215 上课教师:谢 ...

  7. python第七周答案_马哥2016全新Linux+Python高端运维班第七周作业

    1.创建一个10G分区,并格式为ext4文件系统: [root@localhost ~]# fdisk /dev/sdb 欢迎使用 fdisk (util-linux 2.23.2). 更改将停留在内 ...

  8. 2019春第七周作业

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 要求我们掌握指针,可以更好的运用它 我在这个课程的目标是  学懂C语言,会编程一些简单的小应用 这个作业在那个具体方面帮助我实现目标  这 ...

  9. mooc c语言第三周作业,2017moocC语言第七周答案

    <2017moocC语言第七周答案>由会员分享,可在线阅读,更多相关<2017moocC语言第七周答案(5页珍藏版)>请在读根文库上搜索. 1.n层嵌套平方根的计算(4分)题目 ...

  10. 20162302 第七周作业

    20162302 2016-2017-2 <程序设计与数据结构>第七周学习总结 教材学习总结 复习以前的内容,修正代码中的错误 学习多态的用法 学习接口的相关内容,并创建多态引用 教材学习 ...

最新文章

  1. 【Python学习系列十七】基于scikit-learn库逻辑回归训练模型(delta比赛代码2)
  2. 谷歌发布全新TensorFlow库“tf.Transform” 简化机器学习数据预处理过程
  3. leetcode 268. 丢失的数字(Java版)
  4. iphone屏幕突然变暗_iPhone或iPad在使用中发热厉害吗?以下是原因和解决方法
  5. NXP S32K144开发(一)环境搭建和新建工程
  6. BGP 路由属性 公认可选 LOCAL_PREF
  7. 函数凸性与Jensen不等式
  8. 计算机键盘上删除,电脑键盘删除键是哪一个
  9. Linux截图gif,Ubuntu下截图与录制视频并作成gif图片
  10. C语言求x个电阻并联的和的程序,C语言 计算并联电阻的阻值
  11. Python项目 huobi量化交易系统
  12. 高考早知道:自主招生,能用低分读名校,就别再拼高分挤独木桥
  13. 剑指offer_3 -- 构建乘积数组
  14. SQL Server笔记心得(持续更新)
  15. python_操作linux上的mysql
  16. STM32驱动硬件MG90S舵机
  17. 苹果系统地图定位服务器,苹果手机能1秒定位对象在哪?你知道吗?
  18. 520表白html实现3D动态相册,换成女朋友照片
  19. 哪些飞机机型是安全、可信赖的
  20. 我写的一个简单触发器,实现计票功能

热门文章

  1. 华为云 搭建 Zabbix监控服务
  2. could not initialize javavm mysql_Could not initialize JavaVM
  3. ZFM_RFC_FIDOC-创建财务凭证-BAPI_ACC_DOCUMENT_CHECK/BAPI_ACC_DOCUMENT_POST/POSTING_INTERFACE_DOCUMENT
  4. 汽车高级驾驶辅助系统ADAS全盘点
  5. python假设检验
  6. autoCAD2010裁剪工具使用
  7. 新浪登陆按钮三种方式解析
  8. 【运动学】基于matlab EKF姿态估计【含Matlab源码 1638期】
  9. 考研408.计算机网络.特殊IP地址的记忆方法
  10. CVE-2020-1938 /CNVD-2020-10487漏洞调试