linux 學習隨筆-shell簡單編寫,隨筆-shell

來源:互聯網
上載者:User

linux 學習隨筆-shell簡單編寫,隨筆-shell

指令碼最好都放在/usr/local/sbin中

指令碼的執行 sh -x 指令碼.sh -x可以查看執行過程1在指令碼中使用變數 使用變數的時候,需要使用$符號: #!/bin/bash  ##把命令賦值為變數,需要使用反引號  d=`date +"%H:%M:%S"`  echo "The script begin at $d"  echo "Now we'll sleep 2 seconds"  sleep 2  d1=`date +"%H:%M:%S"`  echo "The script end at $d" 2在指令碼中使用數學運算要用[]括起來如下#! /bin/bash  a=1 b=2 ##數學運算用[]括起來 sum=$[$a+$b] echo "$a + $b = $sum" 3在指令碼中和控制台互動 使用read命令#! /bin/bash  read -p "Please input a number: " x read -p "Please input a number: " y sum=$[$x+$y] echo "The sum of the tow numbers is : $sum" 4shell中的預設變數#! /bin/bash  echo "$1 $2 $3 $0" 1 2 就是指令碼中的預設變數 一個指令碼中的預設變數是沒有限制的,0表示指令檔本書# sh option.sh 1 2 3 執行此命令輸出內容如下所示:1 2 3 option.sh 5shell中的邏輯判斷不帶else的if 注意if後面的判斷語句要用(())否則會報錯#! /bin/bash  read -p "Please input your score: " a if ((a<60)); then     echo "You didn't pass the exam." fi 5.1帶else 執行個體代碼如下#! /bin/bash  read -p "Please input your score: " a if ((a<60)); then     echo "You didn't pass the exam." else     echo "Good! You passed the exam." fi 5.2帶有else if(這是c中的說法)在shell中表示為elif#! /bin/bash  read -p "Please input your score: " a if ((a<60)); then     echo "You didn't pass the exam." elif ((a>=60)) && ((a<85)); then     echo "Good! You passed the exam." else     echo "Very good! Your score is very heigh" fi 5.3case 邏輯判斷#! /bin/bash  read -p "Input a number: " n a=$[$n%2] case $a in 1)     echo "The number is odd."     ;; 0)     echo "The number is even."     ;; *)     echo "It's not a number"     ;; esac * 表示其他值6for迴圈執行個體代碼如下:#! /bin/bash  for file in `ls`;do     echo $file done 7while 迴圈#! /bin/bash  a=5 while [ $a -ge 1 ]; do     echo $a     a=$[$a-1] done      

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.