<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      fstream

      博客園 首頁 新隨筆 聯系 訂閱 管理

      最近寫個一些dash shell 相關的代碼,中間遇到了一些坑以及需要注意的地方,記錄一下

      1 參數

      numberofargmuments(){

        echo "The number of args: " "$#"

        # >> 2 參數個數

        # $0 當前執行腳本的相對路徑、

        echo "shell script path:" $0 $1 $2 分別代表第一 第二個參數。。。

        獲取當前腳本的路徑

        currentdir1=$(cd $(dirname $0)|pwd)
        echo "current dir is(cd dirname pwd)": "$currentdir1"

      #說明 $(dirname $0) 獲取當前腳本的目錄的相對路徑,cd 進去 執行pwd獲得全路徑獲取當前腳本的路徑

        currentdir2=$(dirname $(readlink -f $0))

        echo "current dir is(dirname readlink -f)": "$currentdir2"

      }

      1.1 參數的處理 1
      ────────

      $* 除$0以外的所有參數, 是一個完整的字符串$@ 除$0以外的所有參數, 是一個
      數組兩者區別可以用下面的代碼看出來./prac1.sh arg1 arg2
      arguments_deal1(){

        echo print '$*'

        for arg in "$*";do

           echo "$arg"

        done

      arg1 arg2 輸出為一行echo print '$@' for arg in "$@";do echo "$arg"
      done輸出為2行arg1 arg2 } arguments_deal1 $@ arguments_deal1 $*


      1.2 參數處理 2
      ───────

      argment_deal2(){如何取出option可以先判斷$1 or $2 然后再取出option if [
      "$1" = "-m" ];then echo '$0' is -m and value is: $2 fi #or if [ "$2" =
      "-m" ];then echo '$2' is -m and value is: $1 fi }


      1.3 參數處理 3
      ───────

      shift 會將參數往左移動一位,也就相當于刪除第一個參數eg:./prac1.sh -f
      v1 v2 v3 or ./prac1.sh v1 v2 v3 -f arguments_deal3(){ values="" while
      [ -n "$1" ];do if [ "$1" = "-f" ];then shift continue else
      values="$values $1" fi shift done

      echo argments values:"$values" } #arguments_deal3 $@


      2 字符串
      ════════

      2.1 字符串截取
      ───────

      var="/user/home/user1" echo ${var#*home/} # 去掉*home/ 留下 user1


      2.2 split & count
      ────────

      count=0 vars=$(echo $var | tr '/' ' ') # split for v in ${vars};do
      count=`expr $count + 1` # count echo $v done tr commands can referce
      here <http://www.rzrgm.cn/bingguoguo/articles/9188703.html>


      2.3 join
      ────

      joinedstr=""

      for v in ${vars};do

         if [ "$joinedstr" = "" ]; then

          joinedstr=$v

        else

          joinedstr="$joinedstr#$v"

        fi

      done


      3 目錄
      ══════

      3.1 列出絕對路徑
      ────────

      files=$(ls | sed "s:^:`pwd`/: ")

      for f in $files;

      do

        echo $file

      done


      4 函數
      ══════

      4.1 函數的定義函數反回和調用
      ──────────────

      bash可以有關鍵字function 但是dash沒有,一律不使用function 關鍵字函數

      可以返回一些字符串或者數字

      funcreturnstr(){

        echo "returnstring"

      }

      if [ $(funcreturnstr) = "returnstring" ] ;then

        echo "return ok"

      fi

      帶參數

      funcreturnstr(){

      if [ "$1" = "1" ];then

        echo "return1"

      elif [ "$1" = "2" ];then

        echo "return2"

      else

        echo "returnstring"

      fi

      }

      if [ $(funcreturnstr 1) = "return1" ];then

        echo "return1 ok"

      fi

      注意參數$1在比較的時候一定要加雙引號,不要問為什么,誰不加誰知道。

      5 here string
      ═════════════

      在使用while read 的時候會遇到在while里面的變量無法更改 比如total=0

      echo "a,b,c" | tr ',' '\n'|while read x;do

         $((total=total+1))

        echo in while total:$total

      done

      echo out total:$total

      out total打印始終為0 原因就是 代碼是通過管道傳送給while read 進程,
      total變量被傳到了子進程中,而父進程是得不到在子進程中的更改的,要獲得
      這個更改需要使用here documents

      eg:

      total=0

      while read x;do

        total=`expr $total + 1`

        echo in while total:$total

      done<<EOF

      `echo "a,b,c" | tr ',' '\n'`h

      EOF


      6 讀取文件
      ══════════

      文件每行存放的方式為key value 中間是空格,如果要取出key為某一個值的時
      候可以這樣

      while read line;do

      第一種方法

      key=`echo $line | cut -d ' ' -f1`

      if [ "$key" = "key2" ] ;then

        echo first method: "$key"

      fi

      第二中方法

      read x y <<EOF $line

      EOF

      if [ "$x" = "key3" ];then

        echo second method:$x , $y

      fi

      done < tmpfile


      7 需要注意的地方
      ════════════════

      7.1 判斷文件夾和文件
      ──────────

      if [ -f file ];then echo exists fi if [ -d folder ];then echo exists
      fi

      if [ -e path ] ;then echo exists fi


      7.2 使用參數比較的時候一定要用雙引號
      ──────────────────

      shell將參數按字符串解析,如果不用引號括起來,字符串中間有空格的時候,
      會出現兩個字符串這樣就出問題了。


      7.3 文件操作的時候需要注意路徑,絕對路徑或相對路徑,要統一
      ─────────────────────────────


      8 參考
      ══════

      <https://www.jianshu.com/p/762d4cccee7e>
      <http://blog.chinaunix.net/uid-25266990-id-3268759.html>
      <http://www.rzrgm.cn/Malphite/p/7742406.html>
      <http://www.rzrgm.cn/zwgblog/p/6031256.html>
      <http://www.rzrgm.cn/xuxm2007/p/7554543.html>
      <http://www.rzrgm.cn/wangtao1993/p/6136894.html>

      posted on 2019-07-24 17:39  空心柚子哥  閱讀(2413)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 丰满熟女人妻一区二区三| 国产精品国产三级国产专i| 亚洲熟妇自偷自拍另亚洲| 97久久久亚洲综合久久| 成人亚欧欧美激情在线观看 | 子长县| 伊人色综合九久久天天蜜桃| 色婷婷五月综合久久| 国内视频偷拍一区,二区,三区| 无码av免费毛片一区二区| 亚洲精品午夜精品| 免费观看欧美猛交视频黑人| 亚洲va中文字幕无码久久不卡| 人人人澡人人肉久久精品| 永久黄网站色视频免费直播| 久久久久人妻精品一区二区三区| 久久一级精品久熟女人妻| 国产一区二区三区自拍视频| 中文字幕av一区二区三区| 久久精品国产亚洲av天海翼| 伊人久久大香线蕉AV网禁呦| 国产免费午夜福利757| 亚洲蜜臀av乱码久久| 天堂中文8资源在线8| 国产AV福利第一精品| 国产不卡一区二区四区| 亚洲丰满老熟女激情av| 久久精品国产亚洲不AV麻豆| 少妇人妻偷人精品系列| 精品国产欧美一区二区三区在线 | 人妻少妇精品视频二区| 国产亚洲精品第一综合另类| 一区二区三区国产偷拍| av亚洲在线一区二区| 老司机性色福利精品视频| 国产在线精品欧美日韩电影| 国产精品一区二区三区激情 | 国产精品成人一区二区不卡| 精品精品久久宅男的天堂| 亚洲国产在一区二区三区| 中文字幕丰满乱子无码视频|