Linux下判斷是否存在多個同名程序

2021-05-27 10:00:36 字數 526 閱讀 5180

linux 下如何判斷同名程序的個數,這個可以通過shell命令ps -e | grep -c "所查程序名字",就可以得到程序的個數。這裡給出**實現。

bool processnumber(char *name)

;char buf[255] = ;

sprintf(cmd,"ps -e | grep -c %s",name);

if((fptr = popen(cmd,"r")) != null)*/}

}popen(fptr);

printf("the process number is %d\n",atoi(buf));

return bret;

}如果要求同名函式只有乙個,那麼也可以利用這個函式(開啟上面的注釋),當執行這個函式的時候,如果存在的個數大於等於2個,那麼就說明已經存在乙個同名程序,不需要建立新程序。

注:這裡傳進去的引數是程序的名字,不是程序的絕對路徑名。

如果要判斷的是程序的絕對路徑名,可以用命令「ps -ef | grep -c 'pathname' 」。

Linux下C語言判斷檔案是否存在

如下,include include int main void else return 0 簡要分析 使用unistd.h裡的函式access 來判斷檔案是否存在,其原型如下,return 0 if ok return 1 on error int access const char pathna...

Linux判斷檔案是否存在

1,判斷檔案目錄是否存在 bin bash echo 請輸入您要查詢的目錄路徑 read file path fileif d then echo exists else echo not exists fi2,判斷檔案是否存在 bin bash file home bai.txt if f the...

VC下判斷檔案是否存在

方法1 include include include void main void 方法2 if invalid handle value createfile d a.txt generic read,file share read,null,open existing,file attribu...