LinuxAnsen 发表于 2013-04-15 11:04

Linux Shell 怎样获得命令的执行结果

find $path1 -maxdepth 1 -iname$moduleName

if [ $? -eq 0 ];
then
   #$moduleCommand $result1
   echo "result of find is OK"
else
   echo "result of find is failed"
fi

大家好,我写了个以上的Shell 程序,在执行这个程序时,执行的结果始终未:result of find is OK,
path1为传进来的目录,moduleName为要查找的模块名,执行时,不管moduleName在指定的目录下是否存在,其执行结果始终为:result of find is OK,跟踪下来,
在 if [ $? -eq 0 ]这个语句处,结果始终为:if [ 0 -eq 0 ]
不知道是什么原因?谢谢!
由于是刚进来的新手,故此题暂时没有悬赏分,在此谢过给予答复的大牛们,谢谢!

cxytz01 发表于 2013-04-16 12:51

$? 为系统变量,只要执行的程序不出错,echo $? 始终为0。 (当然也可以不为0,命令是否出错不能使用$?来判断)

#include <stdio.h>
int main() {

    return 100;
    //return -1;
    //return 0;
    //return 77;
    //return -99;
}

上面的代码里的return你都跑一次,然后echo $?就知道结果。

dengxiayehu 发表于 2013-04-16 13:06

find

EXIT STATUS
       findexitswithstatus0ifall files are processed successfully,
       greater than 0 if errors occur.   This isdeliberatelyaverybroad
       description,butif the return value is non-zero, you should not rely
       on the correctness of the results of find.换个思路撒:
find "$path1" -maxdepth 1 -iname"$moduleName" | wc -l
非零表示文件找到

PS:参数加上双引号
页: [1]
查看完整版本: Linux Shell 怎样获得命令的执行结果