歡迎您光臨本站 註冊首頁

shell編程基礎···初學者必用

←手機掃碼閱讀     火星人 @ 2014-03-29 , reply:0

1.shell基礎知識
作者:Stephen Bourne 在Bell實驗室開發
建議:man sh 查看相關UNIX上的改進或特性

(1)shell提示符及其環境
/etc/passwd文件
提示符:$
/etc/profile $HOME/.profile
(2)shell執行選項
-n 測試shell script語法結構,只讀取shell script但不執行
-x 進入跟蹤方式,顯示所執行的每一條命令,用於調度
-a Tag all variables for export
-c "string" 從strings中讀取命令
-e 非交互方式
-f 關閉shell文件名產生功能
-h locate and remember functions as defind
-i 交互方式
-k 從環境變數中讀取命令的參數
-r 限制方式
-s 從標準輸入讀取命令
-t 執行命令後退出(shell exits)
-u 在替換中如使用未定義變數為錯誤
-v verbose,顯示shell輸入行

這些選項可以聯合使用,但有些顯然相互衝突,如-e和-i.

(3)受限制shell(Restircted Shell)
sh -r 或 /bin/rsh

不能執行如下操作:cd, 更改PATH,指定全路徑名,輸出重定向,因此可以提供一個較
好的控制和安全機制。通常rsh用於應用型用戶及撥號用戶,這些用戶通常是看不到提
示符的。通常受限制用戶的主目錄是不可寫的。

不足:如果用戶可以調用sh,則rsh的限制將不在起作用,事實上如果用戶在vi及more
程序中調用shell,而這時rsh的限制將不再起作用。

(4)用set改變 shell選項
用戶可以在$提示符下用set命令來設置或取消shell的選項。使用-設置選項,+取消相應
選項,大多數UNIX系統允許a,e,f,h,k,n,u,v和x的開關設置/取消。

set -xv
啟動跟蹤方式;顯示所有的命令及替換,同樣顯示輸入。
set -tu
關閉在替換時對未定義變數的檢查。

使用echo $-顯示所有已設置的shell選項。


(5)用戶啟動文件 .profile
PATH=$PATH:/usr/loacl/bin; export PATH

(6)shell環境變數
CDPATH 用於cd命令的查找路徑
HOME /etc/passwd文件中列出的用戶主目錄
IFS Internal Field Separator,默認為空格,tab及換行符
MAIL /var/mail/$USERNAME mail等程序使用
PATH
PS1,PS2 默認提示符($)及換行提示符(>)
TERM 終端類型,常用的有vt100,ansi,vt200,xterm等

示例:$PS1="test:";export PS1
test: PS1="$";export PS1
$echo $MAIL
/var/mail/username
(7)保留字元及其含義
$ shell變數名的開始,如$var
| 管道,將標準輸出轉到下一個命令的標準輸入
# 註釋開始
& 在後台執行一個進程
? 匹配一個字元
* 匹配0到多個字元(與DOS不同,可在文件名中間使用,並且含.)
$- 使用set及執行時傳遞給shell的標誌位
$! 最後一個子進程的進程號
$# 傳遞給shell script的參數個數
$* 傳遞給shell script的參數
$@ 所有參數,個別的用雙引號括起來
$? 上一個命令的返回代碼
$0 當前shell的名字
$n (n:1-) 位置參數
$$ 進程標識號(Process Identifier Number, PID)
>file 輸出重定向
>fiile 輸出重定向,append

轉義符及單引號:
$echo "$HOME $PATH"
/home/hbwork /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:
$echo '$HOME $PATH'
$HOME $PATH
$echo $HOME $PATH
$HOME /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/hbw
ork/bin

其他:
$dir=ls
$$dir
$alias dir ls
$dir

ls > filelist
ls >> filelist
wc -l /dev/rmt/0h

書寫程序的目的是一次編程,多次使用(執行)!

$ cat > backup.sh
cd /home/hbwork
ls * | cpio -o > /dev/rmt/0h
^D

執行:
$ sh backup.sh

或:
$ chmod +x backup.sh
$ ./backup.sh

技巧:在shell script中加入必要的註釋,以便以後閱讀及維護。

(2)shell是一個(編程)語言
同傳統的編程語言一樣,shell提供了很多特性,這些特性可以使你的shell script
編程更為有用,如:數據變數、參數傳遞、判斷、流程式控制制、數據輸入和輸出,子
程序及以中斷處理等。

. 在shell編程中使用數據變數可以將程序變數更為通用,如在上面backup.sh中:
cd $WORKDIR
ls * | cpio -o > /dev/rmt/0h

. Shell編程中的註釋以#開頭
. 對shell變數進行數字運算,使用expr命令
expr integer operator integer
其中operator為+ - * / %, 但對*的使用要用轉義符,如:
$expr 4 * 5
20
$int=`expr 5 + 7`
$echo $int
12

(3)Shell編程的參數傳遞, 可通過命令行參數以及互動式輸入變數(read)

restoreall.sh 對backup.sh程序的備份磁帶進行恢復
$cat > restoreall.sh
cd $WORKDIR
cpio -i /dev/rmt/0h
rm -rf *

