Shell编写字符菜单管理-8

时间:2021-12-27 16:13:50

第8章 Shell编写字符菜单管理

一.shell函数定义
function menu(){
echo 'this is a func!!';
}

二.shell函数使用
menu

三.cat命令的heredoc使用方法
cat << eof
aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa
eof

四.字符界面下字体的颜色控制
echo -e "\033[30;47m test \033[0m"

五.shell如何包含文件
vi func.sh
. menu.sh
menu #执行menu.sh中的menu函数

六.while如何写一个死循环
while true
do
#执行代码
done

字符菜单制作:
1. 用户添加
2. 用户删除
3. 修改密码
4. 查看硬盘空间使用情况
5. 查看内存空间使用情况
6. 退出菜单

#!/bin/bash
#index.sh

. menu.sh

clear
menu

while true
do
read -p "please input a option:" option

case $option in
1)
read -p "add a user:" name
useradd $name &>/dev/null

if [ $? -eq 0 ];then
str="user ${name} is created successfully!!!"
echo -e "\033[30;47m$str\033[0m"
else
str="user ${name} is created failly!!!"
echo -e "\033[31;47m$str\033[0m"
fi
;;
2)
read -p "input the user:" name
read -p "set pass for the user:" pass
echo $pass | passwd --stdin $name &>/dev/null

if [ $? -eq 0 ];then
str="${name}'s password is set successfully!!!"
echo -e "\033[30;47m$str\033[0m"
else
str="${name}'s password is set failly!!!"
echo -e "\033[31;47m$str\033[0m"
fi
;;
3)
read -p "delete a user:" name
userdel -r $name &>/dev/null

if [ $? -eq 0 ];then
str="user ${name} is deleted successfully!!!"
echo -e "\033[30;47m$str\033[0m"
else
str="user ${name} is deleted failly!!!"
echo -e "\033[31;47m$str\033[0m"
fi
;;
4)
str=`free -m`
echo -e "\033[30;47m$str\033[0m"
;;
5)
str=`df -Th`
echo -e "\033[30;47m$str\033[0m"
;;
6)
echo -e "\033[30;47mQuit successfully!!\033[0m"
break
;;
7)
clear
menu
;;
esac
done

#!/bin/bash
#menu.sh

function menu(){
title="My Menu"
url="www.lampym.com"
time=`date +%Y-%m-%d`

cat << eof
#######################################
`echo -e "\033[32;40m$title\033[0m"`
#######################################
* 1)add a user
* 2)set password for user
* 3)delete a user
* 4)print disk space
* 5)print mem space
* 6)quit
* 7)return main menu
#######################################
$url $time
#######################################
eof
}