nisha
nisha

Reputation: 31

shell script for multiplication table

This is my code

mul.sh

echo "multiplication table"
echo "enter number"
read n
m=0
for((j=1;j<=12;j++))
do
echo -n -e " $j\t"
done
echo ""
echo 
for((i=1;i<=n;i++))
do
for((k=1;k<=12;k++))
do
m=` expr $k \* $i `
echo -n -e " $m\t"
done
echo ""
done

When I run this I got:

malathy@malathy:~/Desktop/fosslab/20084664/shell$ sh matrix.sh

multiplication table

enter number

2

matrix.sh: 5: Syntax error: Bad for loop variable

Can anyone help me to solve this?

Upvotes: 0

Views: 30645

Answers (3)

Renjith
Renjith

Reputation: 11

try this for multiplication simple and easy

echo Enter the multiplication number required:
read number
for i in 1 2 3 4 5 6 7 8 9 10
do
echo "$number * $i =`expr $number \* $i`"
done

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360133

You're running a script that uses syntax that is not supported in the shell you're using. Either change the first line of your script to:

#!/bin/bash

The Korn shell (at least some versions) and zsh also support the form of the for statement.

If you're using the Bourne shell (or something vary close like Dash), you need to change the for statement to use seq or jot:

for i in `seq $n`

or

for i in `jot $n`

Upvotes: 1

thomson_matt
thomson_matt

Reputation: 7691

This syntax isn't supported in all shells:

for((j=1;j<=12;j++))

Try this instead:

for j in {1..12}

Upvotes: 0

Related Questions