广告
返回顶部
首页 > 资讯 > 操作系统 >megeedu Linux+Python
  • 383
分享到

megeedu Linux+Python

megeeduLinuxPython 2023-01-31 06:01:41 383人浏览 独家记忆
摘要

本周作业内容:1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;[root@localhost ~]# cp /etc/rc.d/rc.sysi

本周作业内容:

1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;

[root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp/    #复制文件
[root@localhost ~]# vim /tmp/rc.sysinit          #编辑文件
:%s/^[[:space:]]/#&/                      #末行模式下查找替换

替换结果对比:

wKioL1fX_FiQuHj2AAHX6HF4eLU896.jpg-wh_50

2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;

[root@localhost ~]# cp /boot/grub/grub.conf /tmp/    #复制文件
[root@localhost ~]# vim /tmp/grub.conf               #vim编辑文件
:%s/^[[:space:]]\+//                                 #末行模式查找替换,不指定替换内容即为删除

删除结果对比:

wKiom1fX_nHw4bEYAADKWlj_T90258.jpg-wh_50

3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符

[root@localhost ~]# vim /tmp/rc.sysinit                         #vim 编辑文件   
:%s/^#[[:space:]]\+//                                           #末行模式查找替换

删除结果对比:

wKioL1fYAFfBuF49AACy923S7I0442.jpg-wh_50

4、为/tmp/grub.conf文件中前三行的行首加#号;

[root@localhost ~]# vim /tmp/grub.conf         #vim 编辑文件
:1,3s/^/#&/                                    #末行模式查找替换,1,3指定搜索范围,
:w /tmp/grub.conf.bak                          #另存为 /tmp/grub.conf.bak 
[root@localhost ~]# vimdiff /tmp/grub.conf /tmp/grub.conf.bak     #vimdiff 对比两个文件

对比结果:

wKioL1fYAlrgMYLaAADLbZsVbZQ316.jpg-wh_50

5、将/etc/yum.repos.d/Centos-Media.repo文件中所有的enabled=0或gpGCheck=0的最后的0修改为1;

[root@localhost ~]# cp /etc/yum.repos.d/CentOS-Media.repo /tmp/    #复制文件
[root@localhost ~]# vim /tmp/CentOS-Media.repo                     #vim 编辑文件
:%s/enabled=0/enabled=1/g                                          #全局查找替换
:%s/gpgcheck=0/gpgcheck=1/g

替换前后对比:

wKiom1fYBQWD6oPpAADE4JYSCP4819.jpg-wh_50

6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201608300202

[root@localhost ~]# mkdir /backup
[root@localhost ~]# crontab -e
0 */4  * * * /bin/cp /etc /backup/etc-`date +%Y%m%d%H%M`

7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20160830

[root@localhost ~]# crontab -e
0 0 * * 2,4,6 /bin/cp -a /var/log/messages /backup/messages_logs/messages-`date +%Y%m%d`

8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中

[root@localhost ~]# mkdir /stats
[root@localhost ~]# crontab -e
0 */2 * * * /bin/grep '^S' /proc/meminfo > /stats/memory.txt

9、工作日的工作时间内,每两小时执行一次echo "howdy"

[root@localhost ~]# crontab -e
0 8-17/2 * * 1,2,3,4,5  /bin/echo "howdy"        #1-5为工作日,8-17为工作时间

脚本编程练习

10、创建目录/tmp/testdir-当前日期时间;

[root@localhost ~]# vim /MK-testdir.sh
#!/bin/bash
mkdir /tmp/testdir-`date +%Y%m%d%H%M`

[root@localhost ~]# chmod +x /MK-testdir.sh 
[root@localhost ~]# ll /MK-testdir.sh 
-rwxr-xr-x. 1 root root 54 9月   9 10:41 /MK-testdir.sh
[root@localhost ~]# /MK-testdir.sh
[root@localhost ~]# ls /tmp/
3    CentOS-Media.repo  maxusers.txt  testdir-201609091042
3]   crontab.RT2uVE     mylinux       tfile-2016-08-12-17-01-01
a_c  etc.conf           mytest1       yum.log
a_d  etc.test           mytest2       yum_save_tx-2016-08-01-01-55m_XEdJ.yumtx
b_c  grub.conf          mytest3
b_d  grub.conf.bak      rc.sysinit
[root@localhost ~]#

11、在此目录创建100个空文件:file1-file100

[root@localhost tmp]# vim /MK-testdir.sh 
#!/bin/bash
dir=/tmp/testdir-`date +%Y%m%d%H%M`
mkdir $dir
if [ $? -eq 0 ]; then      #判断之前目录是否创建成功,若创建成功则进入循环创建文件file1-file100
  for i in {1..100};do
    touch $dir/file$i
  done
  echo 'touch file1-file100 success.'
