Java 企业应用
不要温柔的走入那个良夜

 

程序的目的是在redhat和AIX上同时运行,对于很多指令,使用Redhat完成了,但是AIX上没有,这时,我们选择Alias的方式,在AIX的Env中,提供具备这样功能的指令模块。

下面是研究怎么实现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).

image

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

The IP version to which this filter applies. Valid values are "4" and "6"
-n

The filter ID, or number: The new rule will be added before the number specified with this flag. If not specified, the rule will be added to the end of the filter rules table.
-a

The "action" of the rule: valid values are "P" (permit) and "D" (deny)
-s

The source address: Specify either a fully qualified domain name (FQDN) or an IP address of the host or network to which this rule will apply. The value "0.0.0.0" specifies all IP addresses.
-m

The source subnet mask: This will be used with the source address in determining whether this filter rule matches. The value "0.0.0.0" specifies all subnet masks.
-d

The destination address: Specify either the FQDN or the IP address of the interface for which incoming packets should be matched against. The value "0.0.0.0" specifies all IP addresses on the system.
-M

The destination subnet mask: This will be used with the destination address in determining whether this filter rule matches. The value "0.0.0.0" specifies all subnet masks.
-g

Specifies whether this rule applies to source routed packets. Valid values are "Y" (yes) and "N" (no).
-c

Protocol: Specify the protocols which will be matched by this filter rule. Valid values are "udp", "icmp", "tcp", "tcp/ack", and "all".
-o

Source port/ICMP operation: This is the comparison operator that will be used in matching the source port of the packet to this rule. Valid values are "lt" (less than), "le" (less than or equal to), "gt" (greater than), "ge" (greater than or equal to), "eq" (equal), "neq" (not equal), or "any".
-p

Source port/ICMP type: This value will be compared to the source port of the packet for possible matches.
-O

Destination port/ICMP operation: This is the comparison operator that will be used in matching the destination port of the packet to this rule. Valid values are the same as for the "-o" flag.
-P

Destination port/ICMP type: This value will be compared to the destination port of the packet for possible matches.
-r

Routing/Scope: Specifies whether the rule will apply to forwarded packets (R), packets destined or originated from the local host (L), or both (B).
-w

Direction: Specifies whether the rule will apply to incoming packets (I), outgoing packets (O), or both (B).
-l

Logging: Specifies that an entry to syslog will be sent for packets that match this rule. Valid values are "Y" (yes) and "N" (no).
-f

Fragmentation control: Specifies whether the rule will apply to fragment headers and unfragmented packets (H), fragment headers and fragments only (O), unfragmented packets only (N), or all packets (Y).
-i

Interface: specifies the interface on which this filter rule applies. Valid values are the logical names of interfaces (en0, tr0, lo0, etc.) or "all" for all interfaces.

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.

  • lsfilt: List filters rules present in the table. When created, each rule is assigned a number, which can be easily seen using this command.
  • genfilt: Adds a filter rule to the table. This is the one you use to create new filters. If you do not specify a position with the –nparameter, the new rule is added at the end of the table.
  • chfilt: Used to change existing filter rules. You need to provide the rule ID to indicate which rule you want to modify. Rule 1 is the default rule and can't be changed with this command.
  • rmfilt: The 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.
  • mkfilt: This is a key command that allows us to activate or deactivate the filter rules in the table, enable or disable logging for filters, and change the default rules. For the changes done to the filters table to take effect, you'll have to run this command with some arguments.

 

Refer:http://www.darklab.net/resources/aix-ipsec-filtering.html

           http://unix.ittoolbox.com/groups/technical-functional/ibm-aix-l/implementing-ipsec-on-aix-machines-1332621

           http://www.ibm.com/developerworks/aix/library/au-aixfiltering/index.html

使用过程:

(1)启动/关闭

# 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)设置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中防火墙流行的管理工具
针对本文的作用,仅仅说明一种使用,若要详尽的了解,需耐心的阅读一个非常优秀的文章,链接如下:
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表上添加一条规则,如果socket pack来自TCP且push到${port}端口,就接收。

cmd 2 .在filter表上添加一条规则,如果源IP地址是本机,就接收。

 

Step (4)mock iptables

如果是用IPsec来实现打开端口,允许访问的话,是这样:

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
}

但是,这还没有结束

使用iptables时,我们是这样:

iptables -A INPUT -p tcp -s localhost -j ACCEPT
service iptables save

语句1 用来实现添加一条 rules

语句2 用来使变更生效

所以,还需要mock service 方法,这样,在两个平台中,语句1和语句2就都完成了打开一些防火墙端口的功能。  

function aix_service
{
    if [ "$1" = "iptables" ]; then
        mkfilt -u  
    else
        echo "aix_service $*"
    fi
}

对于系统的判断:

function on_AIX
{
    test "`uname`" = "AIX"
}

