歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Linux下的shell與make

Linux下的shell與make

日期:2017/3/1 10:17:37   编辑:SHELL編程

一、shell

1.1 什麼是shell

● 用戶與Linux的接口

● 命令解釋器

● 支持多用戶

● 支持復雜的編程語言

● Shell有很多種,如:csh,tcsh,pdksh,ash,sash,zsh,bash等。Linux的缺省Shell為bash(Bourne Again Shell)。

Shell是用戶和操作系統之間最主要接口。通過Shell,可以同時在後台運行多個應用程序,並且在把需要與用戶交互的程序放在前台運行。

通過在多條命令的序列中使用變量和流程控制,Shell可以作為一名復雜的編程語言。

1.2 轉義字符和通配字符

● 轉義字符(metacharacters)指的是在Shell中有特殊含義的字符。

  例如:< > | ; ! ? * [ ] $ \ " ' ` ~ ( ) { } ^

#ls file[1-10].c

#count=`ls –l | grep ‘^d’| wc –l`

● 通配字符(wildcards)是轉義字符的子集,用來查找匹配文件名

  例如:? * [] [-] [!]

● 使用<Tab>鍵,可以自動補齊。

通配一個字符:"?"

通配多個字符:"*"

1.3 輸入輸出重定向

● 系統定義三個標准文件:標准輸入(stdin)、標准輸出(stdout)、標准錯誤輸入(stderr)

● 管道符:"|",將前一個命令的輸出轉成下一個命令的輸入。

● 過濾器(filters):用來接收標准輸入,經過一定的轉化,再寫到標准輸出。所以,過濾器一般放在管道符中間。

每個程序起動時,就有三個文件描述:STDIN (0),STDOUT(1),STDERR(2)。用"<"改變輸入,用">"改變輸出,用"2>"改變錯誤輸出,用">>"來追加到已有的文件中。

常用的過濾器如下:expand,sed,awk,fmt,tac,tr,grep,nl,pr。

多條命令可以輸入到一行中,並用";"字符分隔。在一行命令後加"\"表示另起一行繼續輸入,下一行的提示符變為">"

printf(“\t”);

1.4 Shell變量

● Shell變量(Variables)是程序用來保存數據用的。

● Shell特殊變量:系統定義了一些特殊的變量。如$HOME等。使用set命令查看當時系統中定義的變量。

系統定義的特殊變量有:$TERM(當前終端類型),$PATH(命令的搜索路徑),$MANPATH(手冊頁存放的目錄),$HOME(用戶主目錄),$LANG(當前使用語言),$PS1,$PS2(命令提示符),$SHELL(當前Shell名)。

在命令行上打入:變量名=變量值,可以定義變量。使用 echo $變量名查看變量值。使用 unset 取消對變量的定義。

#hello=”123”

