instead of return
just echo the value
then store it
numIn=$(romanConvert params)
[also, not clear on your use of numIn, it is not initialized in the first invocation]
Edit:
Based on your full sample here is a way this could work, ut note that because the function has 2 outputs (the next inNum and the rchar) I actually hacked by echoing the rchar
to the stderr
while using stdout
for numIn
). As I said its a hack, but tried to minimize changes to your script. Alternatives could be to have the function return something that can be captured in an array or other solutions like not using a function at all
# numIn reads in user input from command line as an argument.
# numMax sets the maximum input by user.
# numMin sets the minimum input by user.
numIn=$1
numMax=3999
numMin=1
#Checks to see if command line was empty, if empty, prompts for an input.
if
[[ "$numIn" == "" ]]; then
read -p "Please enter a number between 3999 and 1: " numIn
if
[[ "$numIn" -gt "$numMax" ]]; then
read -p "Please enter a number smaller than 4000: " numIn
fi
if
[[ "$numIn" -lt "$numMin" ]]; then
read -p "Please enter a number larger than 0: " numIn
fi
fi
#Displays numer entered for conversion.
echo "The number you entered is $numIn."
output=""
romanConvert ()
{
### Changed to use passed-in-value
number=$1
factor=$2
rchar=$3
let "remainder = number - factor"
while [ "$remainder" -ge 0 ]
do
echo -n $rchar >&2
let "number -= factor"
let "remainder = number - factor"
done
echo $number
}
numIn=$(romanConvert $numIn 1000 M)
numIn=$(romanConvert $numIn 900 CM)
numIn=$(romanConvert $numIn 500 D)
numIn=$(romanConvert $numIn 400 CD)
numIn=$(romanConvert $numIn 100 C)
numIn=$(romanConvert $numIn 90 XC)
numIn=$(romanConvert $numIn 50 L)
numIn=$(romanConvert $numIn 40 XL)
numIn=$(romanConvert $numIn 10 X)
numIn=$(romanConvert $numIn 9 IX)
numIn=$(romanConvert $numIn 5 V)
numIn=$(romanConvert $numIn 4 IV)
numIn=$(romanConvert $numIn 1 I)
echo
### NOT NEEDED
exit