改進如下:

#當使用了管道命令時,管理命令的返回代碼為最後一個命令的返回代碼
if ls -a | cpio -o > /dev/rmt/0h
then
rm -rf *
fi

. if-then-else語句
if command_1
then
command_2
else
command_3
fi

技巧: 由於shell對命令中的多餘的空格不作任何處理,一個好的程序員會用這一特

對自己的程序採用統一的縮進格式,以增強自己程序的可讀性.

. 使用test命令進行進行條件測試
格式: test conditions

test在以下四種情況下使用: a. 字元比較 b.兩個整數值的比較
c. 文件操作,如文件是否存在及文件的狀態等
d. 邏輯操作,可以進行and/or,與其他條件聯合使用

a. 測試字元數據: shell變數通常民政部下均作為字元變數
str1 = str2 二者相長,相同
str1 != str2 不同
-n string string不為空(長度不為零)
-z string string為空
string string不為空

例:
$ str1=abcd #在含有空格時必須用引號括起來
$ test $str1=abcd
$ echo $?
0
$ str1="abcd "
$ test $str1=abcd
$ echo $?
1
Note: 在test處理含有空格的變數時最好用引號將變數括起來,否則會出現錯誤的
結果,
因為shell在處理命令行時將會去掉多餘的空格,而用引號括起來則可以防止
shell去掉這些空格.
例:
$ str1=" "
$ test $str1
$ echo $?
1
$ test "$str1"
$ echo $?
0
$ test -n $str1
test: argument expected
$ test -n "$str1"
$ echo $?
0
$

b. 整數測試: test與expr相同,可以將字元型變數轉換為整數進行操作,expr進行
整數的算術運算,而test則進行邏輯運算.

表達式 說明
---------------------------------------
int1 -eq int2 相等?
int1 -ne int2 不等?
int1 -gt int2 int1 > int2 ?
int1 -ge int2 int1 >= int2 ?
int1 -lt int2 int1 empty
$ test -r empty
$ echo $?
0
$ test -s empty
1
$ test ! -s empty
$ echo $?
0
e. 測試條件之邏輯運算
-a And
-o Or

例: $ test -r empty -a -s empty
$ echo $?
1
f. 進行test測試的標準方法
因為test命令在 shell編程中佔有很重要的地位,為了使shell能同其他編程語言
一樣
便於閱讀和組織, Bourne Shell在使用test測試時使用了另一種方法:用方括弧將
整個
test測試括起來:

$ int1=4
$ [ $int1 -gt 2 ]
$ echo $?
0

例: 重寫unload程序,使用test測試
#!/bin/sh
#unload - program to backup and remove files
#syntax: unload directory