如果是AIX系统的话,就要在AIX上执行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环境下进行的。

posted @ 2012-08-11 13:59 cpegtop 阅读(4206) | 评论 (0)编辑 收藏
 

http://qlj.sh.cn/python/20100402/python-time/

 

python 的内嵌time模板翻译及说明

一、简介

time模块提供各种操作时间的函数
说明:一般有两种表示时间的方式:
第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的
第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同
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.

二、函数介绍
1.asctime()
asctime([tuple]) -> string
将一个struct_time(默认为当时时间),转换成字符串
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
该函数有两个功能,
在第一次调用的时候,返回的是程序运行的实际时间;
以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔

示例:

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)
线程推迟指定的时间运行,经过测试,单位为秒,但是在帮助文档中有以下这样一句话,这关是看不懂
“The argument may be a floating point number for subsecond precision.”

4.ctime(…)
ctime(seconds) -> string
将一个时间戳(默认为当前时间)转换成一个时间字符串
例如:

  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)
将一个时间戳转换成一个UTC时区(0时区)的struct_time,如果seconds参数未输入,则以当前时间为转换标准

6.localtime(…)
localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
将一个时间戳转换成一个当前时区的struct_time,如果seconds参数未输入,则以当前时间为转换标准

7.mktime(…)
mktime(tuple) -> floating point number
将一个以struct_time转换为时间戳

8.strftime(…)
strftime(format[, tuple]) -> string
将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出
python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)

%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身

9.strptime(…)
strptime(string, format) -> struct_time
将时间字符串根据指定的格式化符转换成数组形式的时间
例如:
2009-03-20 11:45:39 对应的格式化字符串为:%Y-%m-%d %H:%M:%S
Sat Mar 28 22:24:24 2009 对应的格式化字符串为:%a %b %d %H:%M:%S %Y

10.time(…)
time() -> floating point number
返回当前时间的时间戳

三、疑点
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分别表示的是夏令时和标准时间,它们之间转换为时间戳应该相关3600,但是转换后输出都为646585714.0

四、小应用
1.python获取当前时间
time.time() 获取当前时间戳
time.localtime() 当前时间的struct_time形式
time.ctime() 当前时间的字符串形式

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.将格式字符串转换为时间戳

  a = "Sat Mar 28 22:24:24 2009"
  b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

转载自csdn

python 的内嵌time模板翻译及说明

一、简介

time模块提供各种操作时间的函数
说明:一般有两种表示时间的方式:
第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的
第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同
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.

二、函数介绍
1.asctime()
asctime([tuple]) -> string
将一个struct_time(默认为当时时间),转换成字符串
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
该函数有两个功能,
在第一次调用的时候,返回的是程序运行的实际时间;
以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔

示例:

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)
线程推迟指定的时间运行,经过测试,单位为秒,但是在帮助文档中有以下这样一句话,这关是看不懂
“The argument may be a floating point number for subsecond precision.”

4.ctime(…)
ctime(seconds) -> string
将一个时间戳(默认为当前时间)转换成一个时间字符串
例如:

  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)
将一个时间戳转换成一个UTC时区(0时区)的struct_time,如果seconds参数未输入,则以当前时间为转换标准

6.localtime(…)
localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
将一个时间戳转换成一个当前时区的struct_time,如果seconds参数未输入,则以当前时间为转换标准

7.mktime(…)
mktime(tuple) -> floating point number
将一个以struct_time转换为时间戳

8.strftime(…)
strftime(format[, tuple]) -> string
将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出
python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)

%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身

9.strptime(…)
strptime(string, format) -> struct_time
将时间字符串根据指定的格式化符转换成数组形式的时间
例如:
2009-03-20 11:45:39 对应的格式化字符串为:%Y-%m-%d %H:%M:%S
Sat Mar 28 22:24:24 2009 对应的格式化字符串为:%a %b %d %H:%M:%S %Y

10.time(…)
time() -> floating point number
返回当前时间的时间戳

三、疑点
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分别表示的是夏令时和标准时间,它们之间转换为时间戳应该相关3600,但是转换后输出都为646585714.0

四、小应用
1.python获取当前时间
time.time() 获取当前时间戳
time.localtime() 当前时间的struct_time形式
time.ctime() 当前时间的字符串形式

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.将格式字符串转换为时间戳

  a = "Sat Mar 28 22:24:24 2009"
  b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

转载自csdn

posted @ 2012-07-30 13:45 cpegtop 阅读(4872) | 评论 (0)编辑 收藏
 

From:http://www.juyimeng.com/python-common-time-function.html

