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
projectsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MikeOM_DBACommented:
Your script makes no sense, specially the "# Gen codes" section.
0
projectsAuthor Commented:
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.
0
MikeOM_DBACommented:
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".
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

projectsAuthor Commented:
You might want to take a closer look.
0
MikeOM_DBACommented:
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

0
projectsAuthor Commented:
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.
0
MikeOM_DBACommented:
It worked perfectly for me, did you enter the existing activation code?
0
projectsAuthor Commented:
I just made one up of course :)
But as you can see, doing so just killed the script.
0
projectsAuthor Commented:
# ./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.
0
projectsAuthor Commented:
If you look at your code, the elseif sections are different?

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


elif [ ! "$some_variable" = "$PASS_CODE" ]
0
MikeOM_DBACommented:
# ./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).
0
projectsAuthor Commented:
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.
0
MikeOM_DBACommented:
Here:
NAME=Test1
files=.

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)
else
  ACT_CODE=${some_variable}
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)
else
  PASS_CODE=${some_variable}
fi

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

Open in new window

Was it too hard to make the change?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
projectsAuthor Commented:
A few edits and it's working perfectly now.

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

Too hard to change what?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.