[英]bash scripting - using of options and not option
我在 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
那么在腳本中寫什么來獲取參數如果用戶不使用選項將輸入什么
檢查password
變量是否已經設置。 如果沒有,請讓用戶輸入。
while getopts ":f:" option; do
case $option in
f) password=`cat $OPTARG` ;;
esac
done
if [ -z "$password" ]
then read -p "Enter password: " -r password
fi
通常,在一段while getopts
循環之后,您希望shift
剛剛處理的選項:
while getopts ":f:" option; do
case $option in
f) # bash builtin way to slurp a file into a variable
password=$(< "$OPTARG") ;;
esac
done
# remove the options, if any
shift $((OPTIND - 1))
# the remaining command line arguments are at `$1`, `$2`, etc, as usual.
[[ -n $1 ]] && password=$1
如果您的用戶同時指定-f
選項並在命令行上提供密碼,您想做什么? 你想用哪一個? 使用我所寫的內容,命令行密碼獲勝。
另一種實現方式,其中-f
選項優先:
password_file=""
while getopts ":f:" option; do
case $option in
f) password_file=$OPTARG ;;
esac
done
shift $((OPTIND - 1))
if [[ -n $password_file ]]; then
if [[ ! -r $password_file ]]; then
echo "Cannot read '$password_file'" >&2
exit 1
fi
password=$(< "$password_file")
elif [[ -n $1 ]]; then
password=$1
else
echo "No password provided. Usage: ..." >&2
exit 1
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.