我们先导入必须用到的一个module
>>> import time
设置一个时间的格式,下面会用到
>>>ISOTIMEFORMAT=’%Y-%m-%d %X’
看一下当前的时间,和其他很多语言相似这是从epoch(1970 年 1 月 1 日 00:00:00)开始到当前的秒数。
>>> time.time()
1180759620.859
上面的看不懂,换个格式来看看
>>> time.localtime()
(2007, 6, 2, 12, 47, 7, 5, 153, 0)
localtime返回tuple格式的时间,有一个和它类似的函数叫gmtime(),2个函数的差别是时区,gmtime()返回的是0时区的值,localtime返回的是当前时区的值。
>>> time.strftime( ISOTIMEFORMAT, time.localtime() )
’2007-06-02 12:54:29′
用上我们的时间格式定义了,使用strftime对时间做一个转换,如果取现在的时间,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的区别。
查看时区用
>>> time.timezone
-28800
上面的值是一个秒值,是当前时区和0时区相差的描述,-28800=-8*3600,即为东八区。

帖几个简单的函数

def ISOString2Time( s ):
'''
    convert a ISO format time to second
    from:2006-04-12 16:46:40 to:23123123
    把一个时间转化为秒
'''
return time.strptime( s, ISOTIMEFORMAT )
def Time2ISOString( s ):
'''
    convert second to a ISO format time
    from: 23123123 to: 2006-04-12 16:46:40
    把给定的秒转化为定义的格式
'''
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是另外一个函数,可以处理,3天,13分钟,10小时等字符串,回头再来写这个,需要结合正则表达式。
'''
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 )

posted @ 2012-07-30 13:40 cpegtop 阅读(170) | 评论 (0)编辑 收藏
 

     首先将需要升级的文件放到一个路径下面。

     然后指定升级命令,将参数指定到补丁文件的位置和配置文件。

     假设升级文件经过上传,到了 /opt/package1 中,在package1中,或许是一个单一的解压后的ifix包,或许是多个zip格式的ifix压缩包。或许是若干 rpm包。也可能是zip包和rpm包的混合。

     每个ifix文件是zip格式,每个zip文件的根目录下都有一个配置文件:repository.xml

     而rpm包,则是linux系统上 使用 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文件,则检测当前目录下,有无repository.xml 有则将package1复制到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
 
posted @ 2012-07-17 15:29 cpegtop 阅读(166) | 评论 (0)编辑 收藏
 

           LEARN FROM:http://learn.akae.cn/media/ch31s03.html

 

1.变量

环境变量可以从父进程传给子进程,因此Shell进程的环境变量可以从当前Shell进程传给fork出来的子进程。用printenv命令可以显示当前Shell进程的环境变量.用set命令可以显示当前Shell进程中定义的所有变量(包括本地变量和环境变量)和函数.

本地变量:$ VARNAME=value   注意等号两边都不能有空格,否则会被Shell解释成命令和命令行参数。

子进程shell变量导出:$ export VARNAME=value 这样父进程的Shell也可以使用这个变量

2.通配符

   image

ls ch[012][0-9].doc

3.命令代换

由反引号括起来的也是一条命令,Shell先执行该命令,然后将输出结果立刻代换到当前命令行中。例如定义一个变量存放date命令的输出:

image

DATE=$(date)

4.算数代换

用于算术计算,$(())中的Shell变量取值将转换成整数

5.转义“\”

6.条件测试

image

 

7.控制
    case

#! /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

    for

#! /bin/sh
 
for FRUIT in apple banana pear ; do 
    echo "I like $FRUIT"
done
 

       if

#! /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

       While

 

#! /bin/sh
 
echo "Enter Password:"
read TRY
while [ "$TRY" != "p" ]; do 
    echo "Sorry, Try again"
    read TRY
done

      Shift

#! /bin/bash
echo "the number of params $#" 
echo "the contents of params $@"
shift 
echo "$1" 
shift 
echo "$1"
shift
echo "$1"

shift 向左偏移 ,$#代表传进参数的个数 而$@是具体的内容,当执行shift的时候,$#和$@也会相应的改变。

#! /bin/bash
echo "the number of params $#"
echo "the contents of params $@"
shift
echo "num:$# contents:$@"
shift
echo "num:$# contents:$@"

image

 

8.函数

#! /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

9.调试

Shell提供了一些用于调试脚本的选项,如下所示:

-n

读一遍脚本中的命令但不执行,用于检查脚本中的语法错误

-v

一边执行脚本,一边将执行过的脚本命令打印到标准错误输出

-x

提供跟踪执行信息,将执行的每一条命令和结果依次打印出来

使用这些选项有三种方法,一是在命令行提供参数

set -xset +x分别表示启用和禁用-x参数,这样可以只对脚本中的某一段进行跟踪调试。

 

从今天的一些编码来看,Shell编程要注意脚本中的空格,在if,while语句中 “[ ]”要留空格,以及变量的定义中,等号两端不要存有空格。

posted @ 2012-06-25 18:12 cpegtop 阅读(217) | 评论 (0)编辑 收藏
仅列出标题
共6页: 上一页 1 2 3 4 5 6 下一页