linux shell命令之脚本要有足够的提示

时间:2021-04-14 11:41:38   收藏:0   阅读:0

一个有参数但无提示说明的脚本
#login1.sh: 用户登录,判断用户输入的用户名和密码是否错误

vi login1.sh
#!/bin/bash

for ((i=0; i < 3; i++))
do
read username
read password
if test "$username" = "user" -a "$password" = "pwd"
then
echo "login success"
flag=1;
break;
fi
done

./login1.sh
^C

一个有参数且有提示说明的脚本
#login2.sh:用户登录,判断用户输入的用户名和密码是否正确
vi login2.sh
#!/bin/bash

flag=0;

#提示该脚本的用途
echo "This script is used to username and password what you input is right or wrong."

#for 循环用于提示输入用户名和密码,并判断正误
for ((i=0; i < 3; i++))
do
echo -n "Please input your name:"
read username
echo -n "Please input your password: "
read password
if test "$username" = "user" -a "$password" = "pwd"
then
echo "Login success"
flag=1
break
else
echo "The username or password is wrong. Try again! "
fi
done

#三次输入不正确,提示用户退出脚本
if [ "$flag" -eq 0 ]
then
echo "You have tried 3 times. login fail!"
fi
./login2.sh
This script is used to username and password what you input is right or wrong.
Please input your name:alloy
Please input your password: 123
The username or password is wrong. Try again!
Please input your name:user
Please input your password: pwd
Login success

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!