#check arguments
if [ $# -ne 1 ]
then
echo "usage: $0 directory"
exit 1
fi

#check for valid directory name
if [ ! -d "$1" ]
then
echo "$1 is not a directory"
exit 2
fi

cd $1

ls -a | cpio -o > /dev/rmt/0h

if [ $? -eq 0 ]
then
rm -rf *
else
echo "A problem has occured in creating backup"
echo "The directory will not be ereased"
echo "Please check the backup device"
exit 3
fi
# end of unload

在如上示例中出現了exit, exit有兩個作用:一是停止程序中其他命令的執行,二

設置程序的退出狀態

g. if嵌套及elif結構
if command
then
command
else
if command
then
command
else
if command
then
command
fi
fi
fi

改進:使用elif結構
if command
then
command
elif command
then
command
elif command
then
command
fi

elif結構同if結構類似,但結構更清淅,其執行結果完全相同.
--
Bourne Shell及Shell編程(2)
h.互動式從鍵盤讀入數據
使用read語句,其格式如下:

read var1 var2 ... varn

read將不作變數替換,但會刪除多餘的空格,直到遇到第一個換行符(回車),
並將輸入值依次賦值給相應的變數。

例:
$ read var1 var2 var3
Hello my friends
$ echo $var1 $var2 $var3
Hello my friends
$ echo $var1
Hello
$ read var1 var2 var3
Hello my dear friends
$ echo $var3
dear friends while循環:
格式:
while command
do
command
command
command
...
done

例: 計算1到5的平方
#!/bin/sh
#
#Filename: square.sh
int=1

while [ $int -le 5 ]
do
sq=`expr $int * $int`
echo $sq
int=`expr $int + 1`
done
echo "Job completed"

$ sh square.sh
1
4
9
16
25
Job completed

until循環結構:
格式:
until command
do
command
command
....
command
done

示例:使用until結構計算1-5的平方
#!/bin/sh

int=1

until [ $int -gt 5 ]
do
sq=`expr $int * $int`
echo $sq
int=`expr $int + 1`
done
echo "Job completed"

使用shift對不定長的參數進行處理
在以上的示例中我們總是假設命令行參數唯一或其個數固定,或者使用$*將整個命

行參數傳遞給shell script進行處理。對於參數個數不固定並且希望對每個命令參

進行單獨處理時則需要shift命令。使用shift可以將命令行位置參數依次移動位置

即$2->$1, $3->$2. 在移位之前的第一個位置參數$1在移位后將不在存在。

示例如下:

#!/bin/sh
#
#Filename: shifter

until [ $# -eq 0 ]
do
echo "Argument is $1 and `expr $# - 1` argument(s) remain"
shift
done


$ shifter 1 2 3 4
Argument is 1 and 3 argument(s) remain
Argument is 2 and 2 argument(s) remain
Argument is 3 and 1 argument(s) remain
Argument is 4 and 0 argument(s) remain
$

使用shift時,每進行一次移位,$#減1,使用這一特性可以用until循環對每個參
數進行處理,如下示例中是一個求整數和的shell script:

#!/bin/sh
# sumints - a program to sum a series of integers
#

if [ $# -eq 0 ]
then
echo "Usage: sumints integer list"
exit 1
fi

sum=0

until [ $# -eq 0 ]
do
sum=`expr $sum + $1`
shift
done
echo $sum


$ sh sumints 324 34 34 12 34
438
$

使用shift的另一個原因是Bourne Shell的位置參數變數為$1~$9, 因此通過位置變

只能訪問前9個參數。但這並不等於在命令行上最多只能輸入9個參數。此時如果想
訪問
前9個參數之後的參數,就必須使用shift.

另外shift后可加整數進行一次多個移位,如:

shift 3


. for循環
格式:
for var in arg1 arg2 ... argn
do
command
....
command
done

示例:
$ for letter in a b c d e; do echo $letter;done
a
b
c
d
e

對當前目錄下的所有文件操作:
$ for i in *
do
if [ -f $i ]
then
echo "$i is a file"
elif [ -d $i ]
echo "$i is a directory"
fi
done

求命令行上所有整數之和:
#!/bin/sh

sum=0

for INT in $*
do
sum=`expr $sum + $INT`
done

echo $sum


從循環中退出: break和continue命令
break 立即退出循環
continue 忽略本循環中的其他命令,繼續下一下循環

在shell編程中有時我們要用到進行無限循環的技巧,也就是說這種循環一直執行

到break或continue命令。這種無限循環通常是使用true或false命令開始的。UNIX
系統中的true總是返加0值,而false則返回非零值。如下所示:

#一直執行到程序執行了break或用戶強行中斷時才結束循環
while true
do
command
....
command
done

上面所示的循環也可以使用until false, 如下:

until false
do
command
....
command
done

在如下shell script中同時使用了continue,break以及case語句中的正規表達式用
法:

#!/bin/sh
# Interactive program to restore, backup, or unload
# a directory

echo "Welcome to the menu driven Archive program"

while true
do
# Display a Menu
echo
echo "Make a Choice from the Menu below"
echo _
echo "1 Restore Archive"
echo "2 Backup directory"
echo "3 Unload directory"
echo "4 Quit"
echo

# Read the user's selection
echo -n "Enter Choice: "

read CHOICE

case $CHOICE in
[1-3] ) echo
# Read and validate the name of the directory

echo -n "What directory do you want? "
read WORKDIR

if [ ! -d "$WORKDIR" ]
then
echo "Sorry, $WORKDIR is not a directory"
continue
fi

# Make the directory the current working directory
cd $WORKDIR;;

4) :;; # :為空語句,不執行任何動作
*) echo "Sorry, $CHOICE is not a valid choice"
continue
esac

case "$CHOICE" in
1) echo "Restoring..."
cpio -i /dev/rmt/0h;;

3) echo "Unloading..."
ls | cpio -o >/dev/rmt/0h;;

4) echo "Quitting"
break;;
esac

#Check for cpio errors

if [ $? -ne 0 ]
then
echo "A problem has occurred during the process"
if [ $CHOICE = 3 ]
then
echo "The directory will not be erased"
fi

echo "Please check the device and try again"
continue
else
if [ $CHOICE = 3 ]
then
rm *
fi
fi
done


(6)結構化編程:定義函數
同其他高級語言一樣,shell也提供了函數功能。函數通常也稱之為子過程(subroutine)
,
其定義格式如下:

funcname()
{
command
...
command; #分號
}

定義函數之後,可以在shell中對此函數進行調用,使用函數定義可以將一個複雜的程序

為多個可管理的程序段,如下所示:

# start program

setup ()
{ command list ; }

do_data ()
{ command list ; }

cleanup ()
{ command list ; }

errors ()
{ command list ; }

setup
do_data
cleanup
# end program

技巧:
. 在對函數命名時最好能使用有含義的名字,即函數名能夠比較準確的描述函數所
完成
的任務。
. 為了程序的維護方便,請儘可能使用註釋


使用函數的另一個好處就是可以在一個程序中的不同地方執行相同的命令序列(函數),
如下所示:

iscontinue()
{
while true
do
echo -n "Continue?(Y/N)"
read ANSWER

case $ANSWER in
[Yy]) return 0;; <

[火星人 ] shell編程基礎···初學者必用已經有538次圍觀

http://coctec.com/docs/linux/show-post-198431.html