Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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 Unix脚本,查找当前目录_Bash_Unix_Find - Fatal编程技术网

Bash Unix脚本,查找当前目录

Bash Unix脚本,查找当前目录,bash,unix,find,Bash,Unix,Find,对于这段代码,我试图做的是,如果我有一个名为find.sh的脚本,当我键入find.sh而没有任何目录时,它将只查找当前目录中的文件,如果给定一个目录,例如find.sh test,它将查找test目录中的所有文件,问题是我不确定代码的第一部分是否正确,不确定它是否找到当前目录 您似乎没有在那里使用Dir2 我愿意 #!/bin/sh Dir1=$1 Dir2=$2 if [ $# -lt 1 ] then echo "`find -type f | wc -l` ordinar

对于这段代码,我试图做的是,如果我有一个名为find.sh的脚本,当我键入find.sh而没有任何目录时,它将只查找当前目录中的文件,如果给定一个目录,例如find.sh test,它将查找test目录中的所有文件,问题是我不确定代码的第一部分是否正确,不确定它是否找到当前目录

您似乎没有在那里使用Dir2

我愿意

#!/bin/sh

Dir1=$1
Dir2=$2


if [ $# -lt 1 ]
then
    echo  "`find -type f | wc -l` ordinary `find -type f -executable | wc -l` executable     `find -type l | wc -l` links `find -type d | wc -l` directories"
else
    if [ $# -eq 1 ]
    then
        echo "$Dir1: `find $Dir1 -type f | wc -l` ordinary `find $Dir1 -type f -executable | wc     -l` executable `find $Dir1 -type l | wc -l` links `find $Dir1 -type d | wc -l` directories"
    else
    fi
fi
用于变量检测。那么你只需要:

if [ -z $1 ]
then
   Dir1=$(/bin/pwd)
else
   Dir1=$1
fi
您的可执行文件也有相同的作业。

这是修复方法

number=$(find $Dir1 -type f |wc -l)
echo "$Dir1 has $number files" 
解释 dir1=${1:-.}如果存在$1,则将$1分配给dir1,否则,请给出默认值。表示当前目录。
目前的输出:28个普通的4个可执行的0个链接4个目录预期输出类似于我现在的输出,但我不确定当前代码是否正在计算我的当前目录。那么,请检查您的假设。注释掉查找之后出现的任何内容,并亲自检查find是否在正确的目录上运行。在这里教你调试101。哦,去掉第二个else,它是多余的,因为else部分是空的。
#!/usr/bin/env bash

dir1=${1:-.}
dir2=${2:-.}
echo $dir1 $dir2

for dir in $dir1 $dir2
do
  ordinary=$(find $dir -type f | wc -l )
  executable=$(find $dir -type f -executable | wc -l)
  directories=$(find $dir -type d | wc -l)
  links=$(find $dir -type l | wc -l)
  echo "$dir: $ordinary $executable $links $directories"
done