單引號表示忽略其中所有的轉義字符,雙引號表示忽略其中除了 $ ` \ 以外的其他的轉義字符。反斜線(\)表示忽略下一個轉義字符。

多條命令可以輸入到一行中,並用";"字符分隔。在一行命令後加"\"表示另起一行繼續輸入,下一行的提示符變為">"

● Shell系統變量

$?:最近執行的命令返回的值;

$$:本進程的進程號

$!:最近後台進程號

$#:shell自變量個數,$1表示第一個自變量

● Shell用戶變量

#varname=value :賦值

#readonly varname :標記只讀

#export varname :標記移出:變量可以被子進程繼承

#setenv PATH=/home:/usr:/etc:/bin:/usr/bin: (csh中)

#varname=` expr $varname + 1 ` :變量值增1 #x=$[ $x + 1 ]

#echo $PATH

1.5 定制用戶環境

● 用戶在登錄Shell時,會依次執行一系列的腳本。

● 使用alias命令,可以定義一些別名。

Alias ‘rm –f’ rm

登錄BASH時,用戶將依次執行一系列的腳本:/etc/profile , $HOME/.bash_profile ( 如果沒有,執行 $HOME/.bash_login ,還沒有,執行 $HOME/.profile)。注銷時,會自動執行 $HOME/.bash_logout 。

使用 RedHat 的用戶,登錄時除了這兩個文件以外,還會自動執行 $HOME/.bashrc ,這個文件又會再執行 /etc/bashrc

1.6 條件控制

1)test命令

測試文件、變量的屬性,表達式的值,或命令執行返回值。

test –d /usr à [ -d /usr ]

test –f .bashrc à [ -f .bashrc ]

test $count –gt 0 à [ $count –gt 0 ]

2)if語句

if (expression) then

command-list

else

command-list

fi

3)case語句

case $var in

pattern1) command-list ;;

pattern2) command-list ;;

esac

4)邏輯運算符 && 和 ||

# test –f myfile.c && echo “file found”

if test –f myfile.c then

echo “file found”

fi

# test –f myfile.c | | echo “file not found”

if test ! –f myfile.c then

echo “file not found”

fi

1.7 循環控制

1)for語句

for var in word-list

do

command-list

done

for count in 1 2 3

do

echo $I

done

for var

do

2)while語句

while (expression)

do

command-list

done

#greeting=’hello world’

i = 1

while test $i –le 100 ; do

case $i in

*0) echo “**********”>file$i ;;

*) echo $i > file$i ;;

esac

i = ` expr $i + 1 `

done

例:append命令的實現:

case $# in

1) cat >> $1 ;;

2) cat < $1>> $2 ;;

*) echo ‘usage: append [from] to ’;;

esac

#chmod +x myappend

#myappend file1 file2

1.8函數

functionname( )

{

command-list

}

usage()

{

echo “usage:…..$1”

}

usage “from … to “

注意:函數的使用就象可執行程序一樣,但必須先定義,後使用。

1.9 here文檔

here文檔指在shell腳本中指定輸入源,而不是來自文件或標准輸入,其中的“<<”是here文檔保留字。

# mail cindy << !@$

happy birthday

I love you

!@$

1.10 shell內部命令:不產生子進程

1) eval:在shell程序中,利用變量的值來構建命令

A=ls

B= ‘ | wc -w’

eval $A$B

2) exec:轉去執行exec後命令,不建立新進程,也不返回到當前的執行過程,相當於go to 語句。

#cat execdemo

exec date

echo hello

3) read:從標准輸入設備(鍵盤)讀入一行,並把讀入的字依次賦給各變量,所有剩余的字賦給最後一個變量。

#cat parrot

echo “you say:\c”

read what

echo “I repeat:$what”

4) shift:使命令行參數向左移動一位,並使記錄參數總數的變量$#減1

#cat shiftdemo

while test $# != 0

do

echo $1 $2 $3

shift

done

#shiftdemo a b c

a b c

b c

c

5)wait:等待當前進程所有子進程結束,若wait後跟參數n,則等待進程n結束。

#cat waitdemo

echo “This is a new file”

(sleep 5; date)&

wait

echo “the file terminate”

執行結果:

This is a new file

April 20 10:08:26 BJT 2002-04-20

The file terminate

6) trap:中斷處理命令

trap 命令表 中斷信號表

#cat trapfile

trap echo ‘ This is INT 2’ 2

trap echo ‘ This is INT 3’ 3

for I in /bin /bin/usr

do

echo $I

done

下面程序實現scan:掃描當前目錄下的每一個子目錄,並執行用戶提交的命令:

d=`pwd`

for i in *

do

if test –d $d/$i

then

cd $d/$i

while echo “$i:”

trap exit 2

read x

do trap : 2 ; eval $x; done

fi

done

7)點命令 .

在bsh利用 . 命令執行一個命令時,不創建子進程。(csh中用source)

8)空命令:不做任何事情

1.11 shell程序實例

下面程序dircmp測試當前目錄與指定目錄是否含有相同文件數

if test $# -ne 1 then

echo “Usage:dircmp dirname”

exit 1

else if test !-d $1 then

echo “\”$1\” is not a directory”

exit 1

else

this = `ls –l |grep ‘^-’ | wc –l `

that = `ls –l $1 |grep ‘^-’ | wc –l `

if test $this –ne $that then

echo “Current directory and \”$1\” do not match”

else

echo “Current directory and \”$1\” have same number of files”

fi

fi

fi

#dircmp abc

“abc” is not a directory

1.12 shell程序的執行方法:

1) $chmod u+x dircmp

$./dircmp /usr/bin

2) $sh dircmp

3) $sh < dircmp

4) $ . dircmp(用點命令執行程序,不創建子進程)

%source dircmp(csh中)

Copyright © Linux教程網 All Rights Reserved