-2

Write a short program that prints each number from 1 to 100 on a new line. • For each multiple of 3, print "Fizz" instead of the number. • For each multiple of 5, print "Buzz" instead of the number. • For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.

This what i have so far:

#!/bin/bash


*for i in {0..20..5}
do
  echo "Number: $i"
done*

but i get this:

Number: {0..20..5}

Need help

aynber
  • 22,380
  • 8
  • 50
  • 63
Kachi
  • 17
  • 2
  • The syntax of that kind of loop is `for (( expr1 ; expr2 ; expr3 )) ; do list ; done`. Refer to the `man bash` for more details. – Stephen C Oct 07 '22 at 10:17
  • Assuming that the two asterisks in your code are just a copy-and-paste error, I would conclude from the output you get, that this is not executed by bash, or by a very old version of bash, which does not support the `{...}` construct yet. Put in front of the `for` loop a `echo version=$BASH_VERSION`. – user1934428 Oct 07 '22 at 10:27
  • Indeed, `{x..y..incr}` was added at bash4.0 version, year 2009. So one would assume you're on a mac with the default bash shell. – Jetchisel Oct 07 '22 at 10:30
  • You don't want to skip any numbers anyway; you want to consider *every* value of `i` between 1 and 100, then decide whether to print the number itself, "Fizz", or "Buzz". `for i in {1..100}; do ...; done` would be the correct loop (and it would work in `bash` 3, though I would still use StephenC's suggestion to generate the numbers as needed instead of all at once). The real problem is writing the code that goes in the *body* of the loop. – chepner Oct 07 '22 at 12:08

2 Answers2

0

If it's on a mac, use $(seq 0 5 20) to get the same result.

0

Using GNU sed

$ for ((i=1;i<=100;i++)); do echo "$i"; done | sed -E '0~3s/[0-9]+/Fizz/;0~5s/[0-9]+|$/Buzz/'
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
HatLess
  • 10,622
  • 5
  • 14
  • 32
  • Why would you use `sed` for an arithmetic problem? – chepner Oct 07 '22 at 12:00
  • @chepner It is not clear what OP is asking, so I did each step at a time. 1. Print lines 1-100. 2. Change multiples of 3 to Fizz. 3. Change multiples of 5 to Buzz. 4 Overlaps, print both Fizz and Buzz. Does not exactly seem like an arithmetic problem but rather replacing line numbers with strings – HatLess Oct 07 '22 at 12:06