; N26-第六周博客 | Linux运维部落

N26-第六周博客

vim编辑器及简单shell脚本示例

请详细总结vim编辑器的使用并完成以下练习题 
1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;


1
[root@localhost tmp]<span class="hljs-comment"># vim rc.sysinit</span><br />:%s@^[[:space:]]\+[^[:space:]]@<span class="hljs-comment">#&amp;@g</span><br />

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


1
[root@localhost tmp]<span class="hljs-comment"># vim grub.conf</span><br />:%s@^[[:space:]]\+@@<br />

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


1
[root@localhost tmp]<span class="hljs-comment"># vim rc.sysinit</span><br />:%s@^<span class="hljs-comment">#[[:space:]]\+@@</span><br />

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


1
[root@localhost tmp]<span class="hljs-comment"># vim grub.conf</span><br />:<span class="hljs-number">1</span>,<span class="hljs-number">3</span>s@^@<span class="hljs-comment">#@</span><br />

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


1
[root@localhost /]<span class="hljs-comment"># vim /etc/yum.repos.d/CentOS-Media.repo</span><br />:%s@\(enabled\|gpgcheck\)=<span class="hljs-number">0</span>@\<span class="hljs-number">1</span>=<span class="hljs-number">1</span>@g <br />

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


1
[root@localhost ~]<span class="hljs-comment"># crontab -e</span><br /><span class="hljs-number">0</span> */<span class="hljs-number">4</span> * * * /bin/tar -zcf /backup/etc-$(date +\%Y\%m\%d\%H\%M).tar.gz /etc<br />

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


1
[root@localhost ~]<span class="hljs-comment"># crontab -e</span><br /><span class="hljs-number">0</span> <span class="hljs-number">0</span> * * <span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">6</span> /bin/tar -zcf /backup/messages_logs/messages-$(date +\%Y\%m\%d).tar.gz /var/<span class="hljs-built_in">log</span>/messages<br />

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


1
[root@localhost ~]<span class="hljs-comment"># crontab -e</span><br /><span class="hljs-number">0</span> */<span class="hljs-number">2</span> * * * /bin/grep <span class="hljs-string">&quot;^S&quot;</span> /proc/meminfo &gt;&gt; /stats/memory.txt<br />

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


1
[root@localhost ~]<span class="hljs-comment"># crontab -e</span><br /><span class="hljs-number">0</span> <span class="hljs-number">9</span>-<span class="hljs-number">18</span>/<span class="hljs-number">2</span> * * * <span class="hljs-number">1</span>-<span class="hljs-number">5</span> /bin/<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;howdy&quot;</span> &gt;&gt; /howdy.log<br />

脚本编程练习 
10、创建目录/tmp/testdir-当前日期时间;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br />mkdir /tmp/testdir-$(date +%F-%H-%M)<br />

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br />touch /tmp/file{<span class="hljs-number">0</span>..<span class="hljs-number">100</span>}<br />

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br />sed -n <span class="hljs-string">&#039;n;p&#039;</span> /etc/passwd | cut <span class="hljs-operator" style="line-height: 1.6">-d</span>: <span class="hljs-operator" style="line-height: 1.6">-f</span> <span class="hljs-number">1</span><br />

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> {<span class="hljs-number">24</span>,<span class="hljs-number">25</span>};<span class="hljs-keyword">do</span><br /> &nbsp;<span class="hljs-keyword">if</span> <br /> &nbsp; &nbsp; &nbsp; ! id user<span class="hljs-variable" style="line-height: 1.6">$num</span> &amp;&gt; /dev/null;<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp;useradd user<span class="hljs-variable" style="line-height: 1.6">$num</span><br /> &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;user<span class="hljs-variable">$num</span>&quot;</span> | passwd --stdin user<span class="hljs-variable" style="line-height: 1.6">$num</span><br /> &nbsp;<span class="hljs-keyword">else</span><br /> &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;the user<span class="hljs-variable">$num</span> is exists&quot;</span><br /> &nbsp;<span class="hljs-keyword">fi</span><br /><span class="hljs-keyword">done</span><br />

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> {<span class="hljs-number">10</span>..<span class="hljs-number">19</span>};<span class="hljs-keyword">do</span><br /><span class="hljs-keyword">if</span><br /> &nbsp; [ ! <span class="hljs-operator" style="line-height: 1.6">-e</span> file<span class="hljs-variable" style="line-height: 1.6">$num</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; touch /tmp/file<span class="hljs-variable" style="line-height: 1.6">$num</span><br /><span class="hljs-keyword">else</span><br /> &nbsp; <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;the file<span class="hljs-variable">$num</span> is exists&quot;</span> &nbsp;<br /><span class="hljs-keyword">fi</span><br /><span class="hljs-keyword">done</span><br />

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> {<span class="hljs-number">10</span>..<span class="hljs-number">19</span>};<span class="hljs-keyword">do</span><br /> &nbsp; chown user<span class="hljs-variable" style="line-height: 1.6">$num</span>:user<span class="hljs-variable" style="line-height: 1.6">$num</span> /tmp/file<span class="hljs-variable" style="line-height: 1.6">$num</span><br /><span class="hljs-keyword">done</span><br />

 

原创文章,作者:youngfer,如若转载,请注明出处:/68892

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论列表(1条)

  • 马哥教育
    马哥教育 2017-04-10 16:25

    总结的很好,脚本的思路清晰,加油~~~~

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1660809109@qq.com

工作时间:周一至周五,9:30-18:30,节假日同时也值班

友情链接:测试  万达娱乐招商  万达登录  万达开户  万达娱乐注册  万达娱乐主管  万达注册  万达娱乐招商  guoqibee.com  guoqibee.com