【发布时间】:2021-11-04 17:02:03
【问题描述】:
我在 bash 中创建了一个脚本来检查密码组合的强度 我想添加一个选项,用户可以从文本文件中输入密码(用户输入 -f,然后输入文件路径,然后他会获得密码审查),或者他输入密码之前没有任何选项
如果我想使用参数,我想问我需要做什么。 就像用户不使用文件来从文本文件中读取密码一样 他只是自己输入密码
#!/bin/bash
while getopts ":f:" option; do
case $option in
f) password=`cat $OPTARG` ;;
esac
done
#evaluating how much chars the password has
password_length=${#password}
#counter for checking in how much sections the password meets the rquirements
count=0
#Creating an array for stroing reasons why the password is incorrect
requirements=(foo bar)
#Checking if password includes minimum of 10 characters
if [ $password_length -ge 10 ];
then
requirements[0]="Correct"
else
requirements[0]="Incorrect password syntax. The password
length must includes minimum of 10 characters"
fi
#checkig if the password includes both alphabet and number
if [[ "$password" == *[a-zA-Z]* && "$password" == *[0-9]* ]]
then
requirements[1]="Correct"
else
requirements[1]="Incorrect password syntax. The password
must includes both alphabet and number"
fi
#checking if password includes both the small and capital case letters.
if [[ "$password" == *[A-Z]* && "$password" == *[a-z]* ]];
then
requirements[2]="Correct"
else
requirements[2]="Incorrect password syntax. The password
must includes both the small and capital case letters"
fi
#checking whether the password is according to the requirements or not
#if yes count will equal to 3 at the end
for i in "${requirements[@]}"
do
if [[ $i == "Correct" ]];
then
let count++
fi
done
#if the counter count is equal to 3 print the password in light green color and return exit 0
if [[ $count -eq 3 ]];
then
echo -e "\e[92m$password"
# sleep - user has the time to see that the password's syntax is correct
sleep 3
exit 0
#if the count is not equal to 3 print the password in reg color and return exit 1
else
echo -e "\e[91m$password"
for i in "${requirements[@]}"
do
if [[ $i != "Correct" ]];
then
echo $i
fi
done
# sleep - user has the time to see that the password's syntax is incorrect and the reasons for that
sleep 6
exit 1
fi
那么在脚本中写什么来获取参数如果用户不使用选项将键入什么
【问题讨论】: