0

I created a script in bash that checking the strength of a password comibination I want to add an option that the user can input a password from a text file (The user types -f and then the file path and the he gets a password review ) or he types the password without any option before

What I want to ask what I need to do if I want to use an argument. Like if the user doesnt use a file in order to read the password from text file he just type the password by himself

#!/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

so what to write in the script for take an argument what the user will type if he doesnt uses an option

Shawn
  • 47,241
  • 3
  • 26
  • 60

2 Answers2

0

Check whether the password variable has already been set. If not, ask the user to enter it.

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
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Generally, after a while getopts loop, you want to shift off the options you've just processed:

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

What do you want to do if your user specifies both the -f option and gives a password on the command line? Which one do you want to use? With what I've written, the command line password wins.

Another way to implement this, where the -f option takes precedence:

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
glenn jackman
  • 238,783
  • 38
  • 220
  • 352