簡體   English   中英

計算PATH中每個變量中的可執行文件數

[英]Counting number of executable files in every variable inside PATH

我想編寫一個腳本,該腳本將計算變量PATH中每個文件夾中的可執行文件數量。 我的代碼:

#!/bin/bash

IFS=":"
for directory in $PATH; do
        files=0
        ls -l $directory | while read rights x name group siz m d h name; do
                if [ `echo $rights | cut -c1` = "-" ]; then
                        files=$((${files}+1))
                fi
        done
        echo "Directory ${directory} contains ${files} executable files"
done

我希望回聲在while循環結束之后和next for循環開始之前進行處理,但是它總是打印出文件數量=0。if條件內的計數有效。

您是否考慮過使用find命令? 看到這個鏈接 在基於GNU的find版本中,類似以下內容的內容應替換ls調用和while循環。

find $directory -type f -executable -print | wc -l

如果您擔心遍歷子目錄,可以使用maxdepth標志,即

find $directory -maxdepth 1 -type f -executable -print | wc -l

對於BSD版本,請再次參見鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM