[英]Program to check if each item is file or directory in path
我已經有了這個起始代碼,但是我不知道從這里出發。 現在,我正在使用〜/ Desktop路徑,但是它不讀取Desktop目錄。 並為目錄和文件數回顯0。
#!/bin/sh
path="$1"
num_file=0
num_dir=0
for item in $path; do
if [ -d ]
then
num_dir+=1
elif [ -f ]
then
num_file+=1
fi
done
echo "The number of directories is $num_dir"
echo "The number of files is $num_file"
path="$1"
dirs=$(find "$path" -mindepth 1 -type d -printf "d");
fils=$(find "$path" -mindepth 1 -type f -printf "f");
echo "dirs: ${#dirs} files: ${#fils}"
打印變量的大小,這些變量僅收集字符“ d”或“ f”。
為避免訪問子目錄,請將-maxdepth 1
添加到find命令。
在您的代碼中,我看到了多個問題:
for item in $path; do
for item in "$path"/* ; do
您必須使用*來獲取項目列表。
if [ -d ]
if [ -d "$item" ]
如果-d是什么? 好吧,項目。
num_dir+=1
((num_dir+=1))
雙圓括號做arithmetik。 在Bash中-至少。 也許您在“ sh-Shell”上所做的某些事情,但我對此表示懷疑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.