0

i'm trying to solve this problem. i've a script that it's goal is to create a random password , afterwards if requested encrypt it / decrypt it. the main problem is : that the random function work perfectly with OPTARG , but the rest of the positional parameters request that i enter another value (an OPTARG value) while i'm calling it.(which i don't need ) and i can't resolve it . please help see the code and the objective:

# Objective:Create shell script that will generate the random password based
# on amount    characters that you provide to it.
# 1. Script should provide us with random password.
# 2. Provide us with encrypting and decrypting that same password.
# 3. Provide us with capability to saving the passwords to local database.





function choose(){
echo -n ${1:RANDOM%${#1}:1} 
}


function menu(){
i=0
num=$1
myhash=$(while (( i<$num ))
do
  choose '!@#$%^&*()1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  let i++
done )


}

while getopts e:d:h: options
do

case "${options}" in
  e)
   
   echo "encrypting you password please wait ..."
   sleep 2.0
   echo 
   mask="$(echo -n "$myhash" | base64 )"
   echo $mask 
   mask2="$(mask)"
   echo $mask2  
  ;; 
    d)
   
    echo "decrypting your password please wait..."
    sleep 2.0
    dec=$(echo -n "$mask2" | base64 -d )  # the decryption stage...
    echo $dec
;;

  h) # Specify Random digit value
    ho=${OPTARG}
    echo $ho
    if [[ $ho -eq 0 ]];then
      echo "you need to provide a value for the random password length"
    else
   
      echo "Hasing your password..."  
      sleep 1.0
      menu "$ho"
    fi
 ;;
 esac   
done

this is the result for executing the script as follows :./myrandom.sh -h1 -e

1 Hasing your password... ./myrandom.sh: option requires an argument -- e

  • `getopts e:d:h:` - so, what do you think `:` means? – KamilCuk Jan 20 '22 at 00:12
  • Your options string is wrong; see [this answer](https://stackoverflow.com/questions/16483119/an-example-of-how-to-use-getopts-in-bash/34531699#34531699). Also, it's almost always best to just set variables indicating what options were supplied inside the `getopts` loop, then act on them only after all options have been processed (i.e. *after* the loop). – Gordon Davisson Jan 20 '22 at 00:41

0 Answers0