【问题标题】:bash scripting - using of options and not optionbash 脚本 - 使用选项而不是选项
【发布时间】: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

那么在脚本中写什么来获取参数如果用户不使用选项将键入什么

【问题讨论】:

    标签: linux bash options


    【解决方案1】:

    通常,在 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
    

    如果您的用户指定both -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
    

    【讨论】:

      【解决方案2】:

      检查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
      

      【讨论】:

        猜你喜欢
        • 2019-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-27
        • 2021-04-05
        • 1970-01-01
        • 1970-01-01
        • 2011-12-06
        相关资源
        最近更新 更多