Linux shell4

                 第六章  shell函數
6.1
shell允許將一組命令集或語句形成一個可用塊,這些塊稱爲shell函數
定義函數的格式爲:
函數名()
{
 命令1
 ...
}
或者
function 函數名()
{
 ...
}

函數可以放在同一個文件中作爲一段代碼,也可以放在只包含函數的單獨文件中
#!/bin/bash
#hellofunction
function hello()
{
 echo "Hello,today is `date`"
 return 1
}

#!/bin/bash
#hello function test
function hello()
{
   echo "Hello,today is `date`"
   return 1
}
echo "now going to the function hello"
hello
echo "back from the function hello"

6.2 參數傳遞
向函數傳遞參數就像在腳本中使用位置變量$1,$2...$9
#!/bin/bahs
#function  參數
function hello()
{
 echo "Hello,$1 today is `date`"
}
echo "now going to the function hellp"
hello chinaUxix
echo "back from the function"

6.3函數文件
#!/bin/bash
#function
#source function
. hellofun.sh #載入函數文件 .後一定要有空格
echo "now going to the function hello"
hello  #執行hellofun.sh 中的函數hello
echo "back from the function"


hellofun.sh
#!/bin/bash
#hello function
function hello()
{
 "Hello,today is `date`"
 return 1
}

系統中函數文件的路徑
 more /etc/init.d/functions
 
6.4 檢查載入函數和刪除函數
察看載入函數  set
刪除函數  unset
#!/bin/bash
#source function
. hellofun.sh #載入函數文件
set|grep hello
#set
echo "now going to the function"
unset hello
hello
echo "back from the function"

6.5 函數返回狀態
返回的是狀態值

#!/bin/bash
#hello function ruturn
. hellofun.sh #載入函數文件
echo "now going to the function"
hello
echo $? #狀態值
echo "back from the function"

                      第7章 腳本參數傳遞
shift n 命令
getopts

7.1 shift
 每次將參數位置向左偏移n位

#!/bin/bash
#shift test totle file lines 統計文件的行數
function usage()
{
 echo "usage:`basename $0` filenemes"
}
totalline=0
if [ $# -lt 2 ];then
 usage
 exit #退出
fi
while [ $# -ne 0 ]
do
 line = `cat $1 | wc -l`
 echo "$1:${line}"
 totalline=${$totalline+$line}
 shift
done
echo "----------"
echo "total:${totalline}"

7.2  getopts
獲得多個命令行參數
#!/bin/bash
ALL=false
HELP=false
FILE=false
VERBOSE=false
while getopts ahfvc: OPTION
do
 case $OPTION in
 a)
  ALL=true
  echo "ALL is $ALL"
  ;;
 h)
  HELP=true
  echo "HELP IS $HELP"
  ;;
 f)
  FILE=true;
  echo "FILE IS $FILE"
  ;;
 v)
  VERBOSE=true
  echo "VERBOSE IS $VERBOSE"
  ;;
 c)
  c=$OPTARG
  echo "c is $c"
  ;;
 ?)
  echo "`basename $0` -[a h f v] -[c value] file"
  ;;
 esac
done

             第八章 深入討論AWK  深入討論<<
1.1 深入討論AWK
記錄和域,模式和動作,正則表達式和元字符
條件操作符
awk內置變量
NF,NR和FILENAME
awk操作符
內置的字符串函數
字符串屏蔽序列
awk輸出函數printf
awk數組

條件操作符
操作符                描述
  <                  小於
  >=                大於等於
  <=                小於等於
  ==                  等於
  !=                 不等於
   ~              匹配正則表達式
  !~             不匹配正則表達式

邏輯操作符
操作符                描述
  &&                  and
  ||                   or
  !                   not

