[关闭]
@Great-Chinese 2016-08-10T01:37:15.000000Z 字数 532 阅读 944

Linux练习

linux练习


将文本三行合并一行

awk 'ORS=NR%3?" ":"\n"{print}' 1.txt

求和

  1. #!/bin/bash
  2. sum=0
  3. index=1
  4. while [ $index -le $1 ]
  5. do
  6. sum=$[$sum+$index];
  7. index=$[$index+1]
  8. done
  9. echo $sum

判断输入的是否为数字

  1. #!/bin/bash
  2. if [ ! -n "$1" ]
  3. then
  4. echo "Please input a number" ;
  5. exit 0
  6. fi
  7. result=`echo $1 | grep '^[0-9]*$'`
  8. if [ -n "$result" ]
  9. then
  10. echo "$1 Is a Number"
  11. else
  12. echo "$1 Is Not Number"
  13. fi

求规律数

第一个数1
第二个数2
第三个数3
第四个数5
第五个数8
第六个数13

  1. #!/bin/bash
  2. a=1
  3. b=2
  4. i=3
  5. while [ $i -le 50 ]
  6. do
  7. c=$[$a+$b]
  8. a=$b;
  9. b=$c;
  10. i=$[$i+1];
  11. echo "$a -> $b -> $c"
  12. done
  13. echo $c;

乘法口诀

  1. #!/bin/bash
  2. for ((i=1;i<=9;i++))
  3. do
  4. for ((j=1;j<=$i;j++))
  5. do
  6. echo -n "$i*$j="$[$i*$j]" "
  7. done
  8. echo ""
  9. done
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注