1

i have written code to count the number of files and directories, but im struggling to make it exit if no argument is given. here is what i have right now, with the first is statement being the problem. how can i change this if statement to make it exit if no argument is given?

#!/bin/bash
if [$# -eq 0];
 echo "no arguments"
 exit 1
fi
cd "$1" || exit
n=0
m=0
for d in *;
do
    if [ -d "$d" ]; then
        n=$((n+1))
    else
        m=$((m+1))
    fi
done
echo "Files $m"
echo "Directories $n"
Grant Clark
  • 49
  • 1
  • 8

1 Answers1

3

The syntax is: if [ $# -eq 0 ]; then.

The spaces are not optional and nor is the then. So the beginning of your script should be:

#!/bin/bash
if [ $# -eq 0 ]; then
 echo "no arguments"
 exit 1
fi
mit
  • 11,083
  • 11
  • 50
  • 74
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142