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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM