Вопрос

#!/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

For this code, what i am trying to do is that, if i have a script called find.sh, when i type in find.sh without any directory, it will just find files that are in current directory, if a directory is given for example, find.sh test, it will find all the files in test directory, problem is i am not sure if the first part of the code is right, not sure if its finding the current directory or not.

Это было полезно?

Решение

Here is the fix

#!/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

Explanation

  • dir1=${1:-.} assign $1 to dir1 if $1 is exist, otherwise, give the default value . . . means current directory.

Другие советы

You don't seem to be using Dir2 there.

I would do

if [ -z $1 ]
then
   Dir1=$(/bin/pwd)
else
   Dir1=$1
fi

for your variable detection. then you just need:

number=$(find $Dir1 -type f |wc -l)
echo "$Dir1 has $number files" 

and same job for your executable.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top