; 马哥教育网络班22期+第九周课程练习 | Linux运维部落

马哥教育网络班22期+第九周课程练习

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


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-built_in">declare</span> <span class="hljs-operator" style="line-height: 1.6">-a</span> loginuser<br /><span class="hljs-built_in">declare</span> -i sum_login=<span class="hljs-number">0</span><br /><span class="hljs-built_in">declare</span> -i sum_nologin=<span class="hljs-number">0</span><br />list=($(cat /etc/passwd |awk -F: <span class="hljs-string">&#039;{print $NF}&#039;</span>))<br /><br /><span class="hljs-keyword">for</span> ((i=<span class="hljs-number">1</span>;i&lt;=<span class="hljs-variable" style="line-height: 1.6">${#list[*]}</span>;i++));<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">if</span> [[ <span class="hljs-string">&quot;<span class="hljs-variable">${list[i]}</span>&quot;</span> == <span class="hljs-string">&quot;/sbin/nologin&quot;</span> ]];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">let</span> sum_nologin=<span class="hljs-variable" style="line-height: 1.6">$sum_nologin</span>+<span class="hljs-number">1</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">else</span> [[ <span class="hljs-string">&quot;<span class="hljs-variable">${list[i]}</span>&quot;</span> == <span class="hljs-string">&quot;/bin/bash&quot;</span> ]]<br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">let</span> sum_login=<span class="hljs-variable" style="line-height: 1.6">$sum_login</span>+<span class="hljs-number">1</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">fi</span><br /><span class="hljs-keyword">done</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;no login users is <span class="hljs-variable">$sum_nologin</span>&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;login users is <span class="hljs-variable">$sum_login</span>&quot;</span><br />

