1

My script is as shown: it searches for directories and provides info on that directory, however I am having trouble setting exceptions.

if [ -d "$1" ];
then
  directories=$(find "$1" -type d | wc -l)
  files=$(find "$1" -type f | wc -l)
  sym=$(find "$1" -type l | wc -l)

  printf "%s %'d\n" "Directories" $directories
  printf "%s %'d\n" "Files" $files
  printf "%s %'d\n" "Sym links" $sym
  exit 0
else
  echo "Must provide one argument"
  exit 1
fi

How do I make it so that if I search for a file it tells me that a directory needs to be inputted? I'm stuck on it, I've tried test commands but I don't know what to do.

1 Answer 1

1

You're missing your shebang in the first line of your script:

#!/bin/bash

I get correct results from your script if I add it:

Directories 1,991
Files 13,363
Sym links 0

You may have to set the correct execution permissions also chmod +x scriptname.sh?

Entire script looks like this:

#!/bin/bash

if [ -z "$1" ];
        then
                echo "Please provide at least one argument!"
                exit 1
elif [ -d "$1" ];
        then
                directories=$(find "$1" -type d | wc -l)
                files=$(find "$1" -type f | wc -l)
                sym=$(find "$1" -type l | wc -l)
                printf "%s %'d\n" "Directories" $directories
                printf "%s %'d\n" "Files" $files
                printf "%s %'d\n" "Sym links" $sym
                exit 0
        else
                echo "This is a file, not a directory"
                exit 1
fi
Sign up to request clarification or add additional context in comments.

5 Comments

I want a message to say something along the lines of "not a directory" when i search for the file, where would that printf/echo go? Im really new to scripting.
But the script is not searching for a file - please five an example of what you want to find where. I mean you could simply use „find / -name (filename)“, then you wouldn‘t need the rest of the script, or am I getting you wrong?
So the script searches for a directory, if the directory exists, it lists some properties like the number of files in the directory, sym links etc.
If the user doesnt enter an argument/sirectory name at all, the "Must provide one argument" kicks in
I want it if the argument entered is a file name, it asks for the user to enter a directory with a simple echo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.