Linux课程设计报告 下载本文

内容发布更新时间 : 2024/5/13 2:40:56星期一 下面是文章的全部内容请认真阅读。

八、源程序清单

(1)Menu.sh文件

1

#! /bin/bash

dataSource='passwd'; addUsers=\

deleteUsers=\CheckDataSourceFileExist() {

if [ -f2 $dataSource ] then }

#文件存在 return 0; else fi

#文件不存在 clear;

echo -n \警告 【$dataSource】 不存在!请确认!\ read ; return 1;

choice=\

while [ $choice != \3 $choice != \do

CheckDataSourceFileExist; if [ $?4 -ne5 1 ] then clear;

echo \ 用户信息管理主菜单\ echo

\

echo \ echo \ echo \ echo \ echo \

1.显示当前所有记录\

2.格式化显示当前所有记录\3.显示用户名和用户ID\4.格式化显示用户名和ID\5.查询特定用户信息\

1

#!程序解释器路径 2

-f file判断文件存在

当两边的两个表达式都为真时 4

$?代表上一个命令执行后的推出状态,0是成功,非0失败 5

-ne是不等于

3

13

echo \ 6.添加新用户\echo \ 7.删除用户\echo \ Q.退出\echo -n \你的选择:\read choice;

if [ -z1 $choice ] then

choice=\fi

clear;

if [ $choice = \then

echo \选项尚未选择!\else

case $choice in

1) CheckDataSourceFileExist; if [ $? -ne 1 ] then

echo \当前的所有的用户信息如下所示:\

echo \用户名 密码 ID GID 说明 工作目录 登录Shell\cat $dataSource | tr2 \

2) CheckDataSourceFileExist; if [ $? -ne 1 ] then

fi;;

echo \当前的所有的用户信息如下所示:\ echo -e3 \用户名\\t密码\\tID\\tGID\\t说明\\t工作目录\\t登录Shell\ sort -k 1 $dataSource | awk -F \'{print $1 \ fi;;

3) CheckDataSourceFileExist; if [ $? -ne 1 ] then

echo \当前的所有的用户的用户名和用户ID如下所示:\ echo -e \用户名,ID\

awk -F4 \ fi;; 4) CheckDataSourceFileExist; if [ $? -ne 1 ]

12

-z判断长度是0 tr 用空格替换: 3

-e后加上\\起到转义作用 4

-F以后面的东西作为分隔符

14

then

echo \当前的所有的用户的用户名和用户ID如下所示:\

echo -e \用户名 \\t ID\

awk -F \'{print $1 \\ $3}' $dataSource | more;

fi;;

5) CheckDataSourceFileExist; if [ $? -ne 1 ] then

keyWords=\

while [ -z $keyWords ] do

echo -n \输入搜索关键词:\read keyWords;

if [ -z $keyWords ] then

echo \搜索关键词不能为空,请重新输入\ fi done

CheckDataSourceFileExist; if [ $? -ne 1 ] then

grep -i1 $keyWords $dataSource; if [ $? -eq 1 ] then

echo \很遗憾,【$dataSource】文件中,并不存在与 $keyWords 一致的信息。\ fi fi fi;;

6) $addUsers;; 7) $deleteUsers;;

fi

Q) printf \程序已经退出。\ q) printf \程序已经退出。\

*) echo $choice \:此选项不是默认提供的功能。请确认\esac

echo -n \确认??\ read ; fi Done

1

Grep –i大小写无关的搜索

15

(2)add.sh文件

#! /bin/bash

dataSource='passwd'; clear;

isContinue=%userName=\

while [ $isContinue = \1 $isContinue = \do

userName=\

while [ -z $userName ] do

echo -n \用户名:\read userName;

if [ -z $userName ] then

echo \错误!用户名是不能为空的,请重新输入!\ continue; fi if [ `expr match $userName \-ne `expr length $userName` ] then

echo \错误!用户名的只能由非数字打头的字符和数字组成,请重新输入!\ userName=\ continue; fi done

passWord=\

passWordAgain=\

while [ -z $passWord ] do

echo -n \密码:\read passWord; if [ -z $passWord ] then

echo \错误!密码是不能为空的,请重新输入!\ continue; fi

if [ `expr length $passWord` -ne 6 ] then fi

echo \密码长度为6位,请重新输入!\passWord=\continue;

1

# 表达式1或者表达式2为true

16

if [ `expr match1 $passWord \-ne `expr length $passWord` ] then

1

fi echo \密码由大小写字母、数字和控制字符组成,请重新输入!\passWord=\continue;

echo -n \请在输入一次密码:\read passWordAgain;

if [ $passWordAgain != $passWord ] then

echo \两次输入的密码不一样,请重新输入!\ passWord=\ continue; fi done

#用户UID输入处理,UID为数字、一般非超级用户的ID大等于500 uID=\

while [ -z $uID ] do

echo -n \用户UID:\read uID; if [ -z $uID ] then

echo \错误!用户UID是不能为空的,请重新输入!\ continue; fi

if [ `expr match $uID \2 $uID` ] then fi

echo \错误!用户的UID必须为数字,请重新输入!%uID=\continue;

# < 或者 >

if [ $uID -lt 500 -o $uID -gt 60000 ] then

echo \错误!一般非超级用户的ID范围为500~60000,请重新输入!\ uID=\ continue; fi done

expr match $string substring命令在string字符串中匹配substring字符串,然后返回匹配到的substring字符串的长度,若找不到则返回0 2

expr length $string 求出字符串的长度

17