#!/bin/bash
#awkif
echo "210-219網段的訪問量是:`awk '{if ($1~/^21[0-9]/) print $0}' www.log|wc -l`"
echo "非210-219網段的訪問量是:`awk '{if ($1!~/^21[0-9]/) print $0}' www.log|wc -l`"
echo "2004年07月07日的訪問量是:`awk '{if ($4~/^\[07\/Jul\/2004/) print $0}' www.log|wc -l`"
echo "2004年07月07日/htm/free_call.php的訪問量是:`awk '{if ($4~/^\[07\/Jul\/2004/) print $0}'
 www.log |awk '{if ($7=="/htm/free_call.php") print $0}'|wc -l`"
 
 
awk 內置變量
ARGC              命令行參數個數
ARGV              命令行參數排列
ENVIRON           支持隊列中系統環境變量的使用
FILENAME          awk瀏覽的文件名
FNR               瀏覽文件的記錄數
FS                設置輸入域分隔符,等價於命令行-F選項
NF                瀏覽記錄的域個數
NR                已讀的記錄數
OFS               輸出域分隔符
ORS               輸出記錄分隔符
RS                控制記錄分隔符

awk -F '#' '{print NF,NR,ENVIRON["USER"],$0,FILENAME,ARGC,ARGV[0],ARGV[1]}' grade.txt

字符串函數
gsub(r,s) 在整個$0中用s替代r
gsub(r,s,t) 在整個t中用s替代r
index(s,t) 返回字符串s中字符串t的第一個位置
length(s) 返回s長度
match(s,r) 測試s是否包含匹配r的字符串
split(s,a,fs) 在fs上將s分成序列a
sprint(fmt,exp) 返回經fmt格式化後的exp
sub(r,s) 用$0中最左邊最長的子串代替s
substr(s,p)  返回字符串s中從p開始的後部分
substr(s,p,n) 返回字符串s中從p開始長度爲n的後部分

awk -F '#' '{if (gsub("#","||")) print $0}' grade.txt
awk -F '#' '{if (gsub("s","S",$2)) print $2}' grade.txt
awk -F '#' '{print (index($2,"u"))}' grade.txt

轉義字符
\b          退格鍵
\t          tab鍵
\f          走紙換頁
\ddd        八進制值
\n          新行
\c          任意其他特殊字符,例如\\爲反斜線符號
\r          回車鍵

awk -F '#' '{print (index($2,"u")),"\t",$2}' grade.txt

printf修飾符
%c             ASCII字符
%d             整數
%f             浮動數,例如(123.44)
%e             浮點數,科學計數法
%f             新行
%g             awk決定使用哪種浮點數轉換e或者f
%o             八進制數
%s             字符串
%x             十六進制

awk -F '#' '{printf "%c\n",$1}' grade.txt
awk -F '#' '{printf "%c\t%d\n",$1,$1}' grade.txt
awk -F '#' '{printf "%c\t%f\t%d\n",$1,$1,$1}' grade.txt

awk數組,下標從1開始
awk 'BEGIN {print split("as#qw#1234",array2,"#")}'
awk 'BEGIN {split("as#qw#1234",array2,"#"); print array2[1]}'
awk 'BEGIN {split("as#qw#1234",array2,"#"); print array2[1],"\t",array2[2],"\t",array2[3]}'

#!/bin/awk -f
#awk array
BEGIN{
 FS="#"
 score["0-60"]=0
 score["60-70"]=0
 score["70-80"]=0
 score["80-90"]=0
 score["90-100"]=0
 student["junior"]=0
 student["senior"]=0
}
{
 {if ($1<60)
  score["0-60"]++
 }
 {if ($1<70 && $1>=60)
  score["60-70"]++
 }
 {if ($1<80 && $1>=70)
  score["70-80"]++
 }
 {if ($1<90 && $1>=80)
  score["80-90"]++
 }
 {if ($1<=100 && $1>=90)
  score["90-100"]++
 }
}
{
 for(senior_junior in student)
 {
  if($2==senior_junior)
   student[senior_junior]++
 }
}
END{
{for(number in score) print "The score ",number,"has",score[number],"student"}
{for(senior_junior in student) print "The class has ",student[senior_junior],senior_junior,"student"}
}

#!/bin/bash
loopvar=2
##############
#  main menu #
##############
main_menu()
{
echo
echo
dis_mainmenu="CREATE MINISITE IN ROOT.COM"
curdate=`date "+%Y-%m-%d %T"`
cat <<mayday
                                DATE: $curdate
                                ===============================================
                                              $dis_mainmenu
                                ===============================================
                                  **   1)ADD MINISITE ACCOUNT             **
                                  **   2)ADD DOMAIN IN ROOT.COM           **
                                  **   3)ADD DATEBASE IN MYSQL            **
                                  **   4)ADD VIRTUAL HOST IN APPCHE       **
                                  **   5)BACK MINISITE                    **
                                  **   6)DELETE MINISITE                  **
                                  **   7)EXIT                             **
                                ===============================================
mayday
}
while [ $loopvar -gt 0 ]
do
        clear
        main_menu
        echo -n "                     Please choose [1-7]:"
        read main_choice
        case $main_choice in
        7)
                exit
                ;;
        *)
                continue
                ;;
        esac
done

發佈了71 篇原創文章 · 獲贊 27 · 訪問量 100萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章