else
  echo 'touch file1-file100 failed.'
fi
[root@localhost /]# ./MK-testdir.sh 
touch file1-file100 success.
[root@localhost /]# ll /tmp/testdir-201609091148
总用量 0
-rw-r--r--. 1 root root 0 9月   9 11:48 file1
-rw-r--r--. 1 root root 0 9月   9 11:48 file10
-rw-r--r--. 1 root root 0 9月   9 11:48 file100
-rw-r--r--. 1 root root 0 9月   9 11:48 file11
......

12、显示/etc/passwd文件中位于第偶数行的用户的用户名;

[root@localhost /]# vim test-6-12.sh 
#!/bin/bash
cat -n /etc/passwd | sed -n 'n;p' | cut -d: -f1
[root@localhost /]# ./test-6-12.sh 
     2    hadoop
     4    daemon
     6    lp
     8    shutdown
.........

13、创建10用户user10-user19;密码同用户名;

[root@localhost work]# vim test-6.13.sh 
#!/bin/bash
#
for i in {10..19};do                  
  if id user$i &> /dev/null; then          
    echo "user$i exists."                
  else
    useradd user$i &> /dev/null      
     if [ $? -eq 0 ]; then              
        echo "user$i" | passwd --stdin user$i &> /dev/null
        echo "add user$i success."    
     fi
  fi
done
[root@localhost work]# ./test-6.13.sh 
Add user10 success.
Add user11 success.
Add user12 success.
Add user13 success.
Add user14 success.
Add user15 success.
Add user16 success.
Add user17 success.
Add user18 success.
Add user19 success.

14、在/tmp/创建10个空文件file10-file19;

[root@localhost work]#vim test-6.14.sh 
#!/bin/bash

for i in {10..19};do                  
  touch /tmp/file$i
    if [ $? -eq 0 ];then
       echo "file$i touch success."
    fi
done
[root@localhost work]# ./test-6.14.sh 
file10 touch success.
file11 touch success.
file12 touch success.
file13 touch success.
file14 touch success.
file15 touch success.
file16 touch success.
file17 touch success.
file18 touch success.
file19 touch success.

15、把file10的属主和属组改为user10,依次类推。

[root@localhost work]#vim test-6.15.sh
#!/bin/bash
#
for i in {10..19};do                       
    if [ -e /tmp/file$i ];then             
       chown user$i:user$i /tmp/file$i &> /dev/null  
    else 
       echo "No Find The file."             
    fi
done

[root@localhost work]# ./test-6.15.sh 
[root@localhost work]# ll /tmp/ | grep user
-rw-r--r--.  1 user10 user10     0 9月   9 12:30 file10
-rw-r--r--.  1 user11 user11     0 9月   9 12:30 file11
-rw-r--r--.  1 user12 user12     0 9月   9 12:30 file12
-rw-r--r--.  1 user13 user13     0 9月   9 12:30 file13
-rw-r--r--.  1 user14 user14     0 9月   9 12:30 file14
-rw-r--r--.  1 user15 user15     0 9月   9 12:30 file15
-rw-r--r--.  1 user16 user16     0 9月   9 12:30 file16
-rw-r--r--.  1 user17 user17     0 9月   9 12:30 file17
-rw-r--r--.  1 user18 user18     0 9月   9 12:30 file18
-rw-r--r--.  1 user19 user19     0 9月   9 12:30 file19
-rw-r--r--.  1 root   root     854 8月  19 18:07 maxusers.txt
[root@localhost work]#


--结束END--

本文标题: megeedu Linux+Python

