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