Check if Array1 length is equal to Array2 length (bash) - bash

In a for loop I am looking to find out: If Array1 length matches Array2 length then break the for loop.
Shellcheck throws an error (while the script runs fine)
if [[ "${!Array1[#]}" == "${!Array2[#]}" ]] ; then
break;
fi
^-- SC2199: Arrays implicitly concatenate in [[ ]]. Use a loop (or explicit * instead of #).
I'm still learning bash and my teacher said "Always verify with Shellcheck" and to "always place conditionals in double [[ ]]" and to "never use * for array length"
The error(s) are removed with the correction
if [ "${!Array1[*]}" == "${!Array2[*]}" ] ; then
break;
fi
I was wondering what is the best practice here?

Your code is partly correct. The problem is, that you are expanding the array's indicies with th ! operator, and not the length by using the # operator.
Thus a warning about implicit concatenation is issued for both uses of ${array[#]}, as the list of indicies is 0 1 2 3 .... Nevertheless your code is working, as two non associative bash arrays with equal length will have identical index lists 0 1 2 .. N.
To get rid of the warning, you should replace ${!array[#]} with ${#array[#]}. Of course, using ${!array[*]} will also suppress the warning, but that is definitely not what you want to do here, as you would continue comparing index lists.
For further reading: The author of shellcheck has explained the concatenation issue here in detail.

Nyronium's works well and explains the solution perfectly.
I also found a solution that utilities # to check the array length for the condition.
Sorry I did not have this example script before, as I wrote it to test logic afterwards.
Example
#!/bin/bash
array1=(1 2 3)
array2=()
echo "array1 length: ${#array1[#]}" #output :3
for i in {1..10} ; do
echo "array2 length: ${#array2[#]}"
array2+=("$i")
#I want to exit when array2 = a length of 3
if [[ "${#array1[#]}" == "${#array2[#]}" ]] ; then
echo "exit"
break
fi
done
echo "final array1 length: ${#array1[#]} vs array2 length: ${#array1[#]}"
Result: final array1 length: 3 vs array2 length: 3

Related

How can i make my arguments my starting and ending number in my loop

my script
how can i make the first argument become the first number in the loop and make my second argument become the second number in the loop?
We can use a new variable $counter to avoid changing the value of your variables as follows. Obviously you can do other things with the values than echo them to STDOUT.
#!/bin/bash
bhuser1=2
bhuser2=5
counter=$bhuser1
while [ $counter -le $bhuser2 ]
do
echo $counter
((counter++))
done
output
2
3
4
5
[Execution complete with exit code 0]
I have found the following tutorial helpful: https://ryanstutorials.net/bash-scripting-tutorial/bash-loops.php
The range expression { .. } unfortunately does not accept variables, because range expansion happens before parameter expansion. You have to implement a countint loop explicitly, something like.
for ((i=bhuser1; i<=bhuser2; i+=1))
do
....
done

How do I subtract 1 from an array item in BASH?

I have a side-project in BASH for fun, and I have this code snippet (ARRAY[0] is 8):
while [ $ALIVE == true ]; do
$ARRAY[0] = ${ARRAY[0]} - 1
echo ${ARRAY[0]}
done
However, it comes back with this error:
line 16: 8[1]: command not found
I just started working in BASH, so I might be making an obvious mistake, but I've searched and searched for an answer to a problem like this and came up with no result.
The smallest change is simply:
ARRAY[0]=$(( ${ARRAY[0]} - 1 ))
Note:
No $ before the name of the variable to assign to (foo=, not $foo=)
No spaces around the = on the assignment
$(( )) is the syntax to enter a math context (and expand to the result of that operation).

grub2 howto increment variable

The grub2 shell aims to be a minimalistic bash like shell.
But how can I increment a variable in grub2?
In bash I would do:
var=$((var+1))
or
((var=var+1))
In grub2 I get a syntax error on these calls. How can I achieve this in the grub2 shell?
Grub2 does not have builtin arithmetic support. You need to add Lua support if you want that, see this answer for details.
Based on this answer (as already linked by other answer), the following appears to work with GRUB's regexp command (allows incrementing from any number 0-5, add more <from>,<to> pairs as needed):
num=0
incr="" ; for x in 0,1 1,2 2,3 3,4 4,5 5,6 ; do
regexp --set=1:incr "${num},([0-9]+)" "${x}"
if [ "$incr" != "" ] ; then
echo "$num incremented to $incr"
num=$incr
break
fi
done
Decrementing similarly works (just flipping two the regular expression parts):
num=6
decr="" ; for x in 0,1 1,2 2,3 3,4 4,5 5,6 ; do
regexp --set=1:decr "([0-9]+),${num}" "${x}"
if [ "$decr" != "" ] ; then
echo "$num decremented to $decr"
num=$decr
break
fi
done

Solving a (simple) numeric exercise in bash

Some of you are probably familiar with Project Euler, and I'm currently attempting a few of their problems to teach myself some more bash. They're a bit more mathematical than 'script-y' but it helps with syntax etc.
The problem currently asks me to solve:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
The code I have looks like so:
#!/bin/bash
i="1"
for i in `seq 1 333`
do
threes[$i]=`calc $i*3` # where 'calc' is a function written in bashrc
#calc actually looks like: calc() {awk "BEGIN { print "$*"} }
let "sumthrees = sumthrees + ${threes[$i]}"
done
for i in `seq 1 199`
do
fives[$i]=`calc $i*5`
let "sumfives = sumfives + ${fives[$i]}"
done
let "ans = $sumfives + $sumthrees"
echo "The sum of all 3 factors is $sumthrees and the sum of all five factors is $sumfives"
echo "The sum of both is $ans"
#So I can repeatedly run the script without bash remembering the variables between executions
unset i
unset fives
unset threes
unset sumfives
unset sumthrees
unset ans
So far I've not gotten the correct answer, but have run out of ideas as to where I'm going wrong. (FYI, the script currently gives me 266333, which I believe is close, but I don't know the answer yet.)
Can anyone spot anything? And for my own learning, if there are more elegant solutions to this that people might like to share that would be great.
EDIT
Thanks for all the answers, super informative. Since there are so many useful answers here I'll accept my favourite as the proper thread answer.
Blue Moon pointed out the actual problem with your logic.
You don't need to store all the threes and fives in arrays because you don't need them later.
You don't need to unset variables at the end of a script if you use ./yourscript or bash script because they'll disappear along with the
shell instance (better to initialize them first in any case).
You don't need awk to do math, bash does that just fine.
seq and let are not the best way to do anything in a bash script.
Here's a straight forward version:
#!/bin/bash
sum=0
for ((i=1; i<1000; i++))
do
if (( i%3 == 0 || i%5 == 0 ))
then
(( sum += i ))
fi
done
echo "$sum"
Your logic is almost right except that there are numbers which divide by both 3 and 5. So you are adding these numbers twice. Hence, you get wrong answer.
Use another loop similar to ones you have and subtract the ones that divide by both 3 and 5 from the result.
A few tips you might find useful:
In bash, you use let to give the shell a hint that a variable should be considered a number. All bash variables are strings, but you can do arithmetic on numerical strings. If I say let i=1 then i is set to 1, but if I say let i="taco" then $i will be 0, because it couldn't be read as a number. You can achieve a small amount of type-safety when doing mathematical work in the shell.
Bash also has $((this)) mechanism for doing math! You can check it out yourself: echo $((2 + 2)) -> 4, and even more relevant to this problem: echo $((6 % 3 == 0)) -> 1
In case you aren't familiar, % divides the first number by the second, and gives back the remainder; when the remainder is 0, it means that the first is divisible by the second! == is a test to see if two things are equal, and for logical tests like this 1 represents true and 0 represents false. So I'm testing if 6 is divisible by 3, which it is, and the value I get back is 1.
The test brackets, [ ... ] have a "test for equality" flag, -eq, which you can use to check if a math expression has a certain value (man test for more details)!
$ let i=6
$ echo $((i % 3 == 0 || i % 5 == 0))
1
$ if [ $((i % 3 == 0 || i % 5 == 0)) -eq 1 ]; then echo "yes"; fi
yes
(|| is another logical test - $((a || b)) will be 1 (true) when a is true or b is true).
Finally, instead of doing this for the number 6, you could do it in a for loop and increment a sum variable every time you find a multiple of 3 or 5:
let sum=0
for i in {1..1000}; do
if [ $((i % 3 == 0 || i % 5 == 0)) -eq 1 ]; then
let sum=$((sum + i))
fi
done
echo $sum
And there you'd have a working solution!
Bash has a lot of nice little tricks, (and a lot more mean ugly tricks), but it's worth learning at least a handful of them to make use of it as a scripting tool.
How about creative use of the modulus function & some checks. Then you have just 1 loop.
#!/bin/bash
i=1
while [ $i -lt 1000 ]
do
if [ $(($i % 3)) -eq 0 ] || [ $(($i % 5)) -eq 0 ]
then
sumall=$(($sumall+$i))
fi
i=$(($i+1))
done
echo "The sum of both is $sumall"
Answer: 233168
A different solution:
#!/bin/bash
sum=0
for n in {1..999}; do [ $(((n%5) * (n%3))) -eq 0 ] && sum=$((sum+n)); done
echo $sum
The script loops through all numbers below 1000, tests if the product of the number mod 3 and the number mod 5 is 0 (the product of two numbers can only be zero if one of them is zero). If that is the case, it adds the current number to a sum, which is printed out afterwards.
By the way, if I were you I'd include the definition of the calc function inside the script, to get a self-contained solution that doesn't need your specific configuration.

Array intersection in Bash [duplicate]

This question already has answers here:
Intersection of two lists in Bash
(5 answers)
Closed 6 years ago.
How do you compare two arrays in Bash to find all intersecting values?
Let's say:
array1 contains values 1 and 2
array2 contains values 2 and 3
I should get back 2 as a result.
My own answer:
for item1 in $array1; do
for item2 in $array2; do
if [[ $item1 = $item2 ]]; then
result=$result" "$item1
fi
done
done
I'm looking for alternate solutions as well.
The elements of list 1 are used as regular expression looked up in list2 (expressed as string: ${list2[*]} ):
list1=( 1 2 3 4 6 7 8 9 10 11 12)
list2=( 1 2 3 5 6 8 9 11 )
l2=" ${list2[*]} " # add framing blanks
for item in ${list1[#]}; do
if [[ $l2 =~ " $item " ]] ; then # use $item as regexp
result+=($item)
fi
done
echo ${result[#]}
The result is
1 2 3 6 8 9 11
Taking #Raihan's answer and making it work with non-files (though FDs are created)
I know it's a bit of a cheat but seemed like good alternative
Side effect is that the output array will be lexicographically sorted, hope thats okay
(also don't kno what type of data you have, so I just tested with numbers, there may be additional work needed if you have strings with special chars etc)
result=($(comm -12 <(for X in "${array1[#]}"; do echo "${X}"; done|sort) <(for X in "${array2[#]}"; do echo "${X}"; done|sort)))
Testing:
$ array1=(1 17 33 99 109)
$ array2=(1 2 17 31 98 109)
result=($(comm -12 <(for X in "${array1[#]}"; do echo "${X}"; done|sort) <(for X in "${array2[#]}"; do echo "${X}"; done|sort)))
$ echo ${result[#]}
1 109 17
p.s. I'm sure there was a way to get the array to out one value per line w/o the for loop, I just forget it (IFS?)
Your answer won't work, for two reasons:
$array1 just expands to the first element of array1. (At least, in my installed version of Bash that's how it works. That doesn't seem to be a documented behavior, so it may be a version-dependent quirk.)
After the first element gets added to result, result will then contain a space, so the next run of result=$result" "$item1 will misbehave horribly. (Instead of appending to result, it will run the command consisting of the first two items, with the environment variable result being set to the empty string.) Correction: Turns out, I was wrong about this one: word-splitting doesn't take place inside assignments. (See comments below.)
What you want is this:
result=()
for item1 in "${array1[#]}"; do
for item2 in "${array2[#]}"; do
if [[ $item1 = $item2 ]]; then
result+=("$item1")
fi
done
done
If it was two files (instead of arrays) you were looking for intersecting lines, you could use the comm command.
$ comm -12 file1 file2
Now that I understand what you mean by "array", I think -- first of all -- that you should consider using actual Bash arrays. They're much more flexible, in that (for example) array elements can contain whitespace, and you can avoid the risk that * and ? will trigger filename expansion.
But if you prefer to use your existing approach of whitespace-delimited strings, then I agree with RHT's suggestion to use Perl:
result=$(perl -e 'my %array2 = map +($_ => 1), split /\s+/, $ARGV[1];
print join " ", grep $array2{$_}, split /\s+/, $ARGV[0]
' "$array1" "$array2")
(The line-breaks are just for readability; you can get rid of them if you want.)
In the above Bash command, the embedded Perl program creates a hash named %array2 containing the elements of the second array, and then it prints any elements of the first array that exist in %array2.
This will behave slightly differently from your code in how it handles duplicate values in the second array; in your code, if array1 contains x twice and array2 contains x three times, then result will contain x six times, whereas in my code, result will contain x only twice. I don't know if that matters, since I don't know your exact requirements.

Resources