技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁(yè) > 教程 > 服務(wù)器類 >

shell語(yǔ)句的基本使用

來(lái)源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2018-09-03 00:02┆點(diǎn)擊:

  echo "please input x"

  read x

  echo "please input y"

  read y

  if [ $x -gt $y ]; 判斷條件的[]中,必須在[只后有空格,]之前有空格。。]只有可以有分號(hào);,也可以沒有分號(hào)

  then

  echo "$x dayu $y"

  elif [ $x -lt $y ];

  then

  echo "$x xiayu $y"

  else

  echo "$x dengyu $y"

  fi

  2.加法(乘法)

  sum=`expr $x + $y` 整數(shù)的計(jì)算,要加expr,并且在+的兩邊要有空格

  echo “the result is $sum”

  如果是乘法。`expr $x * $y`需要“”進(jìn)行轉(zhuǎn)譯

  3.保存前一個(gè)命令的返回碼

  [ “$x” = “$y” ]

  echo $? ?可以用于保存前一個(gè)命令的返回碼。正確的返回0,錯(cuò)誤的返回1

  4.for語(yǔ)句

  total=0

  for ((i=1;i<=100;i++)) for的后面是雙括號(hào)(())。。一般(())的里面是用于一些計(jì)算的

  do

  total=`expr $total + $i`

  done

  echo “the result is $total ”

  5.while語(yǔ)句

  1)Total=0; num=0 在同一行,但是用分號(hào)隔開

  2)total=0 num=0 在同意行,但是沒有分隔符 這三種的效果是一樣的

  3)total=0

  num=0 用兩行寫

  while((num<100)) while也是雙括號(hào)(())

  do

  num=`expr $num + 1`

  total=`expr $total + $num`

  done

  6.until中,條件為真結(jié)束循環(huán)

  until [ $num -ge 100 ] until是[]的進(jìn)行判斷,這里就不能用<了,要是-gt

  do

  num=`expr $num + 1`

  total=`expr $total + $num`

  done

  7. for循環(huán)里面有if的條件判斷

  for((i=1;i<=10;i++)) 這對(duì)這種的,for里面容易忘記寫do和done

  do

  if((i%2==1))

  then

  rcho “jishu is $i”

  else

  echo “oushu is $i”

  fi

  Done

  8. case語(yǔ)句

  read x

  a=$x

  case $a in

  1)

  echo “the num is 1”;; 每一句的最后都是有兩個(gè)分號(hào);;,這是必須的

  2)

  echo “the num is 2”; echo “hehhehe”;; 輸出兩行,并且是換行的,這兩句之間用一個(gè)分號(hào)鏈接;

  3|4)

  echo “the num is 3 or 4”;; 一個(gè)條件有多個(gè)值,這個(gè)時(shí)候用“|”來(lái)隔開

  *)

  echo “the num is other”;; 對(duì)于其他可能出現(xiàn)的條件,用“*”表示

  esac

  9函數(shù)以及調(diào)用

  add() 函數(shù)體

  {

  sum=`expr $x + $y`

  echo “the sum is $ sum”

  }

  echo “please input x”

  read x

  echo “please input y”

  read y

  add $x $y 函數(shù)調(diào)用

  10.腳本的調(diào)用

  在腳本1中如果調(diào)用腳本2。直接在腳本1中添加一行“./script2”