how to use RANDOM command in declared array - arrays

I am trying to build a script where I need to create a password generator with the following parameters :
must be at least 8 characters long but not longer than 16 characters.
must contain at least 1 digit (0-9).
must contain at least 1 lowercase letter.
must contain at least 1 uppercase letter.
must contain exactly one and only one of # # $ % & * + - =
I have two ideas :
The first :
#!/bin/bash
#
#Password Generator
#
#
Upper=('A''B''C''D''E''F''G''H''I''J'K''L''M''N''O''P''Q''R''S''T''U''V''W''X''Y''Z')
Lower=('a''b''c''d''e''z''f''g''h''i''j''k''l''m''o'''p''q''r''s''t''u''v''w''x''y''z')
Numbers=('1''2''3''4''5''6''7''8''9')
SpecialChar=('#''#''$''%''&''*''+''-''=')
if [ S# -eq 0 ] ; then
Pwlength=`shuf -i 8-16 -n 1`
Password=`< /dev/urandom tr -dc A-Za-z0-9$SpecialChar | head -c $Pwlength`
echo "Random Password is being generated for you"
sleep 5
echo "Your new password is : $Password"
exit
The problem is I get characters that I didnt even defined ?
The secound idea :
for((i=0;i<4;i++))
do
password=${Upper[$random % ${#Lower[#]} ] }
password=${Upper[$random % ${#Upper[#]} ] }
password=${Upper[$random % ${#Number[#]} ] }
password=${Upper[$random % ${#SpecialChar[#]} ] }
done
For some reason non of them work ;/

In your first example, move the "-" character to the end of the SpecialChar. I think the definition as you had it results in allowing "+" to "=" (i.e., the value passed to tr reads as "+-="), which accounts for the characters you were not expecting. Alternatively, replace the "-" with "_".
So, try a definition like:
SpecialChar=('#''#''$''%''&''*''+''=''-')

As already mentioned, it would be cleaner and easier to use directly a string to handle list of characters. Handling special characters in an array may cause side effects (for instance getting the list of files in the current directory with the '*' character). In addition, arrays may be difficult to pass as function arguments).
ALPHA_LOW="abcdefghijklmnopqrstuvwxyz"
# Then simply access the char in the string at the ith position
CHAR=${ALPHA_LOW:$i:1}
You could generate upper cases from the lower cases.
ALPHA_UP="`echo \"$ALPHA_LOW\" | tr '[:lower:]' '[:upper:]'`"
The variable which contains a random number is $RANDOM (in capital letters).
sleep 5 is unnecessary.
You need to find a way to keep count of occurrences left for each character type. For more information, I wrote a complete script to do what you described here.

Your first attempt has the following problems:
You are using arrays to contain single strings. Pasting 'A''B''C' is equivalent to 'ABC'. Simply converting these to scalar variables would probably be the simplest fix.
You are not quoting your variables.
You declare Upper, Lower, and Numbers, but then never use them.
Using variables for stuff you only ever use once (or less :-) is dubious anyway.
As noted by #KevinO, the dash has a special meaning in tr, and needs to be first or last if you want to match it literally.
On top of that, the sleep 5 doesn't seem to serve any useful purpose, and will begin to annoy you if it doesn't already. If you genuinely want a slower computer, I'm sure there are people here who are willing to trade.
Your second attempt has the following problems:
The special variable $RANDOM in Bash is spelled in all upper case.
You are trying to do modulo arithmetic on (unquoted!) arrays of characters, which isn't well-defined. The divisor after % needs to be a single integer. You can convert character codes to integers but it's sort of painful, and it's less than clear what you hope for the result to be.
A quick attempt at fixing the first attempt would be
Password=$(< /dev/urandom tr -dc 'A-Za-z0-9##$%&*+=-' | head -c $(shuf -i 8-16 -n 1))
If you want to verify some properties on the generated password, I still don't see why you would need arrays.
while read -r category expression; do
case $Password in
*[$expression]*) continue;;
*) echo "No $category"; break;;
esac
done <<'____HERE'
lowercase a-z
uppercase A-Z
numbers 0-9
specials -##$%&*+=
HERE

Related

Why is Perl giving "Can't modify string in scalar output" error?

I'm pretty new to Perl and this is my most complex project yet. Apologies if any parts of my explanation don't make sense or I miss something out - I'll be happy to provide further clarification. It's only one line of code that's causing me an issue.
The Aim:
I have a text file that contains a single column of data. It reads like this:
0
a,a,b,a
b,b,b,a
1
a,b,b,a
b,b,b,a
It continues like this with a number in ascending order up to 15, and the following two lines after each number are a combination of four a's or b's separated by commas. I have tied this file to an array #diplo so I can specify specific lines of it.
I also have got a file that contains two columns of data with headers that I have converted into a hash of arrays (with each of the two columns being an array). The name of the hash is $lookup and the array names are the names of the headings. The actual arrays only start from the first value in each column that isn't a heading. This file looks like this:
haplo frequency
"|5,a,b,a,a|" 0.202493719
"|2,b,b,b,a|" 0.161139191
"|3,b,b,b,a|" 0.132602458
This file contains all of the possible combinations of a or b at the four positions combined with all numbers 0-14 and their associated frequencies. In other words, it includes all possible combinations from "|0,a,a,a,a|" followed be "|1,a,a,a,a|" through to "|13,b,b,b,b|" and "|14,b,b,b,b|".
I want my Perl code to go through each of the combinations of letters in #diplo starting with a,a,b,a and record the frequency associated with the row of the haplo array containing each number from 0-14, e.g. first recording the frequency associated with "|0,a,a,b,a|" then "|1,a,a,b,a|" etc.
The output would hopefully look like this:
0 #this is the number in the #diplo file and they increase in order from 0 up to 15
0.011 0.0023 0.003 0.0532 0.163 0.3421 0.128 0.0972 0.0869 0.05514 0.0219 0.0172 0.00824 0.00886 0.00196 #these are the frequencies associated with x,a,a,b,a where x is any number from 0 to 14.
My code:
And here is the Perl code I created to hopefully sort this out (there is more to create the arrays and such which I can post if required, but I didn't want to post a load of code if it isn't necessary):
my $irow = 1; #this is the row/element number in #diplo
my $lrow = 0; #this is the row/element in $lookup{'haplo'}
my $copynumber = 0;
#print "$copynumber, $diplo[2]";
while ($irow < $diplolines - 1) {
while ($copynumber < 15) {
while ($lrow < $uplines - 1) {
if ("|$copynumber,$diplo[$irow]|" = $lookup{'haplo'}[$lrow]) { ##this is the only line that causes errors
if ($copynumber == 0) {
print "$diplo[$irow-1]\n";
#print "$lookup{'frequency'}[$lrow]\t";
}
print "$lookup{'frequency'}[$lrow]\t";
}
$lrow = $lrow + 1;
}
$lrow = 0;
$copynumber = $copynumber + 1;
}
$lrow = 0;
$copynumber = 0;
$irow = $irow + 1;
}
However, the line if ("|$copynumber,$diplo[$irow]|" = $lookup{'haplo'}[$lrow]) is causing an error Can't modify string in scalar assignment near "]) ".
I have tried adding in speech marks, rounded brackets and apostrophes around various elements in this line but I still get some sort of variant on this error. I'm not sure how to get around this error.
Apologies for the long question, any help would be appreciated.
EDIT: Thanks for the suggestions regarding eq, it gets rid of the error and I now know a bit more about Perl than I did. However, even though I don't get an error now, if I put anything inside the if loop for this line, e.g. printing a number, it doesn't get executed. If I put the same command within the while loop but outside of the if, it does get executed. I have strict and warnings on. Any ideas?
= is assignment, == is numerical comparison, eq is string comparison.
You can't modify a string:
$ perl -e 'use strict; use warnings; my $foo="def";
if ("abc$foo" = "abcdef") { print "match\n"; } '
Found = in conditional, should be == at -e line 1.
Can't modify string in scalar assignment at -e line 1, near ""abcdef") "
Execution of -e aborted due to compilation errors.
Nonnumerical strings act like zeroes in a numerical comparison:
$ perl -e 'use strict; use warnings; my $foo="def";
if ("abc$foo" == 0) { print "match\n"; } '
Argument "abcdef" isn't numeric in numeric eq (==) at -e line 1.
match
A string comparison is probably what you want:
$ perl -e 'use strict; use warnings; my $foo="def";
if ("abc$foo" eq "abcdef") { print "match\n"; } '
match
This is the problematic expression:
"|$copynumber,$diplo[$irow]|" = $lookup{'haplo'}[$lrow]
The equals sign (=) is an assignment operator. It assigns the value on its right-hand side to the variable on its left-hand side. Therefore, the left-hand operand needs to be a variable, not a string as you have here.
I don't think you want to do an assignment here at all. I think you're trying to check for equality. So don't use an assignment operator, use a comparison operator.
Perl has two equality comparison operators. == does a numeric comparison to see if its operands are equal and eq does a string comparison. Why does Perl need two operators? Well Perl converts automatically between strings and numbers so it can't possibly know what kind of comparison you want to do. So you need to tell it.
What's the difference between the two types of comparison? Well, consider this code.
$x = '0';
$y = '0.0';
Are $x and $y equal? Well it depends on the kind of comparison you do. If you compare them as numbers then, yes, they are the same value (zero is the same thing whether it's an integer or a real number). But if you compare them as strings, they are different (they're not the same length for a start).
So we now know the following
$x == $y # this is true as it's a numeric comparison
$x eq $y # this is false as it's a string comparison
So let's go back to your code:
"|$copynumber,$diplo[$irow]|" = $lookup{'haplo'}[$lrow]
I guess you started with == here.
"|$copynumber,$diplo[$irow]|" == $lookup{'haplo'}[$lrow]
But that's not right as |$copynumber,$diplo[$irow]| is clearly as string, not a number. And Perl will give you a warning if you try to do a numeric comparison using a value that doesn't look like a number.
So you changed it to = and that doesn't work either as you've now changed it to an assignment.
What you really need is a string comparison:
"|$copynumber,$diplo[$irow]|" eq $lookup{'haplo'}[$lrow]

How to slice a variable into array indexes?

There is this typical problem: given a list of values, check if they are present in an array.
In awk, the trick val in array does work pretty well. Hence, the typical idea is to store all the data in an array and then keep doing the check. For example, this will print all lines in which the first column value is present in the array:
awk 'BEGIN {<<initialize the array>>} $1 in array_var' file
However, it is initializing the array takes some time because val in array checks if the index val is in array, and what we normally have stored in array is a set of values.
This becomes more relevant when providing values from command line, where those are the elements that we want to include as indexes of an array. For example, in this basic example (based on a recent answer of mine, which triggered my curiosity):
$ cat file
hello 23
bye 45
adieu 99
$ awk -v values="hello adieu" 'BEGIN {split(values,v); for (i in v) names[v[i]]} $1 in names' file
hello 23
adieu 99
split(values,v) slices the variable values into an array v[1]="hello"; v[2]="adieu"
for (i in v) names[v[i]] initializes another array names[] with names["hello"] and names["adieu"] with empty value. This way, we are ready for
$1 in names that checks if the first column is any of the indexes in names[].
As you see, we slice into a temp variable v to later on initialize the final and useful variable names[].
Is there any faster way to initialize the indexes of an array instead of setting one up and then using its values as indexes of the definitive?
No, that is the fastest (due to hash lookup) and most robust (due to string comparison) way to do what you want.
This:
BEGIN{split(values,v); for (i in v) names[v[i]]}
happens once on startup and will take close to no time while this:
$1 in array_var
which happens once for every line of input (and so is the place that needs to have optimal performance) is a hash lookup and so the fastest way to compare a string value to a set of strings.
not an array solution but one trick is to use pattern matching. To eliminate partial matches wrap the search and array values with the delimiter. For your example,
$ awk -v values="hello adieu" 'FS values FS ~ FS $1 FS' file
hello 23
adieu 99

For loop to take the value of the whole array each time

Suppose I have 3 arrays, A, B and C
I want to do the following:
A=("1" "2")
B=("3" "4")
C=("5" "6")
for i in $A $B $C; do
echo ${i[0]} ${i[1]}
#process data etc
done
So, basically i takes the value of the whole array each time and I am able to access the specific data stored in each array.
On the 1st loop, i should take the value of the 1st array, A, on the 2nd loop the value of array B etc.
The above code just iterates with i taking the value of the first element of each array, which clearly isn't what I want to achieve.
So the code only outputs 1, 3 and 5.
You can do this in a fully safe and supportable way, but only in bash 4.3 (which adds namevar support), a feature ported over from ksh:
for array_name in A B C; do
declare -n current_array=$array_name
echo "${current_array[0]}" "${current_array[1]}"
done
That said, there's hackery available elsewhere. For instance, you can use eval (allowing a malicious variable name to execute arbitrary code, but otherwise safe):
for array_name in A B C; do
eval 'current_array=( "${'"$array_name"'[#]}"'
echo "${current_array[0]}" "${current_array[1]}"
done
If the elements of the arrays don't contain spaces or wildcard characters, as in your question, you can do:
for i in "${A[*]}" "${B[*]}" "${C[*]}"
do
iarray=($i)
echo ${iarray[0]} ${iarray[1]}
# process data etc
done
"${A[*]}" expands to a single string containing all the elements of ${A[*]}. Then iarray=($i) splits this on whitespace, turning the string back into an array.

What is a regular expression for parsing key = value using C's regex.h lib

I need an expression which would much key=value line
Actually you might have between "key" and "=" "value" as many whitespaces as you want
so key = value is valid as well. But multivalue should not be allowed.
So something like this:
**key = value1 value2**
is not allowed.
I've already tried with
**const char* regexCheckValidityForKeyValue = "([[:print:]]{1,})([:blank:]*)(\\=){1}([[:blank:]]*)([[:graph:]]*)";**
But this does not really work.
Thank you for any help.
At least to me, it appears that using a regex for this is entirely unnecessary. Getting the code correct will be comparatively difficult, and reading it even more so.
I'd just use [sf]scanf:
if (2 == (sscanf(input, "%s = %s %s", key, value, ignore))
// it's good: just `key = value`
else
// malformed
Basically, this attempts to read and convert a key, a value, and a second value. It then checks the return value to see how many of those were matched. If exactly two matched, you have "key = value". If fewer than 2 matched, some part of key = value is missing. If it matches more than that, you have key = value1 value2 (and possibly more after value2), so that's malformed as well.
As a bonus, this also gives you the actual strings that made up your key and value without any extra cruft.
(Decided to put the comment as an answer instead...)
You're close - just put the first [:blank:] inside a character class as you've done with the rest of them. Then remove the escaping of the '=' i.e. : ([[:print:]]{1,})([[:blank:]]*)(=){1}([[:blank:]]*)([[:graph:]]*), or if your flavor supports shorthand - (\w+)\s*=\s*(\S+) which is a bit easier to read (or at least shorter ;)). Also captures only the key and the value, not the spaces and stuff that your regex does.
Regards
The "print" class includes the space character 0x20-0x7e, which is making the key allowed to have spaces, or basically could match all spaces as a key.
I assume this is extended POSIX.
There are problems in your description.
The only way to not allow a second 'value2' is to put edge conditions at the end.
But, not much is known about that.
modified Since there is no way to negate a posix class, you have to be specific to stop part delimeters from being included in key/value.
# ^[[:blank:]]*([[:alpha:]][!-<>-~]*)[[:blank:]]*=[[:blank:]]*([!-<>-~]+)[[:blank:]]*$
^ # BOS
[[:blank:]]* # space or tab
( # (1 start), KEY
[[:alpha:]] # start with alpha char
[!-<>-~]* # any chars not equal sign nor whitespace
) # (1 end)
[[:blank:]]* # spaces or tabs
= # equal sign
[[:blank:]]* # spaces or tabs
( [!-<>-~]+ ) # (2), VALUE any chars not equal sign nor whitespace
[[:blank:]]* # spaces or tabs
$ # EOS

BASH Changing Array element into Variable

I'm trying to create variables from array elements and check value of my created variable in sourcefile.
MySourceFile:
value1abc="10"
value2efg="30"
value3ade="50"
value4tew="something else"
onemorevalue="192.168.1.0"
Script Code :
declare -a CfgData
CfgData=("value1abc" "value2efg" "value3ade" "value4tew" "othervalue" "onemorevalue")
COUNTER="0"
source MySourceFile
until [ $COUNTER == ${#CfgData[#]} ]; do
value=$[${CfgData[$COUNTER]}]
echo -ne "\\n${CfgData[$COUNTER]}=\"$value\""\\n\\n
let COUNTER+=1
done
Script works fine until it comes to values which contain data other than pure numbers (letters, spaces, dots, generally all characters) in which case :
value="randomcharacters" # Gives me "0"
value="something else" & value="192.168.1.0" # line XX: 192.168.1.0: syntax error: invalid arithmetic operator (error token is ".168.1.0")
I'm pretty sure I'm missing something elementary but I cannot figure it out now :P.
First, the $[...] bash syntax is obsolete and should not be used.
Now, it evaluates its content as an arithmetic expression. When you put in a string, is is interpreted as a variable, and its value again evaluated as an arithmetic expression. When a variable is unset, it evaluates to zero.
If you put a string with a single word, these rules make it expand to zero. If you put in a string with multiple words, the expansion fails, because the words are interpreted as variables, with no intermediate arithmetic operator, which is an error. This is what you see.
What you probably want, is replace the line value=$[${CfgData[$COUNTER]}] with value=${!CfgData[$COUNTER]}.
Let me add that your script would probably be better written without the indices, like
for varname in "${CfgData[#]}"; do
value=${!varname}
echo -ne "$varname=$value"
done

Resources