Skip to main content
added 181 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

Your error is here:

if [ DIR eq $1 ]

You are not using the variable, that's the simple string DIR. What you want is:

if [ "$DIR" = "$1" ]

However, since you are setting DIR="$1", that test will always be true by definition. Also, when $1 is empty, so will $DIR and the find command will be run on the current directory. For many versions of find, these commands are equivalent:

find . -name foo
find -name foo

In the absence of a target directory, find will search through ., the current directory. So, a working version of your script would be:

#!/bin/bash

## Avoid using UPPER CASE variable names in bash.
dir="$1"

## Check if $dir exists
if [ -e "$dir" ]
then
    printf "total directories: %s\n" $(find "$dir" -type d | wc -l)
    printf "total files: %s\n" $(find "$dir" -type f | wc -l)
else
    echo " pass parameters"
fi

The number of arguments passed to a bash script is saved as $#, so you could also do

if [ $# -lt 1 ]
then
    echo "pass parameters"
    exit
fi

Your error is here:

if [ DIR eq $1 ]

You are not using the variable, that's the simple string DIR. What you want is:

if [ "$DIR" = "$1" ]

However, since you are setting DIR="$1", that test will always be true by definition. Also, when $1 is empty, so will $DIR and the find command will be run on the current directory. For many versions of find, these commands are equivalent:

find . -name foo
find -name foo

In the absence of a target directory, find will search through ., the current directory. So, a working version of your script would be:

#!/bin/bash

## Avoid using UPPER CASE variable names in bash.
dir="$1"

## Check if $dir exists
if [ -e "$dir" ]
then
    printf "total directories: %s\n" $(find "$dir" -type d | wc -l)
    printf "total files: %s\n" $(find "$dir" -type f | wc -l)
else
    echo " pass parameters"
fi

Your error is here:

if [ DIR eq $1 ]

You are not using the variable, that's the simple string DIR. What you want is:

if [ "$DIR" = "$1" ]

However, since you are setting DIR="$1", that test will always be true by definition. Also, when $1 is empty, so will $DIR and the find command will be run on the current directory. For many versions of find, these commands are equivalent:

find . -name foo
find -name foo

In the absence of a target directory, find will search through ., the current directory. So, a working version of your script would be:

#!/bin/bash

## Avoid using UPPER CASE variable names in bash.
dir="$1"

## Check if $dir exists
if [ -e "$dir" ]
then
    printf "total directories: %s\n" $(find "$dir" -type d | wc -l)
    printf "total files: %s\n" $(find "$dir" -type f | wc -l)
else
    echo " pass parameters"
fi

The number of arguments passed to a bash script is saved as $#, so you could also do

if [ $# -lt 1 ]
then
    echo "pass parameters"
    exit
fi
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

Your error is here:

if [ DIR eq $1 ]

You are not using the variable, that's the simple string DIR. What you want is:

if [ "$DIR" = "$1" ]

However, since you are setting DIR="$1", that test will always be true by definition. Also, when $1 is empty, so will $DIR and the find command will be run on the current directory. For many versions of find, these commands are equivalent:

find . -name foo
find -name foo

In the absence of a target directory, find will search through ., the current directory. So, a working version of your script would be:

#!/bin/bash

## Avoid using UPPER CASE variable names in bash.
dir="$1"

## Check if $dir exists
if [ -e "$dir" ]
then
    printf "total directories: %s\n" $(find "$dir" -type d | wc -l)
    printf "total files: %s\n" $(find "$dir" -type f | wc -l)
else
    echo " pass parameters"
fi