Saving Mod in a variable

Hello Experts,
In one of my shell script, I've been trying to calculate mod and saving it in a variable, below is what I have tried but it isn't working.
Any help appreciated!!!

#!/bin/bash
num1=4
num2=3
echo "Number one is $num1"
echo "Number two is $num2"
mod_final=$(( echo "num1%num2" | bc ))
echo "Final Mod is $mod_final"

You don't need bc's help to do this.

num1=4
num2=3
mod_final=$(( num1%num2 ))

You are

  • using "Arithmetic Expansion" in lieu of "Command Substitution"
  • NOT expandig the variables num1 and num2
1 Like

And if you *want* bc, maybe for handling very long numbers, then it is

num1=12345678901234567890
num2=46
mod_final=$( echo "$num1%$num2" | bc )

=>Command substitution.

1 Like

Hello Experts,

How can i do this with array, this is what I am trying :

num=$( echo "${arr}%2" | bc )

If we only knew WHAT you want to do?

Hello RuciC,

Apologies for not being clear enough, I have been trying to create script that will remove number divisible by 2 from array, this is what I've tried :

#!/bin/bash
echo "This is a shell script to remove number divisible by 2 from the array"
declare -a arr=(1 6 9 12 15)
echo "Original array is ${arr[@]}"
for (( i=0; i<5; i++ ))
do
   num=$( echo "${arr}%2" | bc )
   if [[ $num -eq 0 ]]
   then
       arr=(${arr[@]:0:(( $i-1))} ${arr[@]: (( $i+1 )) })
       #echo "Number divisible"
   fi
done
echo "Modified array is ${arr[@]}"

Error being encountered is :

(standard_in) 1: syntax error
(standard_in) 1: syntax error

This might be a suitable script to do what the comments in your code say it is trying to do:

#!/bin/bash
echo "This is a shell script to remove numbers divisible by 2 from an array"
if [ $# -gt 0 ]
then	# Use commnand line arguments as array elements.
	arr=($@)
else	# Use default array elements.
	arr=(1 6 9 12 15)
fi
case ${#arr[@]} in
(0)	echo 'Original array is empty.';;
(1)	echo "Original array element is ${arr[@]}.";;
(*)	echo "Original array elements are ${arr[@]}.";;
esac

# For each element in the array...
for (( i=0; i<${#arr[@]};  ))
do
	# ... determine if that element is evenly divisible by 2.
	if ((arr % 2))
	then
		# Element i is not evenly divisible.  Evaluate next element.
		echo "Number (arr[$i]=${arr}) is not divisible"
		((i++))
	else
		# Element i is evenly divisible; remove it from the array and
		# re-evaluate element i.
		echo "Number (arr[$i]=${arr}) is divisible"
		arr=(${arr[@]:0:i} ${arr[@]:i+1})
		echo "Modified array is ${arr[@]}."
	fi
done
case ${#arr[@]} in
(0)	echo 'Final array is empty.';;
(1)	echo "Final array element is ${arr[@]}.";;
(*)	echo "Final array elements are ${arr[@]}.";;
esac

Here are a few of the reasons for changes made to your code:

  1. There is no need to invoke bc to perform mod operations since arithmetic expansions in the shell can do that just as well as long as the values in your array are not larger than values that can be assigned to a long integer in C.
  2. When an array element is removed from your array, you need to keep i (not i-1 ) elements of the array before ${arr} is removed. Note that array elements are numbered starting with 0; not starting with 1.
  3. When array element i is removed from your array, you can't increment i. If you do increment i after removing an element, the new arr [i]won't be checked to see if it is divisible by 2.
  4. The errors you were getting are because your loop end check is based on the original size of your array, not the array size after elements are removed from your array. Therefor, the last two times you invoke bc you are invoking it with non-existent elements of your array as an argument to be processed.
  5. Taking out the declare statement makes the code executable by both bash and ksh .
1 Like