Link to home
Start Free TrialLog in
Avatar of projects
projects

asked on

bash scripting

In this bash example, I need to allow the user to RETURN to create a new codes or paste existing codes to be used.

# This is the new code I want to use
read -p 'Hit RETURN for NEW Activation code or paste existing: ' some_variable
read -p 'Hit RETURN for NEW Password or paste existing: ' some_variable


# The following is the existing code I need to modify

# Code file name
ACTCODE=act_code_$NAME

# Gen codes
ACT_CODE=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)
PASS_CODE=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1)

#Store codes
echo "$PASS_CODE" >files/pcode
echo $ACT_CODE > $ACTCODE
echo $PASS_CODE >> $ACTCODE


# Next function
Avatar of MikeOM_DBA
MikeOM_DBA
Flag of United States of America image

Your script makes no sense, specially the "# Gen codes" section.
Avatar of projects
projects

ASKER

Well, thanks for your input but you don't provide enough information unfortunately.
The 'gen' section simply means 'generate codes'

Also, I never said it's a script, I said it's a script example. Just an example.
What are you trying to do?
The first line (ACT_CODE) gives you the 10 first characters (after the "tr" part) of file /dev/urandom
The second line (PASS_CODE) gives you also the 10 first characters (after the "tr" part) of same file /dev/urandom because of the "head -n 1"
Then you write that same data into two files (twice on the "$ACTCODE" file).
Also you provide no clue where you intend to use "some_variable".
You might want to take a closer look.
OK, perhaps I got it:
files=~/scripts/lkb

ACTCODE=act_code_$NAME

ACT_CODE=$( cat $ACTCODE|fold -w 10 | head -n 1)
PASS_CODE=$( cat $files/pcode )


read -p 'Hit RETURN for NEW Activation code or paste existing: ' some_variable
if [ "${some_variable}?" = "?" ]
then
  ACT_CODE=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)
elif [ ! "${some_variable}" = "${ACT_CODE}" ]
  then
    echo "!Error, invalid Activation code."
    exit 1
fi

read -p 'Hit RETURN for NEW Password or paste existing: ' some_variable
if [ "${some_variable}?" = "?" ]
then
  PASS_CODE=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)
elif [ ! "$some_variable" = "$PASS_CODE" ]
  then
    echo "!Error, invalid password code."
    exit 2
fi

#Store codes
echo "$PASS_CODE" >$files/pcode
echo $ACT_CODE > $ACTCODE
echo $PASS_CODE >> $ACTCODE

Open in new window

Lots of problems unfortunately.

When hitting return on both prompts, only the act code is created.
When entering a number into the first prompt, the script dies;

Hit RETURN for NEW Activation code or paste existing: 1234
!Error, invalid Activation code.

I changed some_variable to two unique variables.
It worked perfectly for me, did you enter the existing activation code?
I just made one up of course :)
But as you can see, doing so just killed the script.
# ./cl
Hit RETURN for NEW Activation code or paste existing:
Hit RETURN for NEW Password or paste existing:
PWyYXfAaCU
wvhqmjrnXQ

# ./cl
Hit RETURN for NEW Activation code or paste existing: 55
!Error, invalid Activation code.
If you look at your code, the elseif sections are different?

elif [ ! "${some_variable}" = "${ACT_CODE}" ]


elif [ ! "$some_variable" = "$PASS_CODE" ]
# ./cl
Hit RETURN for NEW Activation code or paste existing: 55
!Error, invalid Activation code.
Its obvious that "55" does not match existing Activation code: "PWyYXfAaCU"

Also even if the elif sections are different, they are equivalent (work the same).
Uh, you've completely misunderstood here :).

I never said anything had to match anything. I only said if no code was input, then create a new code.

This means if I want to use an existing code, I simply enter it. If I hit return without entering anything, it means create a new code please.
ASKER CERTIFIED SOLUTION
Avatar of MikeOM_DBA
MikeOM_DBA
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A few edits and it's working perfectly now.

Too hard to change what?
A few edits and it's working perfectly now.

Too hard to change what?