打赏 jQuery火箭图标返回顶部代码

Shell运算符

1)算数运算符

1)常见的算数运算符,如下图:

说明:变量a在运算符之前,输出表达式的值为a,然后a自增或自减;变量a在运算符之后,输出表达式会先自增或自减,表达式的值就是自增或自减后a的值。

常见的命令运算命令,如下图;

我们来实践一下吧,

 1 [root@king scripts]# cat test.sh
 2 #! /bin/bash
 3 a=$1                #直接把特殊位置参数变量$1赋值给a,
 4 b=$2               #并且把特殊位置参数变量$2赋值给b,这样,脚本传参的内容就会赋值给a和b。
 5 echo "a-b=$(($a-$b))"
 6 echo "a+b=$(($a+$b))"
 7 echo "a*b=$(($a*$b))"
 8 echo "a/b=$(($a/$b))"
 9 echo "a**b=$(($a**$b))"
10 echo "a%b=$(($a%$b))"
11 [root@king scripts]# sh test.sh 6 2
12 a-b=4
13 a+b=8
14 a*b=12
15 a/b=3
16 a**b=36
17 a%b=0

 我们来模仿一下计算器吧,

 1  #! /bin/bash
 2  #add, subtract, multiply and divide   
 3  print_usage(){                  #定义一个函数,名字为print_usage。
 4    printf "Please enter an integer\n"        #打印符合脚本要求的提示信息。
 5     exit 1           #返回值1退出脚本
 6  }
 7  read -p "Please input first number: " firstnum  #请输入一个数字
 8  if [ -n "`echo $firstnum|sed 's/[0-9]//g'`" ]; 
 9     then                   #判断是否为整数,删除读入内容的数字部分看是否为空(-n功能),进而判断读入的内容是否为数字。
10     print_usage            #如果上述条件变量值不为空,说明不是整数,则调用用户帮助函数。
11  fi
12     read -p "Please input the operators: " operators      #继续读入运算符。
13     if [ "${operators}" ! = "+" ]&&[ "${operators}" ! = "-" ] && [ "${operators}"
14 != "" ] && [ "${operators}" ! = "/" ]; 
15         then       #判断第二个输入内容操作符是否为+-*/任意运算符之一。
16         echo "please use {+|-|*|/}"         #如果操作符不符合要求,则给出提示。
17         exit 2      #不符合要求,返回值2退出脚本

let运算命令的用法

语法格式:let赋值表达式,功能等同于((赋值表达式))

1 [root@king scripts]# i=2
2 [root@king scripts]# i=i+8      #不用let进行赋值。
3 [root@king scripts]# echo $i    #打印结果为i+8,也就是没有计算。
4 i+8
5 [root@king scripts]# unset i
6 [root@king scripts]# i=2
7 [root@king scripts]# let i=i+8 #用let赋值再输出。
8 [root@king scripts]# echo $i
9 10  #结果为10

expr运算命令的用法

expr(evaluate (求值)expressions(表达式))命令即可以用于整数运算,也可以用于相关字符串长度,匹配等的运算处理

 1 [root@king scripts]# expr 2 + 2
 2 4
 3 [root@king scripts]# expr 2 - 2
 4 0
 5 [root@king scripts]# expr 22 #*号用\来转义。
 6 expr: 语法错误
 7 [root@king scripts]# expr 2 \* 2
 8 4
 9 [root@king scripts]# expr 2 / 2
10 1

说明:运算符及用计算的数字之间都要有空格,用乘号时,得用\转义。

expr计算字符串的长度,说明,空格也算一个字符。

1 [root@king scripts]# char="Hello world"
2 [root@king scripts]# expr length "$char" #利用expr的length函数计算字符串长度。
3 11
4 [root@king scripts]# echo ${#char}#计算变量子串长度的方法。
5 11
6 [root@king scripts]# echo ${char}|wc -L    #wc方法
7 11
8 [root@king scripts]# echo ${char}|awk '{print length($0)}' #利用awk的length函数来计算字符串的长度。
9 11

bc命令的用法,bc是UNIX/LIinux下的计算器,如计算输出1+2+3+...+10的表达式:

1 [root@king scripts]# seq -s "+" 10    # seq是生成数字序列,-s是指定数字序列之间的分隔符。
2 1+2+3+4+5+6+7+8+9+10
3 [root@king scripts]# echo {1..10}|tr " " "+"    #{1..10}是生成以空格为间隔的数字序列,并交给tr将空格替换为+号。
4 1+2+3+4+5+6+7+8+9+10
5 [root@king scripts]# echo `seq -s '+' 10`=`seq -s "+" 10|bc` #使用bc计算
6 1+2+3+4+5+6+7+8+9+10=55

$[]符号运算,经典问题示例,打印数学杨辉三角,Shell实现:

 1 #! /bin/bash
 2 if (test -z $1) ; then      #判断传参的值长度是不是为0,如果没有传入参数,则使用read读入。
 3     read -p "Input Max Lines:" MAX  #read读入一个数值。
 4 else
 5     MAX=$1                  #如果已经传参了,就把传参的$1赋值给MAX。
 6 fi
 7 i=1
 8 while [ $i -le $MAX ]       #i行控制。
 9 do
10     j=1
11     while [ $j -le $i ]     #j列控制。
12     do
13         f=$[i-1]            #f=i-1是$[]计算写法。
14         g=$[j-1]            #g=j-1是$[]计算写法。
15         if [ $j -eq $i ] || [ $j -eq 1 ] ; then
16             declare SUM_${i}_$j=1                     #声明变量头尾都是1。
17         else
18             declare A=$[SUM_${f}_$j]                  #取上一行的j列变量。
19             declare B=$[SUM_${f}_$g]                  #取上一行的j-1列变量。
20  declare SUM_${i}_$j=`expr $A + $B` #声明并计算当前变量的值。
21         fi
22         echo -en $[SUM_${i}_$j]" "                    #输出当前变量。
23         let j++                                       #let运算用法。
24     done
25     echo                                              #换行。
26     let i++                                           #let运算用法。
27 done

Java实现杨辉三角

 1 public class SanJiao {
 2 public static void main(String[] args) {
 3 int row = 10; // 三角的行数
 4 int[][] result = new int[row][row];
 5 for (int i = 0; i < row; i++) { //
 6 result[i][0] = 1;
 7 System.out.print(result[i][0] + "\t");
 8 for (int j = 1; j <= i; j++) { //
 9 result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
10 System.out.print(result[i][j] + "\t");
11        }
12 System.out.println();
13      }
14    }
15 }

read命令基础

Shell变量除了可以直接赋值或脚本传参外,还可以使用read命令从标准输入汇总获得,语法格式:read[参数][变量名],常用的参数 -p prompt 设置提示信息,-t timeout 设置输入等待的时间,单位默认为妙

1 [root@king scripts]# read -t 10 -p "Pls input one num:" num  #读入一个输入,赋值给num变量,注意,num变量前需要有空格。
2 Pls input one num:18                #输出数字18,相当于把18赋值给num变量。
3 [root@king scripts]# echo $num #输出变量值。
4 [root@king scripts]# read -p "please input two number:" a1 a2 #读入两个输入,注意要以空格隔开,分别赋值给a1和a2变量,a1变量前后都需要有空格。
5 please input two number:1 2
6 [root@king scripts]# echo $a1
7 1
8 [root@king scripts]# echo $a2
9 2

 

posted @ 2018-10-26 15:17  浪漫De刺猬  阅读(1292)  评论(0编辑  收藏  举报