My problem has to possible starting points.
Well, not really, but getting to either one is no problem at all.
So i got either a string in the format of "1920x1080" or simply the integers X = 1920 and Y = 1080.
When I am done, I would like to have an output as the following "16x09" or at least X = 16 and Y = 9, which of course could be simply concatenated.
As I am fairly new to shell-scripting, I got no idea how to even begin, and neither google nor the search were of help to me..
Meanwhile I tried to write something on my own and finally managed to do so..
It's not pretty and it's not optimized, but it works..
Would be nice, if s.o. could tell me, if I made any mistakes,
or if s.th. could/should be made differently..
Here is my bash-version using: comm, cut, factor, let, sed and tr
#!/bin/bash
## reduce2ints.sh
#
# returns the reduced values of 2 given integers.
#
# error-handling
if [[ ( "$#" -lt 2 ) ]]; then
echo -e "Error!!!\x0ATry \"$0 value1 value2\"";
exit 1;
else
# put parameters in own variables
X=$1;
Y=$2;
# split values into their prime-factors and put them in arrays
declare -a XPrimes=$(factor $X | cut -d$'\x20' -f2-);
declare -a YPrimes=$(factor $Y | cut -d$'\x20' -f2-);
# subtract arrays from eachother to get the differences
declare -a RestX=("$(comm -23 <(echo ${XPrimes} | sed 's/ /\n/g') <(echo ${YPrimes} | sed 's/ /\n/g') | tr "\n" " ")");
declare -a RestY=("$(comm -13 <(echo ${XPrimes} | sed 's/ /\n/g') <(echo ${YPrimes} | sed 's/ /\n/g') | tr "\n" " ")");
# prepare outvariables
NewX=1;
NewY=1;
# calculate the 'rests'
for element in ${RestX[@]}; do
let NewX=$NewX*$element;
done
for element in ${RestY[@]}; do
let NewY=$NewY*$element;
done
# output the solutions
echo "1. Reduced Value=$NewX";
echo "2. Reduced Value=$NewY";
fi
# EOF