[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

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
0
projects
Asked:
projects
  • 9
  • 6
1 Solution
 
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
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

 
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
 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now