Question

I've been reading up on this script and some of its sources but I cannot figure out how to used numbers larger than 255? I read something about standard out, but I am extremely new to this. Thanks in advance for any help!

I DO WANT TO NOTE THE LATTER HALF OF THIS ORIGINAL POST IS NOT MY SCRIPT!!! It is a bash roman numeral script I found on google!

# 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."

THIS romanConvert IS NOT MINE!!! THIS IS THE PORTION I FOUND AND WAS TRYING TO FIGURE OUT!!! I changed some names and tried to write it myself the way my weak understanding would go about naming things.

romanConvert ()
{
number=$numIn
factor=$2
rchar=$3
let "remainder = number - factor"
while [ "$remainder" -ge 0 ]
do
    echo -n $rchar
    let "number -= factor"
    let "remainder = number - factor"
done  
return $number
}

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
exit
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top