程序的目的是在redhat和AIX上同時運行,對于很多指令,使用Redhat完成了,但是AIX上沒有,這時,我們選擇Alias的方式,在AIX的Env中,提供具備這樣功能的指令模塊。
下面是研究怎么實現(xiàn)redhat iptables的stub.
Step (1)about port <=> [inbound ,outbound]
查看端口占用情況:
Issue the command:(as 389 it the port you want to seek )
netstat -Aan | grep 389
this will return:
f1000089c27a2358 tcp4 0 0 *.389 *.* LIST EN
The next step is to take this value that was generated, f1000089c27a2358 and run it against the rmsock command:
rmsock f100089c27a2358 tcpcb
this command will return the process that is holding the socket.
The socket 0xc27a2000 is being held by process 204914 (ndsd).
Step (2)ipsec
什么是ipsec?
ipsec是IEFT提出的network管理標準工具。
ipsec的指令集:
Filters can be defined via the SMIT panel at the fastpath ips4_add_filter
or via the command line, using the genfilt
command. The SMIT method presents a screen similar to the table in the previous section. To create the filter "by hand", the following flags to the genfilt command are used to specify the attributes of the filter rule:
-v
-n
-a
-s
-m
-d
-M
-g
-c
-o
-p
-O
-P
-r
-w
-l
-f
-i
ipsec四個命令:
To work with TCP/IP filters you only need a few commands, which is explained here and then used in the next section. If you're familiar with AIX commands you see that these follow the same logic of having descriptive prefixes in their names, like mk
, ls
, and rm
, followed by the filt
suffix.
–n
parameter, the new rule is added at the end of the table.
rm
suffix should sound familiar with any UNIX administrator. You use this command whenever you have to remove a filter rule providing its rule ID.
Refer:http://www.darklab.net/resources/aix-ipsec-filtering.html
http://www.ibm.com/developerworks/aix/library/au-aixfiltering/index.html
使用過程:
(1)啟動/關(guān)閉
# smitty ipsec4
(2)是否啟動
# lsdev -l ipsec_v4
(3)做個改變
# chfilt -v 4 -n 3 -i en1 Filter rule 3 for IPv4 has been changed successfully. #
(4)添加新的Rule
## Rules to reject traffic to the Web Application not coming from the Proxy # genfilt -v 4 -a D -s 0 -m 0 -d 172.16.10.45 -M 255.255.255.255 -g N -c tcp -O eq -P 80 -r L -w I -l Y -f Y -i all
(5)設(shè)置log
Now, you are going to configure the syslog daemon to log entries coming from the IP filters in a file that you specify.
## Backup syslog.conf file before modifying it. # cp /etc/syslog.conf /etc/syslog.conf.bak ## Append entry for IP filters logs. # echo "local4.debug /var/adm/ipsec.log" >> /etc/syslog.conf ## Create log file and set permissions (permissions may depend on ## company policies) # touch /var/adm/ipsec.log # chmod 644 /var/adm/ipsec.log ## Refresh the syslog subsystem to activate the new configuration. # refresh -s syslogd 0513-095 The request for subsystem refresh was completed successfully.
(6)配置更新,上面的更新不會立即生效,除非運行下面的命令
# Start the log functionality of the filter rule module # mkfilt -g start # # Activates the filter rules # mkfilt –u
Step (3) iptables
iptables 是linux中防火墻流行的管理工具
針對本文的作用,僅僅說明一種使用,若要詳盡的了解,需耐心的閱讀一個非常優(yōu)秀的文章,鏈接如下:
Refer:http://man.chinaunix.net/network/iptables-tutorial-cn-1.1.19.html
or http://linux.ccidnet.com/pub/html/tech/iptables/index.htm
我們只是需要使用iptables像外界提供訪問本機資源的端口:
httpPort=80
httpsPort=443
adminPort=8008
那么就使用下面的命令:
cmd 1 :iptables -A INPUT -p tcp --dport ${port} -j ACCEPT
cmd 2 :iptables -A INPUT -p tcp -s localhost -j ACCEPT
cmd 1 .在filter表上添加一條規(guī)則,如果socket pack來自TCP且push到${port}端口,就接收。
cmd 2 .在filter表上添加一條規(guī)則,如果源IP地址是本機,就接收。
Step (4)mock iptables
如果是用IPsec來實現(xiàn)打開端口,允許訪問的話,是這樣:
genfilt -v 4 -a P -s 0.0.0.0 -m 0.0.0.0 -d 127.0.0.1 -M 0.0.0.0 -g Y -c tcp –o any –p 0 -O eq -P 80 -r B -w B -l N -f Y -i all
mkfilt -u
所以,就是這樣做一個mock
#
# cloud_iptables - simulate iptables
#
function aix_iptables
{
echo "aix_iptables:[$@]"
port=""
while [ $# -ne 0 ]; do
case $1 in
--dport)
port=$2
genfilt -v 4 -a P -s 0.0.0.0 -m 0.0.0.0 -d 127.0.0.1 -M 0.0.0.0 -g Y -c tcp –o any –p 0 -O eq -P $port -r B -w B -l N -f Y -i all
shift 1
;;
-s)
sourceip=$2
genfilt -v 4 -a P -s $sourceip -m 0.0.0.0 -d 127.0.0.1 -M 0.0.0.0 -g Y -c tcp –o any –p 0 -O any -P 0 -r B -w B -l N -f Y -i all
shift 1
;;
*)
shift 1
;;
esac
done
}
但是,這還沒有結(jié)束
使用iptables時,我們是這樣:
iptables -A INPUT -p tcp -s localhost -j ACCEPT
service iptables save
語句1 用來實現(xiàn)添加一條 rules
語句2 用來使變更生效
所以,還需要mock service 方法,這樣,在兩個平臺中,語句1和語句2就都完成了打開一些防火墻端口的功能。
function aix_service
{
if [ "$1" = "iptables" ]; then
mkfilt -u
else
echo "aix_service $*"
fi
}
對于系統(tǒng)的判斷:
function on_AIX
{
test "`uname`" = "AIX"
}
如果是AIX系統(tǒng)的話,就要在AIX上執(zhí)行aliases了。
if aliases_on_aix ; then
shopt -s expand_aliases # enable expand aliases,keep it on
alias sudo='aix_sudo'
alias hostname='aix_hostname'
alias iptables='aix_iptables'
alias chkconfig='aix_chkconfig'
alias service='aix_service'
alias
fi
上面是在AIX的bash環(huán)境下進行的。
http://qlj.sh.cn/python/20100402/python-time/
python 的內(nèi)嵌time模板翻譯及說明
一、簡介
time模塊提供各種操作時間的函數(shù)
說明:一般有兩種表示時間的方式:
第一種是時間戳的方式(相對于1970.1.1 00:00:00以秒計算的偏移量),時間戳是惟一的
第二種以數(shù)組的形式表示即(struct_time),共有九個元素,分別表示,同一個時間戳的struct_time會因為時區(qū)不同而不同
year (four digits, e.g. 1998)
month (1-12)
day (1-31)
hours (0-23)
minutes (0-59)
seconds (0-59)
weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1) 是否是夏令時
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is -1, mktime() should guess based on the date and time.
二、函數(shù)介紹
1.asctime()
asctime([tuple]) -> string
將一個struct_time(默認為當(dāng)時時間),轉(zhuǎn)換成字符串
Convert a time tuple to a string, e.g. ‘Sat Jun 06 16:26:11 1998′.
When the time tuple is not present, current time as returned by localtime()
is used.
2.clock()
clock() -> floating point number
該函數(shù)有兩個功能,
在第一次調(diào)用的時候,返回的是程序運行的實際時間;
以第二次之后的調(diào)用,返回的是自第一次調(diào)用后,到這次調(diào)用的時間間隔
示例:
view plaincopy to clipboardprint? import time if __name__ == '__main__': time.sleep(1) print "clock1:%s" % time.clock() time.sleep(1) print "clock2:%s" % time.clock() time.sleep(1) print "clock3:%s" % time.clock()
輸出:
clock1:3.35238137808e-006
clock2:1.00004944763
clock3:2.00012040636
其中第一個clock輸出的是程序運行時間
第二、三個clock輸出的都是與第一個clock的時間間隔
3.sleep(…)
sleep(seconds)
線程推遲指定的時間運行,經(jīng)過測試,單位為秒,但是在幫助文檔中有以下這樣一句話,這關(guān)是看不懂
“The argument may be a floating point number for subsecond precision.”
4.ctime(…)
ctime(seconds) -> string
將一個時間戳(默認為當(dāng)前時間)轉(zhuǎn)換成一個時間字符串
例如:
time.ctime()
輸出為:’Sat Mar 28 22:24:24 2009′
5.gmtime(…)
gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)
將一個時間戳轉(zhuǎn)換成一個UTC時區(qū)(0時區(qū))的struct_time,如果seconds參數(shù)未輸入,則以當(dāng)前時間為轉(zhuǎn)換標準
6.localtime(…)
localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
將一個時間戳轉(zhuǎn)換成一個當(dāng)前時區(qū)的struct_time,如果seconds參數(shù)未輸入,則以當(dāng)前時間為轉(zhuǎn)換標準
7.mktime(…)
mktime(tuple) -> floating point number
將一個以struct_time轉(zhuǎn)換為時間戳
8.strftime(…)
strftime(format[, tuple]) -> string
將指定的struct_time(默認為當(dāng)前時間),根據(jù)指定的格式化字符串輸出
python中時間日期格式化符號:
%y 兩位數(shù)的年份表示(00-99)
%Y 四位數(shù)的年份表示(000-9999)
%m 月份(01-12)
%d 月內(nèi)中的一天(0-31)
%H 24小時制小時數(shù)(0-23)
%I 12小時制小時數(shù)(01-12)
%M 分鐘數(shù)(00=59)
%S 秒(00-59)
%a 本地簡化星期名稱
%A 本地完整星期名稱
%b 本地簡化的月份名稱
%B 本地完整的月份名稱
%c 本地相應(yīng)的日期表示和時間表示
%j 年內(nèi)的一天(001-366)
%p 本地A.M.或P.M.的等價符
%U 一年中的星期數(shù)(00-53)星期天為星期的開始
%w 星期(0-6),星期天為星期的開始
%W 一年中的星期數(shù)(00-53)星期一為星期的開始
%x 本地相應(yīng)的日期表示
%X 本地相應(yīng)的時間表示
%Z 當(dāng)前時區(qū)的名稱
%% %號本身
9.strptime(…)
strptime(string, format) -> struct_time
將時間字符串根據(jù)指定的格式化符轉(zhuǎn)換成數(shù)組形式的時間
例如:
2009-03-20 11:45:39 對應(yīng)的格式化字符串為:%Y-%m-%d %H:%M:%S
Sat Mar 28 22:24:24 2009 對應(yīng)的格式化字符串為:%a %b %d %H:%M:%S %Y
10.time(…)
time() -> floating point number
返回當(dāng)前時間的時間戳
三、疑點
1.夏令時
在struct_time中,夏令時好像沒有用,例如
a = (2009, 6, 28, 23, 8, 34, 5, 87, 1)
b = (2009, 6, 28, 23, 8, 34, 5, 87, 0)
a和b分別表示的是夏令時和標準時間,它們之間轉(zhuǎn)換為時間戳應(yīng)該相關(guān)3600,但是轉(zhuǎn)換后輸出都為646585714.0
四、小應(yīng)用
1.python獲取當(dāng)前時間
time.time() 獲取當(dāng)前時間戳
time.localtime() 當(dāng)前時間的struct_time形式
time.ctime() 當(dāng)前時間的字符串形式
2.python格式化字符串
格式化成2009-03-20 11:45:39形式
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
格式化成Sat Mar 28 22:24:24 2009形式
time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())
3.將格式字符串轉(zhuǎn)換為時間戳
a = "Sat Mar 28 22:24:24 2009" b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
轉(zhuǎn)載自csdn
python 的內(nèi)嵌time模板翻譯及說明
一、簡介
time模塊提供各種操作時間的函數(shù)
說明:一般有兩種表示時間的方式:
第一種是時間戳的方式(相對于1970.1.1 00:00:00以秒計算的偏移量),時間戳是惟一的
第二種以數(shù)組的形式表示即(struct_time),共有九個元素,分別表示,同一個時間戳的struct_time會因為時區(qū)不同而不同
year (four digits, e.g. 1998)
month (1-12)
day (1-31)
hours (0-23)
minutes (0-59)
seconds (0-59)
weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1) 是否是夏令時
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is -1, mktime() should guess based on the date and time.
二、函數(shù)介紹
1.asctime()
asctime([tuple]) -> string
將一個struct_time(默認為當(dāng)時時間),轉(zhuǎn)換成字符串
Convert a time tuple to a string, e.g. ‘Sat Jun 06 16:26:11 1998′.
When the time tuple is not present, current time as returned by localtime()
is used.
2.clock()
clock() -> floating point number
該函數(shù)有兩個功能,
在第一次調(diào)用的時候,返回的是程序運行的實際時間;
以第二次之后的調(diào)用,返回的是自第一次調(diào)用后,到這次調(diào)用的時間間隔
示例:
view plaincopy to clipboardprint? import time if __name__ == '__main__': time.sleep(1) print "clock1:%s" % time.clock() time.sleep(1) print "clock2:%s" % time.clock() time.sleep(1) print "clock3:%s" % time.clock()
輸出:
clock1:3.35238137808e-006
clock2:1.00004944763
clock3:2.00012040636
其中第一個clock輸出的是程序運行時間
第二、三個clock輸出的都是與第一個clock的時間間隔
3.sleep(…)
sleep(seconds)
線程推遲指定的時間運行,經(jīng)過測試,單位為秒,但是在幫助文檔中有以下這樣一句話,這關(guān)是看不懂
“The argument may be a floating point number for subsecond precision.”
4.ctime(…)
ctime(seconds) -> string
將一個時間戳(默認為當(dāng)前時間)轉(zhuǎn)換成一個時間字符串
例如:
time.ctime()
輸出為:’Sat Mar 28 22:24:24 2009′
5.gmtime(…)
gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)
將一個時間戳轉(zhuǎn)換成一個UTC時區(qū)(0時區(qū))的struct_time,如果seconds參數(shù)未輸入,則以當(dāng)前時間為轉(zhuǎn)換標準
6.localtime(…)
localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
將一個時間戳轉(zhuǎn)換成一個當(dāng)前時區(qū)的struct_time,如果seconds參數(shù)未輸入,則以當(dāng)前時間為轉(zhuǎn)換標準
7.mktime(…)
mktime(tuple) -> floating point number
將一個以struct_time轉(zhuǎn)換為時間戳
8.strftime(…)
strftime(format[, tuple]) -> string
將指定的struct_time(默認為當(dāng)前時間),根據(jù)指定的格式化字符串輸出
python中時間日期格式化符號:
%y 兩位數(shù)的年份表示(00-99)
%Y 四位數(shù)的年份表示(000-9999)
%m 月份(01-12)
%d 月內(nèi)中的一天(0-31)
%H 24小時制小時數(shù)(0-23)
%I 12小時制小時數(shù)(01-12)
%M 分鐘數(shù)(00=59)
%S 秒(00-59)
%a 本地簡化星期名稱
%A 本地完整星期名稱
%b 本地簡化的月份名稱
%B 本地完整的月份名稱
%c 本地相應(yīng)的日期表示和時間表示
%j 年內(nèi)的一天(001-366)
%p 本地A.M.或P.M.的等價符
%U 一年中的星期數(shù)(00-53)星期天為星期的開始
%w 星期(0-6),星期天為星期的開始
%W 一年中的星期數(shù)(00-53)星期一為星期的開始
%x 本地相應(yīng)的日期表示
%X 本地相應(yīng)的時間表示
%Z 當(dāng)前時區(qū)的名稱
%% %號本身
9.strptime(…)
strptime(string, format) -> struct_time
將時間字符串根據(jù)指定的格式化符轉(zhuǎn)換成數(shù)組形式的時間
例如:
2009-03-20 11:45:39 對應(yīng)的格式化字符串為:%Y-%m-%d %H:%M:%S
Sat Mar 28 22:24:24 2009 對應(yīng)的格式化字符串為:%a %b %d %H:%M:%S %Y
10.time(…)
time() -> floating point number
返回當(dāng)前時間的時間戳
三、疑點
1.夏令時
在struct_time中,夏令時好像沒有用,例如
a = (2009, 6, 28, 23, 8, 34, 5, 87, 1)
b = (2009, 6, 28, 23, 8, 34, 5, 87, 0)
a和b分別表示的是夏令時和標準時間,它們之間轉(zhuǎn)換為時間戳應(yīng)該相關(guān)3600,但是轉(zhuǎn)換后輸出都為646585714.0
四、小應(yīng)用
1.python獲取當(dāng)前時間
time.time() 獲取當(dāng)前時間戳
time.localtime() 當(dāng)前時間的struct_time形式
time.ctime() 當(dāng)前時間的字符串形式
2.python格式化字符串
格式化成2009-03-20 11:45:39形式
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
格式化成Sat Mar 28 22:24:24 2009形式
time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())
3.將格式字符串轉(zhuǎn)換為時間戳
a = "Sat Mar 28 22:24:24 2009" b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
轉(zhuǎn)載自csdn
From:http://www.juyimeng.com/python-common-time-function.html
我們先導(dǎo)入必須用到的一個module
>>> import time
設(shè)置一個時間的格式,下面會用到
>>>ISOTIMEFORMAT=’%Y-%m-%d %X’
看一下當(dāng)前的時間,和其他很多語言相似這是從epoch(1970 年 1 月 1 日 00:00:00)開始到當(dāng)前的秒數(shù)。
>>> time.time()
1180759620.859
上面的看不懂,換個格式來看看
>>> time.localtime()
(2007, 6, 2, 12, 47, 7, 5, 153, 0)
localtime返回tuple格式的時間,有一個和它類似的函數(shù)叫g(shù)mtime(),2個函數(shù)的差別是時區(qū),gmtime()返回的是0時區(qū)的值,localtime返回的是當(dāng)前時區(qū)的值。
>>> time.strftime( ISOTIMEFORMAT, time.localtime() )
’2007-06-02 12:54:29′
用上我們的時間格式定義了,使用strftime對時間做一個轉(zhuǎn)換,如果取現(xiàn)在的時間,time.localtime() 可以不用。
>>> time.strftime( ISOTIMEFORMAT, time.localtime( time.time() ) )
’2007-06-02 12:54:31′
>>> time.strftime( ISOTIMEFORMAT, time.gmtime( time.time() ) )
’2007-06-02 04:55:02′
上面展示了gmtime和localtime的區(qū)別。
查看時區(qū)用
>>> time.timezone
-28800
上面的值是一個秒值,是當(dāng)前時區(qū)和0時區(qū)相差的描述,-28800=-8*3600,即為東八區(qū)。
帖幾個簡單的函數(shù)
def ISOString2Time( s ):
'''
convert a ISO format time to second
from:2006-04-12 16:46:40 to:23123123
把一個時間轉(zhuǎn)化為秒
'''
return time.strptime( s, ISOTIMEFORMAT )
def Time2ISOString( s ):
'''
convert second to a ISO format time
from: 23123123 to: 2006-04-12 16:46:40
把給定的秒轉(zhuǎn)化為定義的格式
'''
return time.strftime( ISOTIMEFORMAT, time.localtime( float( s) ) )
def dateplustime( d, t ):
'''
d=2006-04-12 16:46:40
t=2小時
return 2006-04-12 18:46:40
計算一個日期相差多少秒的日期,time2sec是另外一個函數(shù),可以處理,3天,13分鐘,10小時等字符串,回頭再來寫這個,需要結(jié)合正則表達式。
'''
return Time2ISOString( time.mktime( ISOString2Time( d ))+time2sec( t ) )
def dateMinDate( d1, d2 ):
'''
minus to iso format date,return seconds
計算2個時間相差多少秒
'''
d1=ISOString2Time( d1 )
d2=ISOString2Time( d2 )
return time.mktime( d1 )-time.mktime( d2 )
首先將需要升級的文件放到一個路徑下面。
然后指定升級命令,將參數(shù)指定到補丁文件的位置和配置文件。
假設(shè)升級文件經(jīng)過上傳,到了 /opt/package1 中,在package1中,或許是一個單一的解壓后的ifix包,或許是多個zip格式的ifix壓縮包?;蛟S是若干 rpm包。也可能是zip包和rpm包的混合。
每個ifix文件是zip格式,每個zip文件的根目錄下都有一個配置文件:repository.xml
而rpm包,則是linux系統(tǒng)上 使用 rpm –Uvh 進行安裝。
其中,ifix包使用 IBM InstallationManager的指令 imcl進行安裝。
一個合理的思路就是:
1.檢測package1下面有無rpm文件,有則將其整理成一個list,交給 rpm –Uvh
2.檢測package1下面有無 zip文件且zip中必須含有repository.xml文件
3.如果2中檢測到有符合要求的zip文件,則將其解壓到/opt/package2中,將ifix 路徑傳給 imcl.如果沒有符合要求的zip文件,則檢測當(dāng)前目錄下,有無repository.xml 有則將package1復(fù)制到opt/package2/package1中,交給imcl處理。
install sh
#! /bin/sh
ZIPPATH=/opt/zip
IFIXTOOL=/home/hailiang/script/ifixtool.sh
# List Zips
listZips() {
for file in `find $ZIPPATH -type f -name '*.zip'`;do
(unzip -t "${file}"|grep repository.xml) &> /dev/null && echo $file
done
}
# Update IM
updateIM(){
zipList=`listZips`
echo "Applying iFixs:"$zipList
${IFIXTOOL} $zipList
}
# Update RPM
{
filename=$1
echo "Apply rpm fix file(s)"
rpm -Uvh $filename
}
# Main
echo ">> start to install ifix files "
# check if there is any .rpm file under /opt/zip
rpmCount=`expr $(ls $ZIPPATH/*.rpm 2>/dev/null|wc -l)`
if [ $rpmCount -ne 0 ]; then
fixList=`ls $ZIPPATH`
echo "installing rpm(s):"$fixList
rpmList=""
for file in $ZIPPATH/*.rpm;do
rpmList=$rpmList" "$file
done
echo "Apply rpm(s):"$rpmList
rpm -Uvh $rpmList
fi
# installing ifix and ignore some times.
echo "ensure permissions are suitable for update"
chown -hR $username:users /tmp/update
echo "installing ifix"
installed=0
#Check whether there is any zip file
zipCount=`expr $(listZips|wc -l)`
if [ $zipCount -eq 0 ]; then
# Treat is as a single ifix IM repo
cd $ZIPPATH
zip -r fix,zip `find -type f -name repository.xml -exec dirname{} \;`
cd -
zipCount=1
fi
fixtotal=$zipCount
echo $fixtotal needs to be applyed .
updateIM
else
echo " nothing happened."
fi
# Remove files
rm -rf $ZIPPATH
echo "<< exit"
tool shell
#! /bin/sh
# set -x
zipfilelist=""
for zipfile;do
if [ -f "$zipfile" ];then
zipfiledir=$(cd `dirname "${zipfile}"`;pwd)
zipfilename=$(basename "${zipfile}")
zipfilelist="${zipfiledir}/${zipfilename} ${zipfilelist}"
fi
done
echo $zipfilelist
#unzip ifix
rm -rf /tmp/im
mkdir /tmp/im
cd /tmp/im
for file in ${zipfilelist};do
if [ -f "$file" ]; then
filename=$(basename "${file}")
unzip -d ${filename%.*} "$file"
fi
done
LEARN FROM:http://learn.akae.cn/media/ch31s03.html
環(huán)境變量可以從父進程傳給子進程,因此Shell進程的環(huán)境變量可以從當(dāng)前Shell進程傳給fork
出來的子進程。用printenv
命令可以顯示當(dāng)前Shell進程的環(huán)境變量.用set
命令可以顯示當(dāng)前Shell進程中定義的所有變量(包括本地變量和環(huán)境變量)和函數(shù).
本地變量:$ VARNAME=value 注意等號兩邊都不能有空格,否則會被Shell解釋成命令和命令行參數(shù)。
子進程shell變量導(dǎo)出:$ export VARNAME=value 這樣父進程的Shell也可以使用這個變量
ls ch[012][0-9].doc
由反引號括起來的也是一條命令,Shell先執(zhí)行該命令,然后將輸出結(jié)果立刻代換到當(dāng)前命令行中。例如定義一個變量存放date
命令的輸出:
DATE=$(date)
用于算術(shù)計算,$(())
中的Shell變量取值將轉(zhuǎn)換成整數(shù)
#! /bin/sh
echo "is it moring? Please answer yes or no ."
read YES_OR_NO
case "$YES_OR_NO" in
yes|Yes|y|YES)
echo "Good Moring!";;
[nN]*)
echo "Good afternoon.";;
*)
echo "Sorry, $YES_OR_NO not recognized. Enter yes or no . "
exit 1;;
esac
exit 0
#! /bin/sh
for FRUIT in apple banana pear ; do
echo "I like $FRUIT"
done
#! /bash/sh
echo "is it morning? please answer yes or no ."
read YES_OR_NO
if [ "$YES_OR_NO" = "yes" ]; then
echo "Good Morning."
elif [ "$YES_OR_NO" = "no" ]; then
echo "Good afternoon."
else
echo "Sorry ,$YES_OR_NO not recognized.Enter yes or no."
exit 1
fi
exit 0
#! /bin/sh
echo "Enter Password:"
read TRY
while [ "$TRY" != "p" ]; do
echo "Sorry, Try again"
read TRY
done
#! /bin/bash echo "the number of params $#" echo "the contents of params $@" shift echo "$1" shift echo "$1" shift echo "$1"
shift 向左偏移 ,$#代表傳進參數(shù)的個數(shù) 而$@是具體的內(nèi)容,當(dāng)執(zhí)行shift的時候,$#和$@也會相應(yīng)的改變。
#! /bin/bash
echo "the number of params $#"
echo "the contents of params $@"
shift
echo "num:$# contents:$@"
shift
echo "num:$# contents:$@"
#! /bin/sh
is_d()
{
DIR_NAME=$1
if ( test -d $DIR_NAME ); then
return 1
else
return 0
fi
}
for DIR in "$@" ; do
if is_d "$DIR"
then :
else
echo "not a dir"
fi
done
Shell提供了一些用于調(diào)試腳本的選項,如下所示:
讀一遍腳本中的命令但不執(zhí)行,用于檢查腳本中的語法錯誤
一邊執(zhí)行腳本,一邊將執(zhí)行過的腳本命令打印到標準錯誤輸出
提供跟蹤執(zhí)行信息,將執(zhí)行的每一條命令和結(jié)果依次打印出來
使用這些選項有三種方法,一是在命令行提供參數(shù)
set -x
和set +x
分別表示啟用和禁用-x
參數(shù),這樣可以只對腳本中的某一段進行跟蹤調(diào)試。
從今天的一些編碼來看,Shell編程要注意腳本中的空格,在if,while語句中 “[ ]”要留空格,以及變量的定義中,等號兩端不要存有空格。