#!/bin/ksh
number1="20"
if [ $1 ]
then
number1=$1
fi
number2="1"
while [[ $number2 -lt $number1 -o $number2 -eq $number1 ]]
do
if [[ let "number2 % 3" -eq 0 -a let "number2 % 5" -eq 0 ]]
then
print "FizzBuzz"
elif [[ let "number2 % 3" -eq 0 ]]
then
print "Fizz"
elif [[ let "number2 % 5" -eq 0 ]]
then
print "Buzz"
else
print $number2
fi
let "number2++"
done
I am very new to scripting so sorry if this is really messed up. The intention is to use the command line to get a number from the user and then print the numbers 1 through n. only if n is divisble by 3, 5, or both it prints fizz buzz and fizzbuzz instead. also if the command is not enetered the number is set to 20.
With this code i get the error [[: not found and if i only use single "[";s i get test: number2 % 3 unkown operator. please help ive been stuck on this for a while now. thank you. (do i need to set the PATH variable to something specific?) Also this is being done in korn shell
#!/bin/ksh
number=${1:-20}
for (( i=1; i<=$number; i++ )); do
if ! let "i % 3 || i % 5 "; then
echo "$i FizzBuzz"
elif ! let "i % 3"; then
echo "$i Fizz"
elif ! let "i % 5"; then
echo "$i Buzz"
fi
done
or the (( )) version:
#!/bin/ksh
number=${1:-20}
for (( i=1;i<=$number;i++ )); do
if ! (( i % 3 || i % 5 )); then
echo "$i FizzBuzz"
elif ! (( i % 3 )); then
echo "$i Fizz"
elif ! (( i % 5 )); then
echo "$i Buzz"
fi
done
Posix:
number=${1:-20}
i=1
while [ $i -le $number ]; do
if [ $(( i % 3 )) -eq 0 ] && [ $(( i % 5 )) -eq 0 ]; then
echo "$i FizzBuzz"
elif [ $(( i % 3 )) -eq 0 ]; then
echo "$i Fizz"
elif [ $(( i % 5 )) -eq 0 ]; then
echo "$i Buzz"
fi
i=$((i+1))
done