2. 写一个脚本 
(1) 获取当前主机的主机名,保存于hostname变量中; 
(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com; 
(3) 否则,则显示当前主机名;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br />hostname=$(hostname)<br /><span class="hljs-keyword">if</span> [[ <span class="hljs-variable" style="line-height: 1.6">$hostname</span> == <span class="hljs-string">&quot;localhost&quot;</span> ]];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;hostname wwww.magedu.com<br /><span class="hljs-keyword">else</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-variable" style="line-height: 1.6">$hostname</span><br /><span class="hljs-keyword">fi</span><br />

3. 写一个脚本,完成如下功能 
(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在; 
(2) 如果存在,则显示此设备上的所有分区信息;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-keyword">if</span> [ <span class="hljs-variable" style="line-height: 1.6">$#</span> <span class="hljs-operator" style="line-height: 1.6">-lt</span> <span class="hljs-number">1</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Usage:`basename <span class="hljs-variable">$0</span>` devicename&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">exit</span> <span class="hljs-number">3</span><br /><span class="hljs-keyword">fi</span><br /><br /><span class="hljs-keyword">if</span> [ <span class="hljs-operator" style="line-height: 1.6">-e</span> <span class="hljs-variable" style="line-height: 1.6">$1</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;fdisk <span class="hljs-operator" style="line-height: 1.6">-l</span> <span class="hljs-variable" style="line-height: 1.6">$1</span><br /><span class="hljs-keyword">else</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Without this device&quot;</span><br /><span class="hljs-keyword">fi</span><br />

4. 写一个脚本,完成如下功能 
脚本能够接受一个参数; 
(1) 如果参数1为quit,则显示退出脚本,并执行正常退出; 
(2) 如果参数1为yes,则显示继续执行脚本; 
(3) 否则,参数1为其它任意值,均执行非正常退出;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-built_in">read</span> -p <span class="hljs-string">&quot;Please input your choice:&quot;</span> option<br /><br /><span class="hljs-keyword">while</span> [[ <span class="hljs-variable" style="line-height: 1.6">$option</span> == <span class="hljs-string">&quot;yes&quot;</span> ]];<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">read</span> -p <span class="hljs-string">&quot;Please input your choice again:&quot;</span> option<br /><span class="hljs-keyword">done</span><br /><br /><span class="hljs-keyword">if</span> [[ <span class="hljs-variable" style="line-height: 1.6">$option</span> == <span class="hljs-string">&quot;quit&quot;</span> ]];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Quit&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">exit</span> <span class="hljs-number">0</span> <br /><span class="hljs-keyword">else</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Error&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">exit</span> <span class="hljs-number">3</span><br /><span class="hljs-keyword">fi</span><br />

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) 其它任意值,则显示错误压缩工具,并执行非正常退出;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-keyword">if</span> [ <span class="hljs-variable" style="line-height: 1.6">$#</span> <span class="hljs-operator" style="line-height: 1.6">-lt</span> <span class="hljs-number">1</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Usage:`basename <span class="hljs-variable">$0</span>` {gzip|bzip2|xz}&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">exit</span> <span class="hljs-number">3</span><br /><span class="hljs-keyword">fi</span><br /><br /><span class="hljs-keyword">if</span> [ <span class="hljs-operator" style="line-height: 1.6">-d</span> /backups ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">return</span> <br /><span class="hljs-keyword">else</span><br /> &nbsp; &nbsp;mkdir /backups &amp;&gt;/dev/null<br /><span class="hljs-keyword">fi</span><br /><br /><span class="hljs-keyword">case</span> <span class="hljs-variable" style="line-height: 1.6">$1</span> <span class="hljs-keyword">in</span><br />gzip)<br />tar -zcvf /backups/etc-$(date <span class="hljs-string">&quot;+%Y%m<span class="hljs-variable">$d</span>&quot;</span>).tar.gz /etc<br />;;<br />bzip2)<br />tar -jcvf /backups/etc-$(date <span class="hljs-string">&quot;+%Y%m<span class="hljs-variable">$d</span>&quot;</span>).tar.bz2 /etc<br />;;<br />xz)<br />tar -Jcvf /backups/etc-$(date <span class="hljs-string">&quot;+%Y%m<span class="hljs-variable">$d</span>&quot;</span>).tar.xz /etc<br />;;<br />*)<br /><span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Unkown&quot;</span><br />;;<br /><span class="hljs-keyword">esac</span><br />

6. 写一个脚本,接受一个路径参数: 
(1) 如果为普通文件,则说明其可被正常访问; 
(2) 如果是目录文件,则说明可对其使用cd命令; 
(3) 如果为符号链接文件,则说明是个访问路径; 
(4) 其它为无法判断;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-keyword">if</span> [ <span class="hljs-variable" style="line-height: 1.6">$#</span> <span class="hljs-operator" style="line-height: 1.6">-lt</span> <span class="hljs-number">1</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Usage:`basename <span class="hljs-variable">$0</span>` Path1&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">exit</span> <span class="hljs-number">3</span><br /><span class="hljs-keyword">fi</span><br /><br /><span class="hljs-keyword">if</span> [[ <span class="hljs-operator" style="line-height: 1.6">-f</span> <span class="hljs-variable" style="line-height: 1.6">$1</span> ]];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;This is a common file&quot;</span><br /><span class="hljs-keyword">elif</span> [[ <span class="hljs-operator" style="line-height: 1.6">-d</span> <span class="hljs-variable" style="line-height: 1.6">$1</span> ]];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;This is a directory.&quot;</span><br /><span class="hljs-keyword">elif</span> [[ -L <span class="hljs-variable" style="line-height: 1.6">$1</span> ]];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;This is a Links file&quot;</span><br /><span class="hljs-keyword">else</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Unknow&quot;</span><br /><span class="hljs-keyword">fi</span><br />

7. 写一个脚本,取得当前主机的主机名,判断 
(1) 如果主机名为空或为localhost,或为”“(none)”“,则将其命名为mail.magedu.com; 
(2) 否则,显示现有的主机名即可;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-keyword">if</span> [ <span class="hljs-string">&quot;<span class="hljs-variable">$(hostname)</span>&quot;</span> == <span class="hljs-string">&quot;&quot;</span> -o <span class="hljs-string">&quot;<span class="hljs-variable">$(hostname)</span>&quot;</span> == <span class="hljs-string">&quot;localhost&quot;</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;hostname <span class="hljs-string">&quot;mail.magedu.com&quot;</span><br /><span class="hljs-keyword">else</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;<span class="hljs-variable">$(hostname)</span>&quot;</span><br /><span class="hljs-keyword">fi</span><br />

8. 写一脚本,接受一个用户名为参数; 
(1) 如果用户的id号为0,则显示其为管理员; 
(2) 如果用户的id号大于0且小于500, 则显示其为系统用户; 
(3) 否则,则显示其为普通用户;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-keyword">if</span> [ <span class="hljs-variable" style="line-height: 1.6">$#</span> <span class="hljs-operator" style="line-height: 1.6">-lt</span> <span class="hljs-number">1</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Usage:`basename <span class="hljs-variable">$0</span>` USERNAME&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">exit</span> <span class="hljs-number">3</span><br /><span class="hljs-keyword">fi</span><br /><br /><span class="hljs-keyword">if</span> ! id -u <span class="hljs-variable" style="line-height: 1.6">$1</span> &amp;&gt;/dev/null ;<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;User <span class="hljs-variable">$1</span> is not exists.&quot;</span><br /><span class="hljs-keyword">else</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">if</span> [ `id -u <span class="hljs-variable" style="line-height: 1.6">$1</span>` <span class="hljs-operator" style="line-height: 1.6">-eq</span> <span class="hljs-number">0</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;User <span class="hljs-variable">$1</span> is admin user&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">elif</span> [ `id -u <span class="hljs-variable" style="line-height: 1.6">$1</span>` <span class="hljs-operator" style="line-height: 1.6">-lt</span> <span class="hljs-number">500</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;User <span class="hljs-variable">$1</span> is system user&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;User <span class="hljs-variable">$1</span> is common user&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">fi</span><br /><span class="hljs-keyword">fi</span><br />

10. 写一个脚本,传递一个用户名参数给脚本; 
(1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串; 
(2) 否则,则显示无法登录系统;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br /><span class="hljs-keyword">if</span> [ <span class="hljs-variable" style="line-height: 1.6">$#</span> <span class="hljs-operator" style="line-height: 1.6">-lt</span> <span class="hljs-number">1</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Usage:`basename <span class="hljs-variable">$0</span>` USERNAME&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">exit</span> <span class="hljs-number">3</span><br /><span class="hljs-keyword">fi</span><br /><br /><span class="hljs-keyword">if</span> ! id -u <span class="hljs-variable" style="line-height: 1.6">$1</span> &amp;&gt;/dev/null ;<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;User <span class="hljs-variable">$1</span> is not exists&quot;</span><br /><span class="hljs-keyword">else</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">if</span> [ <span class="hljs-string">&quot;`id -u <span class="hljs-variable">$1</span>`&quot;</span> -ge <span class="hljs-string">&quot;500&quot;</span> <span class="hljs-operator" style="line-height: 1.6">-a</span> <span class="hljs-string">&quot;`cat /etc/passwd |grep <span class="hljs-variable">$1</span> |grep -o &nbsp;sh$`&quot;</span> == <span class="hljs-string">&quot;sh&quot;</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;A user can log system&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Can not login to the system&quot;</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">fi</span><br /><span class="hljs-keyword">fi</span><br />

11. 写一个脚本,完成如下任务 : 
(1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中; 
(2) 复制目录时,才使用cp -r命令; 
(3) 复制文件时使用cp命令; 
(4) 复制链接文件时使用cp -d命令; 
(5) 余下的所有类型,使用cp -a命令;


1
<span class="hljs-shebang">#!/bin/bash</span><br /><span class="hljs-comment">#</span><br />file=/var/<span class="hljs-built_in">log</span>/*<br /><br /><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-variable" style="line-height: 1.6">$file</span>;<span class="hljs-keyword">do</span><br /> &nbsp; &nbsp;<span class="hljs-keyword">if</span> [ <span class="hljs-operator" style="line-height: 1.6">-d</span> <span class="hljs-variable" style="line-height: 1.6">$i</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;cp -r <span class="hljs-variable" style="line-height: 1.6">$i</span> /tmp/<span class="hljs-built_in">test</span>1-testn<br /> &nbsp; &nbsp;<span class="hljs-keyword">elif</span> [ <span class="hljs-operator" style="line-height: 1.6">-f</span> <span class="hljs-variable" style="line-height: 1.6">$i</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;cp <span class="hljs-variable" style="line-height: 1.6">$i</span> /tmp/<span class="hljs-built_in">test</span>1-testn<br /> &nbsp; &nbsp;<span class="hljs-keyword">elif</span> [ -L <span class="hljs-variable" style="line-height: 1.6">$i</span> ];<span class="hljs-keyword">then</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;cp <span class="hljs-operator" style="line-height: 1.6">-d</span> <span class="hljs-variable" style="line-height: 1.6">$i</span> /tmp/<span class="hljs-built_in">test</span>1-testn<br /> &nbsp; &nbsp;<span class="hljs-keyword">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;cp <span class="hljs-operator" style="line-height: 1.6">-a</span> <span class="hljs-variable" style="line-height: 1.6">$i</span> /tmp/<span class="hljs-built_in">test</span>1-testn<br /> &nbsp; &nbsp;<span class="hljs-keyword">fi</span><br /><br /><span class="hljs-keyword">done</span>

原创文章,作者:N22-白蚁,如若转载,请注明出处:/65482

评论列表(1条)

  • 马哥教育
    马哥教育 2017-01-04 16:08

    脚本写的很棒,排版也很漂亮,但是注意,不要使用系统的命令来作为变量名,加油

联系我们

400-080-6560

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

邮件:1660809109@qq.com

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

友情链接:万达招商  万达招商  万达直属QQ  万达主管  华宇招商  万达娱乐直属  万达直属  万达登录  万达主管QQ