9*9 multiplication table

Source: Internet
Author: User
Method 1:
#!/bin/bashdeclare -i i=1declare -i j=1declare -i count=10while [ $i -lt $count ];do for ((j=1;j<=$i;j++));do  echo -ne "$j*$i=$(($j*$i))\t"  if [ $j -eq $i ];then  echo -e '\r'  fi donei=`expr $i + 1`done

Method 2:

#!/bin/bashlet j=1while (($j<10))do i=1 while (($i<=$j)) do    echo -n "$i*$j=$(($i*$j)) "   if [ $i -eq $j ]     then  echo " "     else  :   fi   let "i=i+1" done let "j=j+1"done

Method 3:

#!/bin/bashdeclare -i i=1declare -i j=1declare -i sum=9for ((i=1;i<=sum;i++));do  for ((j=1;j<=i;j++));do     if [ $i -eq $j ];then     echo -e "$j*$i=$(($j*$i))"     else     echo -en "$j*$i=$(($j*$i))\t"     fi  donedone

Note:

**************************************** **************************************** *******

[Oracle @ sor-sys ZY] $ echo-en "Haha \ nhuhu \ r"
Haha
[Oracle @ sor-sys ZY] $ echo-e "Haha \ nhuhu"
Haha
Huhu
[Oracle @ sor-sys ZY] $ echo-en "Haha \ nhuhu"
Haha
Huhu [Oracle @ sor-sys ZY] $ echo-n "Haha \ nhuhu"
Haha \ nhuhu [Oracle @ sor-sys ZY] $ echo "Haha \ nhuhu"
Haha \ nhuhu
[Oracle @ sor-sys ZY] $

**************************************** **************************************** *******

Test results:

[root@sor-sys zy]# sh new9.sh 1*1=11*2=2   2*2=41*3=3   2*3=6   3*3=91*4=4   2*4=8   3*4=12  4*4=161*5=5   2*5=10  3*5=15  4*5=20  5*5=251*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=361*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=491*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=641*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81

(2) bubble Algorithm

We have the following problem:

#! /Bin/bash
A = (12 6 89 48 2)

Array (){
For (I = 0; I <= 4; I ++); do
If [$ {A [$ I + 1]}-lt $ {A [$ I]}]; then
Tmp2 =$ {A [$ I]}
Tmp1 =$ {A [$ I + 1]}
A [$ I + 1] = $ tmp2
A [$ I] = $ tmp1
Fi
Done
}

If [$ {A [0]}-GT $ {A [4]}]; then
Old =$ {A [0]}
Little =$ {A [4]}
A [0] = $ little
A [4] = $ old
# Array
Array
Else
Array
Fi

Echo $ {A [*]}

I have written this before, but there are always problems. I asked my colleagues to know that it is not a syntax error or a logic error. For example, the number on the left will be compared with the number on the right.

When the comparison between the fourth and fifth ends, the fifth and sixth sides will be compared, but the sixth element does not exist, so an error will be reported ~

The test is as follows:

[Root @ test230 test] # sh-n Array. Sh [syntax tree error (⊙ o ⊙) OH ~]

[Root @ test230 test] # sh array. Sh
Array. sh: Line 6: [:-lt: unary operator expected
2 6 48 12 89

The following is the bubble algorithm ~

#! /Bin/bash
Values = (39 5 36 12 9 3 2 30 4 18 22 1 28 25)
Num =$ {# values [*]}
ECHO before:
Echo $ {values [*]}

For (I = 0; I <num; I ++); do
Lowest = $ I
For (j = I + 1; j <num; j ++); do
If [$ {values [J]}-Le $ {values [lowest]}]; then
Lowest = $ J
Fi
Temp =$ {values [I]}
Values [I] =$ {values [lowest]}
Values [lowest] = $ temp
Done
Done

Echo after:
Echo $ {values [*]}

[Root @ test230 test] # sh array. Sh
Before:
39 5 36 12 9 3 2 30 4 18 22 1 28 25
After:
1 2 3 4 5 9 12 22 18 28 30 36 39

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.