Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何通过脚本计算目录中的文件数_Bash - Fatal编程技术网

Bash 如何通过脚本计算目录中的文件数

Bash 如何通过脚本计算目录中的文件数,bash,Bash,我是一个新用户,我试图构建一个脚本,用它我可以计算一个目录中有多少个文件,我这样做了,但它似乎不起作用 #! /bin/bash read -p 'path: ' dir dir=$dir if [ "$dir" != "stop" ] then exit fi while [ -d $dir ] do wc=$(( ls $dir | wc -l )) done echo $wc 获取计数的更简单方法是: wc=$(find &

我是一个新用户,我试图构建一个脚本,用它我可以计算一个目录中有多少个文件,我这样做了,但它似乎不起作用

#! /bin/bash
read -p 'path: ' dir

dir=$dir

if [ "$dir" != "stop" ]
then
    exit
fi

while [ -d $dir ]
do
    wc=$(( ls $dir | wc -l ))
done

echo $wc

获取计数的更简单方法是:

wc=$(find "$dir" -maxdepth 1 - type f | wc -l)

如果用户输入的不是停止,您确定要退出?是否要
[“$dir”=“stop”]
?如果输入了stop,我想退出脚本。谢谢你的调整。我修改了代码,现在出现了以下错误:第13行:ls/home | wc-l:除以0(错误标记为“home | wc-l”)