本文链接: https://www.lsjlt.com/news/189495.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • megeedu Linux+Python
    本周作业内容:1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;[root@localhost ~]# cp /etc/rc.d/rc.sysi...
    99+
    2023-01-31
    megeedu Linux Python
  • megeedu Linux+Pytho
    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。[root@localhost ~]# who root     pts/0        2016-08-19 17:46 (172.16.168....
    99+
    2023-01-31
    megeedu Linux Pytho
  • [Python]linux python
     #!/usr/bin/env python与#!/usr/bin/python的区别 脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它,就这么简单   #!/usr/bin/python是告诉操作系统执行...
    99+
    2023-01-31
    Python linux python
  • Linux Python
    python2.7.X下载安装1.查看python的版本[root@zyl ~]# python -VPython 2.6.62.下载Python-2.7.8[root@zylpython]#wgethttp://python.org/ft...
    99+
    2023-01-31
    Linux Python
  • Python MySQLdb Linux
             本文介绍了Python MySQLdb Linux下安装笔记,本文分别讲解了快速安装和手动编译安装两种方法,并分别讲解了操作步骤,需要的朋友可以参考下      主要针对centos6.5 64位系统           ...
    99+
    2023-01-31
    Python MySQLdb Linux
  • Linux安装Python
        Linux下默认系统自带python2.6的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装python3和python2共存    首先进...
    99+
    2023-01-31
    Linux Python
  • python nohup linux 后
    遇到问题 nohup python flush.py & 这样运行,生成了nohup.out文件,但是内容始终是空的,试了半天也不行。浪费了不少时间。 原因 python的输出又缓冲,导致out.log并不能够马上看到输...
    99+
    2023-01-31
    python nohup linux
  • Python安装 linux
            上一篇文章中我们演示了Python在windows环境下的安装流程,本文就介绍一下python在Linux环境下是如何安装的。         服务器环境:Linux CentOS 7.4         Python版本:...
    99+
    2023-08-31
    服务器 linux python
  • linux+ubuntu解决python
    ubuntu版本: 16.04.2 LTS 内置python版本: Python 2.7.12 和 python python3.5 需求:升级python3 以及使用 下载安装 源码安装包 官网https://www....
    99+
    2023-01-31
    linux ubuntu python
  • python 查看Linux cront
    #encoding:utf-8#!/usr/local/bin/pythonfrom crontab import CronTabimport sys,refrom croniter import croniterfrom datetime...
    99+
    2023-01-31
    python Linux cront
  • linux 安装python djang
    linux 下python django环境安装安装基础环境centos 7安装 Nginx在本教程中,我们使用 Nginx 作为 Web 服务器。执行如下命令来安装 nginxyum install nginx安装完成后,执行如下命令来启...
    99+
    2023-01-31
    linux python djang
  • linux-python安装-Pytho
    安装环境:系统:centos6.5-web服务版 安装好之后执行升级: yum -y update用户权限:root管理员权查看python的版本python -V   #系统自带版本下载直接下载:wget http://python.or...
    99+
    2023-01-31
    linux python Pytho
  • Linux下安装Python
    一般的Linux上都有默认的Python版本,CentOS6.5默认的Python版本的2.6.6的,因为工作原因,这里需要用到Python3.6.3的版本,在这里,小编将会一步步的教大家进行再Linux下Python3的安装。   一、...
    99+
    2023-01-30
    Linux Python
  • Linux-Python-Scapy的T
    TCP 连接扫描:客户端与服务器建立 TCP 连接要进行一次三次握手,如果进行了一次成功的三次握手,则说明端口开放; TCP SYN 扫描(也称为半开放扫描或stealth扫描):这个技术同 TCP 连接扫描非常相似。同样是客户端向服务器...
    99+
    2023-01-31
    Linux Python Scapy
  • linux怎么写python
    这篇文章主要介绍“linux怎么写python”,在日常操作中,相信很多人在linux怎么写python问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”linux怎么写python”的疑惑有所帮助!接下来,请跟...
    99+
    2023-06-29
  • linux离线安装python
    一、服务器环境 系统:CentOS 7 用户:root Python版本:python3.8.5 二、安装步骤 1,找个联网的计算机,下载安装包和依赖 python下载地址:https://www.python.org/ftp/python...
    99+
    2023-09-18
    linux python 服务器
  • Python学习—linux下Pytho
    通常将Python安装在/usr/local/python3(具体安装位置看个人喜好,但是要记住安装的位置),因为/usr/local目录下本不存在目录python3,所以先新建目录: mkdir /usr/local/python3 1....
    99+
    2023-01-31
    Python linux Pytho
  • python获取Linux信息
      刚开始学习Python,用Python写了一个获取Linux服务器信息的脚本,在debian和centos上测试通过。首先需要安装一个psutil库,在安装psutil之前需要安装python的开发工具包#debian  apt-get...
    99+
    2023-01-31
    信息 python Linux
  • Linux 源码安装Python
    下载源码tar包下载地址:https://www.python.org/downloads/我这里下载的 Python-2.7.11.tgz# tar -zxvf Python-2.7.11.tgz进入解压缩后的文件夹# cd Python...
    99+
    2023-01-31
    源码 Linux Python
  • linux下的python升级
    不用卸载,先试试命令:yum update python。   行了就OK。不行看下面:   先下载源码包,任意一个即可: (1)gzip-compressed源码:Python-2.5.4.tgz (2)bzip2-compressed源...
    99+
    2023-01-31
    linux python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作