shell:编写脚本,初始密码为123456,检查密码,屏幕提示“please input your password”,如果输入正确显示“you are right,ok”如果用户三次输入密码均错误,则退出脚本
时间: 2024-02-18 12:06:04 浏览: 139
好的,我可以帮你编写这个脚本。你可以在 Shell 终端中输入以下命令来创建一个新的脚本文件:
```
nano check_password.sh
```
然后将以下代码复制粘贴到文件中:
```
#!/bin/bash
# 初始密码
password="123456"
# 循环三次
for i in {1..3}; do
# 屏幕提示
echo "please input your password: "
read -s input_password
# 检查密码是否正确
if [[ "$input_password" == "$password" ]]; then
echo "you are right, ok"
exit 0
fi
done
# 三次输入密码均错误
echo "Sorry, your password is incorrect. Exiting..."
exit 1
```
保存文件并退出编辑器。然后,您可以使用以下命令来运行脚本:
```
bash check_password.sh
```
现在,当您运行脚本时,它将提示您输入密码,并根据您的输入显示相应的消息。如果您三次输入错误的密码,则脚本将退出。
相关问题
1、编写shell程序,实现自动删除10个账号的功能。账号名为us01至us10。 2、显示传统方式的九九乘法表。 3、编写脚本,初始密码为123456,检查密码,屏幕提示“please input your password”,如果输入正确显示“you are right,ok”如果用户三次输入密码均错误,则退出脚本。
好的,我会逐个回答你的问题。
1、删除10个账号的shell程序可以这样写:
```shell
#!/bin/bash
for i in {1..10}
do
userdel -r us0${i} # -r选项表示删除用户的同时删除其家目录
done
```
2、传统方式的九九乘法表可以这样写:
```shell
#!/bin/bash
for i in {1..9}
do
for j in {1..9}
do
if [ $j -le $i ]
then
echo -n "$j*$i=$((i*j)) "
fi
done
echo # 每一行结束后换行
done
```
3、检查密码的脚本可以这样写:
```shell
#!/bin/bash
password="123456"
count=0
while [ $count -lt 3 ]
do
echo "please input your password:"
read input_password
if [ $input_password = $password ]
then
echo "you are right,ok"
exit 0 # 退出脚本
else
echo "wrong password!"
count=$((count+1))
fi
done
echo "you have tried 3 times,exit now."
exit 1
```
希望这些代码对你有所帮助。
编写shell脚本,系统密码为 123456, 如果输入不对给出提示,最多 3 次机会
好的,下面是编写的脚本,请按照以下步骤操作:
1. 创建一个文件名为 check_password.sh 的文件,可以使用任何编辑器进行编辑,比如 vi check_password.sh
2. 将下面的代码复制粘贴到 check_password.sh 文件中:
```
#!/bin/bash
PASSWORD="123456"
TRIES=3
while [ $TRIES -gt 0 ]
do
read -s -p "Enter password: " user_input
echo
if [ "$user_input" == "$PASSWORD" ]
then
echo "Password correct!"
exit 0
else
echo "Wrong password. Please try again."
TRIES=$((TRIES-1))
fi
done
echo "You've exceeded the maximum number of tries. Exiting..."
exit 1
```
3. 保存文件并退出编辑器。
4. 打开终端,进入到保存 check_password.sh 文件的目录。
5. 运行脚本,输入密码并按回车键进行验证。
脚本会提示您输入密码,如果输入正确,则显示“Password correct!”并退出脚本,否则会提示“Wrong password. Please try again.”并让您重新输入密码,最多可以尝试 3 次。如果您尝试了 3 次仍未输入正确的密码,则脚本将显示“ You've exceeded the maximum number of tries. Exiting...”并退出。
阅读全文
相关推荐














