开发者

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?


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`


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


This syntax isn't supported in all shells:

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

Try this instead:

for j in {1..12}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