|
学习shell做的第一个脚本,感谢云知梦李强强老师的shell编程教程- 创建shell脚本文件:
touch menu.sh
touch index.sh
touch welcome.sh
- 赋予脚本文件可执行权限:
chmod a+x menu.sh index.sh welcome.sh
- menu.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| #!/bin/bash
#menu.sh
function menu(){
title="My Home"
name="Randy"
time=`date +%Y-%m-%d`
cat << qaz
#######################################################################
## ***`echo -e "\e[32m $title \e[0m"`*** ##
#######################################################################
## 1)Add a user ##
## 2)View all users ##
## 3)Set passwd for user ##
## 4)Delete a user ##
## 5)Print disk space ##
## 6)Print mem space ##
## 7)Retrun menu ##
## 8)Logout ##
## 9)Quit ##
#######################################################################
## Name:$name Date:$time ##
#######################################################################
qaz
}
|
4.index.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| #!/bin/bash
#index.sh
function index() {
clear
. menu.sh
menu
while true
do
read -p "Please input a option:" option
case $option in
1)
read -p "Please input username:" name
useradd $name &>/dev/null
if [ $? -eq 0 ];then
echo "user ${name} is created successfully!!!"
else
echo "user ${name} is created failly!!!"
fi
;;
2)
str=`cat /etc/passwd | awk -F: '{print $1}'`
echo -e "\e[32m$str\e[0m"
;;
3)
read -p "input the username:" name
read -p "set password 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;47$str\033[0m"
else
str="${name}'s password is set failly!!!"
echo -e "\033[31;47m$str\033[0m"
fi
;;
4)
read -p "delete the user:" name
userdel -r $name &>/dev/null
if [ $? -eq 0 ];then
str="user ${name} is delete successfully!!!"
echo -e "\033[30;47m$str\033[0m"
else
str="user ${name} is delete failly!!!"
echo -e "\033[31;47m$str\033[0m"
fi
;;
5)
str=`df -Th`
echo -e "\033[30;47m$str\033[0m"
;;
6)
str=`free -m`
echo -e "\033[30;47m$str\033[0m"
;;
7)
clear
menu
;;
8)
echo -e "\e[31mLogout ...\e[0m"
sleep 1
break
;;
9)
echo -e "\e[31mQuit successfully!!!\e[0m"
exit
;;
*)
str="Input error please re-enter"
echo -e "\033[30;47m$str\033[0m"
;;
esac
done
}
|
5.welcome.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| #!/bin/bash
#welcome
clear
echo -e "\e[31mWelcome\e[0m"
while true
do
read -p 'Please enter user name (Quit please input "q") :' name
if [ $name = "q" ]
then
break
else
read -p 'Please enter user password:' password
if [ $name = 'admin' ] && [ $password = 'admin' ]
then
str="Login successfully,Please Wait ......"
echo -e "\e[31m$str\e[0m"
sleep 2
. index.sh
index
else
str="Login failly"
echo -e "\e[31m$str\e[0m"
fi
fi
done
|
6.界面展示:
|
|