日韩欧美自拍在线观看-欧美精品在线看片一区二区-高清性视频一区二区播放-欧美日韩女优制服另类-国产精品久久久久久av蜜臀-成人在线黄色av网站-肥臀熟妇一区二区三区-亚洲视频在线播放老色-在线成人激情自拍视频

怎樣學(xué)習(xí)Linux 腳本編寫基礎(chǔ)

出處:互聯(lián)網(wǎng)    發(fā)布于:2011-09-04 13:19:32

  當(dāng)要將幾行文字傳遞給一個命令時,here documents(譯者注:目前還沒有見到過對該詞適合的翻譯)一種不錯的方法。對每個腳本寫一段幫助性的文字是很有用的,此時如果我們四有那個 here documents就不必用echo函數(shù)一行行輸出。 一個 "Here document" 以 《 開頭,后面接上一個字符串,這個字符串還必須出現(xiàn)在here document的末尾。下面是一個例子,在該例子中,我們對多個文件進(jìn)行重命名,并且使用here documents打印幫助:

  #!/bin/sh

  # we have less than 3 arguments. Print the help text:

  if [ $# -lt 3 ] ; then

  cat <

  ren -- renames a number of files using sed regular expressioNS

  USAGE: ren 'regexp' 'replacement' files…

  EXAMPLE: rename all *.HTM files in *.html:

  ren 'HTM$' 'html' *.HTM

  HELP

  exit 0

  fi

  OLD="$1"

  NEW="$2"

  # The shift command removes ONe argument from the list of

  # command line arguments.

  shift

  shift

  # $* contains now all the files:

  for file in $*; do

  if [ -f "$file" ] ; then

  newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`

  if [ -f "$newfile" ]; then

  echo "ERROR: $newfile exists already"

  else

  echo "renaming $file to $newfile …"

  mv "$file" "$newfile"

  fi

  fi

  done

  這是一個復(fù)雜一些的例子。讓我們詳細(xì)討論一下。個if表達(dá)式判斷輸入命令行參數(shù)是否小于3個 (特殊變量$# 表示包含參數(shù)的個數(shù)) 。如果輸入?yún)?shù)小于3個,則將幫助文字傳遞給cat命令,然后由cat命令將其打印在屏幕上。打印幫助文字后程序退出。如果輸入?yún)?shù)等于或大于3個,我們就將個參數(shù)賦值給變量OLD,第二個參數(shù)賦值給變量NEW。下一步,我們使用shift命令將個和第二個參數(shù)從參數(shù)列表中刪除,這樣原來的第三個參數(shù)就成為參數(shù)列表$*的個參數(shù)。然后我們開始循環(huán),命令行參數(shù)列表被一個接一個地被賦值給變量$file。接著我們判斷該文件是否存在,如果存在則通過sed命令搜索和替換來產(chǎn)生新的文件名。然后將反短斜線內(nèi)命令結(jié)果賦值給newfile。這樣我們就達(dá)到了我們的目的:得到了舊文件名和新文件名。然后使用mv命令進(jìn)行重命名。

  函數(shù)

  如果您寫了一些稍微復(fù)雜一些的程序,您就會發(fā)現(xiàn)在程序中可能在幾個地方使用了相同的代碼,并且您也會發(fā)現(xiàn),如果我們使用了函數(shù),會方便很多。一個函數(shù)是這個樣子的:

  funcTIonname()

  {

  # inside the body $1 is the first argument given to the function

  # $2 the second …

  body

  }

  您需要在每個程序的開始對函數(shù)進(jìn)行聲明。

  下面是一個叫做xtitlebar的腳本,使用這個腳本您可以改變終端窗口的名稱。

  這里使用了一個叫做help的函數(shù)。正如您可以看到的那樣,這個定義的函數(shù)被使用了兩次。

  #!/bin/sh

  # vim: set sw=4 ts=4 et:

  help()

  {

  cat <

  xtitlebar -- change the name of an xterm, gnome-terminal or kde konsole

  USAGE: xtitlebar [-h] "string_for_titelbar"

  OPTIONS: -h help text

  EXAMPLE: xtitlebar "cvs"

  HELP

  exit 0

  }

  # in case of error or if -h is given we call the function help:

  [ -z "$1" ] && help

  [ "$1" = "-h" ] && help

  # send the escape sequence to change the xterm titelbar:

  echo -e "33]0;$107"

  #

  在腳本中提供幫助是一種很好的編程習(xí)慣,這樣方便其他用戶(和您)使用和理解腳本。

  命令行參數(shù)

  我們已經(jīng)見過$* 和 $1, $2 … $9 等特殊變量,這些特殊變量包含了用戶從命令行輸入的參數(shù)。迄今為止,我們僅僅了解了一些簡單的命令行語法(比如一些強(qiáng)制性的參數(shù)和查看幫助的-h選項(xiàng))。但是在編寫更復(fù)雜的程序時,您可能會發(fā)現(xiàn)您需要更多的自定義的選項(xiàng)。通常的慣例是在所有可選的參數(shù)之前加一個減號,后面再加上參數(shù)值 (比如文件名)。

  有好多方法可以實(shí)現(xiàn)對輸入?yún)?shù)的分析,但是下面的使用case表達(dá)式的例子無遺是一個不錯的方法。

  #!/bin/sh

  help()

  {

  cat <

  This is a generic command line parser demo.

  USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2

  HELP

  exit 0

  }

  while [ -n "$1" ]; do

  case $1 in

  -h) help;shift 1;; # function help is called

  -f) opt_f=1;shift 1;; # variable opt_f is set

  -l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2

  --) shift;break;; # end of options

  -*) echo "error: no such option $1. -h for help";exit 1;;

  *) break;;

  esac

  done

  echo "opt_f is $opt_f"

  echo "opt_l is $opt_l"

  echo "first arg is $1"

  echo "2nd arg is $2"

  您可以這樣運(yùn)行該腳本:

  cmdparser -l hello -f -- -somefile1 somefile2

  返回的結(jié)果是:

  opt_f is 1

  opt_l is hello

  first arg is -somefile1

  2nd arg is somefile2

  這個腳本是如何工作的呢?腳本首先在所有輸入命令行參數(shù)中進(jìn)行循環(huán),將輸入?yún)?shù)與case表達(dá)式進(jìn)行比較,如果匹配則設(shè)置一個變量并且移除該參數(shù)。根據(jù)unix系統(tǒng)的慣例,首先輸入的應(yīng)該是包含減號的參數(shù)。

  第2部分 實(shí)例

  現(xiàn)在我們來討論編寫一個腳本的一般步驟。任何的腳本都應(yīng)該具有幫助和輸入?yún)?shù)。并且寫一個偽腳本(framework.sh),該腳本包含了大多數(shù)腳本都需要的框架結(jié)構(gòu),是一個非常不錯的主意。這時候,在寫一個新的腳本時我們只需要執(zhí)行一下copy命令:

  cp framework.sh myscript

  然后再插入自己的函數(shù)。

  讓我們再看兩個例子:

  二進(jìn)制到十進(jìn)制的轉(zhuǎn)換

  腳本 b2d 將二進(jìn)制數(shù) (比如 1101) 轉(zhuǎn)換為相應(yīng)的十進(jìn)制數(shù)。這也是一個用expr命令進(jìn)行數(shù)學(xué)運(yùn)算的例子:

  #!/bin/sh

  # vim: set sw=4 ts=4 et:

  help()

  {

  cat <

  b2h -- convert binary to decimal

  USAGE: b2h [-h] binarynum

  OPTIONS: -h help text

  EXAMPLE: b2h 111010

  will return 58

  HELP

  exit 0

  }

  error()

  {

  # print an error and exit

  echo "$1"

  exit 1

  }

  lastchar()

  {

  # return the last character of a string in $rval

  if [ -z "$1" ]; then

  # empty string

  rval=""

  return

  fi

  # wc puts some space behind the output this is why we need sed:

  numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `

  # now cut out the last char

  rval=`echo -n "$1" | cut -b $numofchar`

  }

  chop()

  {

  # remove the last character in string and return it in $rval

  if [ -z "$1" ]; then

  # empty string

  rval=""

  return

  fi

  # wc puts some space behind the output this is why we need sed:

  numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `

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

  # only one char in string

  rval=""

  return

  fi

  numofcharminus1=`expr $numofchar "-" 1`

  # now cut all but the last char:

  rval=`echo -n "$1" | cut -b 0-${numofcharminus1}`

  }

  while [ -n "$1" ]; do

  case $1 in

  -h) help;shift 1;; # function help is called

  --) shift;break;; # end of options

  -*) error "error: no such option $1. -h for help";;

  *) break;;

  esac

  done

  # The main program

  sum=0

  weight=1

  # one arg must be given:

  [ -z "$1" ] && help

  binnum="$1"

  binnumorig="$1"

  while [ -n "$binnum" ]; do

  lastchar "$binnum"

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

  sum=`expr "$weight" "+" "$sum"`

  fi

  # remove the last position in $binnum

  chop "$binnum"

  binnum="$rval"

  weight=`expr "$weight" "*" 2`

  done

  echo "binary $binnumorig is decimal $sum"

  該腳本使用的算法是利用十進(jìn)制和二進(jìn)制數(shù)權(quán)值 (1,2,4,8,16,),比如二進(jìn)制"10"可以這樣轉(zhuǎn)換成十進(jìn)制:

  0 * 1 + 1 * 2 = 2

  為了得到單個的二進(jìn)制數(shù)我們是用了lastchar 函數(shù)。該函數(shù)使用wc –c計算字符個數(shù),然后使用cut命令取出末尾一個字符。Chop函數(shù)的功能則是移除一個字符。

  文件循環(huán)程序

  或許您是想將所有發(fā)出的郵件保存到一個文件中的人們中的一員,但是在過了幾個月以后,這個文件可能會變得很大以至于使對該文件的訪問速度變慢。下面的腳本rotatefile可以解決這個問題。這個腳本可以重命名郵件保存文件(假設(shè)為outmail)為outmail.1,而對于outmail.1就變成了outmail.2 等等等等…

  #!/bin/sh

  # vim: set sw=4 ts=4 et:

  ver="0.1"

  help()

  {

  cat <

  rotatefile -- rotate the file name

  USAGE: rotatefile [-h] filename

  OPTIONS: -h help text

  EXAMPLE: rotatefile out

  This will e.g rename out.2 to out.3, out.1 to out.2, out to out.1

  and create an empty out-file

  The max number is 10

  version $ver

  HELP

  exit 0

  }

  error()

  {

  echo "$1"

  exit 1

  }

  while [ -n "$1" ]; do

  case $1 in

  -h) help;shift 1;;

  --) break;;

  -*) echo "error: no such option $1. -h for help";exit 1;;

  *) break;;

  esac

  done

  # input check:

  if [ -z "$1" ] ; then

  error "ERROR: you must specify a file, use -h for help"

  fi

  filen="$1"

  # rename any .1 , .2 etc file:

  for n in 9 8 7 6 5 4 3 2 1; do

  if [ -f "$filen.$n" ]; then

  p=`expr $n + 1`

  echo "mv $filen.$n $filen.$p"

  mv $filen.$n $filen.$p

  fi

  done

  # rename the original file:

  if [ -f "$filen" ]; then

  echo "mv $filen $filen.1"

  mv $filen $filen.1

  fi

  echo touch $filen

  touch $filen

  這個腳本是如何工作的呢?在檢測用戶提供了一個文件名以后,我們進(jìn)行一個9到1的循環(huán)。文件9被命名為10,文件8重命名為9等等。循環(huán)完成之后,我們將原始文件命名為文件1同時建立一個與原始文件同名的空文件。

  調(diào)試

  簡單的調(diào)試命令當(dāng)然是使用echo命令。您可以使用echo在任何懷疑出錯的地方打印任何變量值。這也是絕大多數(shù)的shell程序員要花費(fèi)80%的時間來調(diào)試程序的原因。Shell程序的好處在于不需要重新編譯,插入一個echo命令也不需要多少時間。

  shell也有一個真實(shí)的調(diào)試模式。如果在腳本"strangescript" 中有錯誤,您可以這樣來進(jìn)行調(diào)試:

  sh -x strangescript

  這將執(zhí)行該腳本并顯示所有變量的值。

  shell還有一個不需要執(zhí)行腳本只是檢查語法的模式??梢赃@樣使用:

  sh -n your_script

    這將返回所有語法錯誤

 



  

版權(quán)與免責(zé)聲明

凡本網(wǎng)注明“出處:維庫電子市場網(wǎng)”的所有作品,版權(quán)均屬于維庫電子市場網(wǎng),轉(zhuǎn)載請必須注明維庫電子市場網(wǎng),http://www.hbjingang.com,違反者本網(wǎng)將追究相關(guān)法律責(zé)任。

本網(wǎng)轉(zhuǎn)載并注明自其它出處的作品,目的在于傳遞更多信息,并不代表本網(wǎng)贊同其觀點(diǎn)或證實(shí)其內(nèi)容的真實(shí)性,不承擔(dān)此類作品侵權(quán)行為的直接責(zé)任及連帶責(zé)任。其他媒體、網(wǎng)站或個人從本網(wǎng)轉(zhuǎn)載時,必須保留本網(wǎng)注明的作品出處,并自負(fù)版權(quán)等法律責(zé)任。

如涉及作品內(nèi)容、版權(quán)等問題,請?jiān)谧髌钒l(fā)表之日起一周內(nèi)與本網(wǎng)聯(lián)系,否則視為放棄相關(guān)權(quán)利。

廣告
OEM清單文件: OEM清單文件
*公司名:
*聯(lián)系人:
*手機(jī)號碼:
QQ:
有效期:

掃碼下載APP,
一鍵連接廣大的電子世界。

在線人工客服

買家服務(wù):
賣家服務(wù):
技術(shù)客服:

0571-85317607

網(wǎng)站技術(shù)支持

13606545031

客服在線時間周一至周五
9:00-17:30

關(guān)注官方微信號,
第一時間獲取資訊。

建議反饋

聯(lián)系人:

聯(lián)系方式:

按住滑塊,拖拽到最右邊
>>
感謝您向阿庫提出的寶貴意見,您的參與是維庫提升服務(wù)的動力!意見一經(jīng)采納,將有感恩紅包奉上哦!