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"
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[@]}"
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:
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.
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.
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.
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.
Taking out the declare statement makes the code executable by both bash and ksh .