兩個很詳細的shell 執行個體代碼

來源:互聯網
上載者:User

兩個很詳細的shell 執行個體
一般編程步驟
  現在我們來討論編寫一個指令碼的一般步驟。任何優秀的指令碼都應該具有協助和輸入參數。並且寫一個偽指令碼(framework.sh),該指令碼包含了大多數指令碼都需要的架構結構,是一個非常不錯的主意。這時候,在寫一個新的指令碼時我們只需要執行一下copy命令:
cp framework.sh myscript
  然後再插入自己的函數。
  讓我們再看兩個例子:
  二進位到十進位的轉換
  指令碼 b2d 將位元 (比如 1101) 轉換為相應的十進位數。這也是一個用expr命令進行數學運算的例子: 複製代碼 代碼如下:#!/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"
#

  該指令碼使用的演算法是利用十進位和位元權值 (1,2,4,8,16,..),比如二進位"10"可以這樣轉換成十進位:
0 * 1 + 1 * 2 = 2
  為了得到單個的位元我們是用了lastchar 函數。該函數使用wc ?c計算字元個數,然後使用cut命令取出末尾一個字元。Chop函數的功能則是移除最後一個字元。
  檔案迴圈程式
  或許您是想將所有發出的郵件儲存到一個檔案中的人們中的一員,但是在過了幾個月以後,這個檔案可能會變得很大以至於使對該檔案的訪問速度變慢。下面的指令碼rotatefile 可以解決這個問題。這個指令碼可以重新命名郵件儲存檔案(假設為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 Array 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

  這個指令碼是如何工作的呢?在檢測使用者提供了一個檔案名稱以後,我們進行一個Array到1的迴圈。檔案Array被命名為10,檔案8重新命名為Array等等。迴圈完成之後,我們將原始檔案命名為檔案1同時建立一個與原始檔案同名的空檔案。
調試
  最簡單的調試命令當然是使用echo命令。您可以使用echo在任何懷疑出錯的地方列印任何變數值。這也是絕大多數的shell程式員要花費80%的時間來偵錯工具的原因。Shell程式的好處在於不需要重新編譯,插入一個echo命令也不需要多少時間。
  shell也有一個真實的偵錯模式。如果在指令碼"strangescript" 中有錯誤,您可以這樣來進行調試:
sh -x strangescript
  這將執行該指令碼並顯示所有變數的值。
  shell還有一個不需要執行指令碼只是檢查文法的模式。可以這樣使用:
sh -n your_script
  這將返回所有語法錯誤。
  我們希望您現在可以開始寫您自己的shell指令碼,希望您玩得開心。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.