合肥工业大学Linux实验三Linux 下的 shell 编程

Posted 上衫_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了合肥工业大学Linux实验三Linux 下的 shell 编程相关的知识,希望对你有一定的参考价值。

一、实验目的

理解 shell 程序设计方法,掌握 bash 下的基本控制结构,熟悉 shell 程序的编
辑、运行和调试方法。

二、实验任务和要求

  1. 写一个 shell 程序,为系统创建 20 个用户账号,分别是 hfuter1~hfuter20,同
    时设置不同的随机数密码,并将不同的用户名和密码保存在以用户账号为名的文件
    中。
  2. 写一个 shell 脚本 catmn.sh,输入一个文本文件,起始和结束行号,要求能够显示
    该文本文件从开始到结束行号之间的内容。使用语法举例——显示 passwd 文件的
    3 至 7 行内容:
    $ ./catmn.sh /etc/passwd 3 7

三、实验步骤和实验结果

1)写一个 shell 程序,为系统创建 20 个用户账号

  1. 编写shell脚本如下
#!/bin/bash                                                                     
index=1
while(($index<21))
do
   username=hfuter$index
   useradd $username
   passwd="`cat /dev/random | head -1 | sum | head -c 5`"
   echo "$username:$passwd"| chpasswd
   echo "$username--$passwd" >>/home/hj/data/$username.txt
   let "index++"
done
  1. 授予shell脚本执行权限

  2. 执行脚本

  3. 执行结果

  4. 在此次实验基础上我又编写了一个删除用户的shell脚本

#!/bin/bash
index=1
while((index<21))
do
  username=hfuter$index
  userdel $username
  let "index++"
done
  1. 授予执行权限
  2. 执行结果

2)写一个 shell 脚本 catmn.sh

  1. 编写shell脚本如下

  2. 授予执行权限

  3. 执行结果

以上是关于合肥工业大学Linux实验三Linux 下的 shell 编程的主要内容,如果未能解决你的问题,请参考以下文章