我需要檢查Perl腳本中是否存在任何一組目錄。這些目錄以XXXX * YYY格式命名 - 我需要檢查每個XXXX並輸入if語句(如果爲true)。用通配符檢查Perl中的目錄是否存在
在我的腳本中,我有兩個變量$ monitor_location(包含被掃描根目錄的路徑)和$ clientid(包含XXXX)。
下面的代碼片段已被擴展,以顯示更多我在做什麼。我有一個查詢返回每個客戶端ID,然後循環返回每條記錄並嘗試計算該客戶端ID使用的磁盤空間。
我有以下代碼到目前爲止(不工作):
# loop for each client
while (($clientid, $email, $name, $max_record) = $query_handle1->fetchrow_array())
{
# add leading zeroes to client ID if needed
$clientid=sprintf"%04s",$clientid;
# scan file system to check how much recording space has been used
if (-d "$monitor_location/$clientid\*") {
# there are some call recordings for this client
$str = `du -c $monitor_location/$clientid* | tail -n 1 2>/dev/null`;
$str =~ /^(\d+)/;
$client_recspace = $1;
print "Client $clientid has used $client_recspace of $max_record\n";
}
}
要清楚,我想如果有與XXXX開始的任何文件夾進入if語句。
希望這是有道理的!由於
我已經編輯了問題中的代碼,以更好地展示我想實現的目標。想知道如何將上面的代碼嵌入到我的代碼中,會感激不小的輸入嗎?謝謝 – btongeorge 2012-07-26 09:03:34
好的解決方案和良好的'grep'使用方法 – gaussblurinc 2012-07-26 09:12:17
@btongeorge:如果你只是想知道這些目錄是否存在,你可以使用'if(grep -d,glob「$ ...」)''。 – choroba 2012-07-26 09:26:44