我们先来了解一下关于exec函数的使用方法
exec() 原型:string exec (string command [, string array [, int return_var]])
exec ()函数与system()类似,也执行给定的命令,但不输出结果,而是返回结果的最后一行。虽然它只返回命令结果的最后一行,但用第二个参数array 可以得到完整的结果,方法是把结果逐行追加到array的结尾处。所以如果array不是空的,在调用之前最好用unset()最它清掉。只有指定了第二 个参数时,才可以用第三个参数,用来取得命令执行的状态码。
例子:
- exec("/bin/ls -l");
- exec("/bin/ls -l", $res);
- exec("/bin/ls -l", $res, $rc);
第三个参数, 怎么不能接收负数??这里的&$return_var就是程序返回值,起初我的回答是可以为负数,一般在C语言里我们会这样写
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- printf("^_^n");
- return -5;
- }
这个-5就是返回值,但习惯上是写成0或者1的。
注意:很多人的C代码里把main函数写成 void main() 这样实际上是不对的,详细的就不说了。
把上面的代码编译后,到CMD下运行,然后就能看到输出结果了。接着,输入“echo %ERRORLEVEL%”,回车,就可以看到程序的返回值了。这个%ERRORLEVEL%就代表了程序的返回状态。在WIN下确实是可以为负数的,php调用也是正常的。
- E:devphp535>php -r "exec('return.exe',$out,$a);var_dump($a);"
- int(-2)
但是到了linux下,始终为正数,刚开始怀疑是权限问题,用了chmod +x后,排除了权限问题。
- exec("/home/wwwroot/test/rtest.out 2>&1",$out,$a);
- var_dump($out,$a);
- array(1) { [0]=> string(3) "^_^" } int(251)
看起来成了256+return val,可以看到实际上返回了负数,只不过被转换成正数了,接着看了下standard/exec.c里的源代码,没发现啥端倪,干到很奇怪,突然想到自己忘了一步,忘了看程序返回给OS的值了.可以使用echo $? 显示最后命令的推出状况。
- -bash-3.00$ vi main.c
- -bash-3.00$ gcc -o ./mm main.c
- -bash-3.00$ ll
- total 48
- drwxr-xr-x 3 www www 4096 May 4 2011 2011
- drwxr-xr-x 6 www www 4096 Jun 23 2011 eoc
- -rwxr-xr-x 1 www www 7131 Feb 1 12:47 hello
- -rw-r–r– 1 www www 3 Feb 1 12:51 hello.c
- -rw-r–r– 1 www www 99 Feb 1 12:50 main.c
- -rwxr-xr-x 1 www www 4714 Feb 1 12:51 mm
- drwxr-xr-x 3 www www 4096 Jun 24 2011 test
- -bash-3.00$ ./mm
- ^_^
- -bash-3.00$ echo $?
- 251
- -bash-3.00$
这样就可以看看exec返换给OS的值是多少。
在linux下,这个返回值就是无符号类型,返回的是一个正数,所以传给php也是正数了,php实际上也是调用的exec所返回的值。
OUT了,也许linux就是这么规定的,程序中泰返回值为1~255区间,而自己不清楚这个规则。不过还没有确定是否linux里就是这么规定的。虽然问题很蛋疼,意义不大,不过也可以在这里留给遇到类似问题的人参考。不过到这里也没深究的必要了。谁要是真想彻底搞清楚,可以看看shell这块。
波比源码 » php exec在linux中返回值不能为负数
order levofloxacin for sale levaquin 500mg pills
avodart over the counter cheap avodart order ondansetron 4mg online
order generic aldactone 100mg oral spironolactone 25mg buy diflucan 200mg sale
order sildenafil 100mg pills careprost tablet brand robaxin 500mg
lamotrigine 200mg usa purchase lamotrigine pill buy tretinoin gel generic
order generic isotretinoin 40mg azithromycin order oral zithromax
buy tadalafil 10mg sale solutions for ed viagra pills 25mg
anastrozole cost acheter viagra generique pas cher sildenafil 50mg pill
deltasone 5mg cheap usa viagra sales cheap sildenafil generic
tadalafil 5mg fГјr frauen cialis kaufen ohne rezept viagra 100mg generika rezeptfrei kaufen
buy doxycycline 100mg without prescription brand clomiphene 50mg cost furosemide 100mg
alendronate 35mg price famotidine 20mg tablet famotidine 40mg generic
cost isosorbide micardis 80mg price purchase micardis generic
zyban oral buy cetirizine for sale seroquel 50mg cheap
purchase zoloft sale buy zoloft 100mg online cheap order sildenafil 100mg generic
oral imuran 100mcg buy generic imuran viagra online order
dapsone us aceon 4mg ca aceon 4mg sale
provera 10mg for sale order cyproheptadine pill order periactin online
generic zithromax 250mg prednisolone 40mg brand order neurontin 100mg sale
cheap furosemide 100mg purchase lasix generic hydroxychloroquine 200mg tablet
brand aralen buy cenforce online cheap order generic baricitinib 2mg
generic itraconazole 100 mg buy itraconazole 100mg pills order tinidazole 300mg for sale
order glucophage 500mg cialis 10mg generic cheap tadalafil 10mg
zyprexa pill nebivolol 5mg tablet valsartan 80mg for sale
clozapine over the counter order clozaril generic buy decadron sale
order cialis sale sildenafil 50mg tablets sildenafil 100mg pills
order orlistat 60mg without prescription acyclovir 800mg tablet zovirax 800mg usa
order generic cialis 10mg buy tadalafil 5mg pill order clopidogrel for sale
order allopurinol sale allopurinol 300mg pill order ezetimibe sale
buy colchicine generic order colchicine roulette free play for fun
buy cialis 5mg online ciprofloxacin 1000mg without prescription buy ciprofloxacin 500mg for sale
zocor usa simvastatin brand order proscar 1mg
cialis tadalafil oral tadalafil 5mg sildenafil 100mg cost
real casinos online no deposit online casino buy cialis 10mg pills
order furosemide 100mg generic order hydroxychloroquine 200mg online cheap buy hydroxychloroquine for sale
order sildenafil 100mg online cheap fildena 50mg cheap order rhinocort generic
purchase spiriva generic buy minocycline without prescription buy hytrin 5mg online
actos 15mg brand viagra cheap order viagra 50mg online
buy montelukast 10mg without prescription buy singulair pill sildenafil cost
purchase tadalafil pill Buy cialis without rx real money online casino
jackpot party casino online roulette free empire casino online
ivermectin 6 mg pills dapsone 100mg pills buy dapsone generic
order nifedipine 30mg for sale order allegra 180mg pills allegra 180mg price
casino games win real money free slots college essay writing help
letter writing services order generic leflunomide 20mg sulfasalazine 500 mg generic
diamox price acetazolamide order buy imuran sale
buy lanoxin 250mg online cheap micardis 20mg usa molnunat pills
carvedilol 25mg price coreg usa amitriptyline 50mg canada
alendronate 35mg usa oral nitrofurantoin 100mg purchase motrin for sale
buy indomethacin 50mg generic indomethacin price cenforce 100mg usa
nortriptyline 25mg sale paxil without prescription purchase paroxetine generic
tricor pill buy sildenafil 50mg online sildenafil pill
tadalafil 10mg pills Cialis next day delivery buy viagra pill
provigil 100mg generic order promethazine for sale cheap promethazine
generic cialis online pharmacy The elf elders were furosemide dosing cold meds safe for transplant and high blood pressure also completely stunned
provigil 100mg cheap order modafinil 200mg for sale metoprolol 100mg cheap
buy doxycycline 200mg online cheap buy acyclovir generic buy zovirax 400mg generic
buy avodart for sale orlistat 120mg without prescription cheap xenical
imuran 50mg cheap naproxen 250mg generic order naprosyn generic