vi and if statements

Hi

I am very new to Unix programming and shell scripting. I am trying t figure out how to write a little script that will output the number of directories. I can find the number of directories using ls -l | grep "^d" | wc -l I can not figure out how to do it so when I type the name of the file it will show the number of directories. I am using #!/bin/sh shell.

Thanks in advance, Red

You mean you want a script:

#!/bin/bash
find $1 -type d | wc -l | read var
echo "Directory count: $var"

Put that in a file called fdir.sh, with an editor. Then:

chmod +x fdir.sh

To use it:

./fdir.sh /usr

where /usr is a directory. I picked /usr because all unix systems have this directory name. You select any valid directory name.

If you enter a directory that is outside your current directory, use a full path:
/var/log when you want the log directory under /var.

This script will count the current directory, the hidden directories and all subdirectories at any level too which is probably not be what the OP asks for. Moreover, while it would work in ksh, it can't in bash due to a (poor IMHO) bash implementation choice. The var variable setting is lost when trying to display its value.
A more portable way would be:

#!/bin/bash # or ksh, dash, ...
var=$(find $1 -type d | wc -l) 
echo "Directory count: $var"

That said, I have frankly no idea about what the open poster is asking for and especially how the thread title "vi and if statement" can relate to printing a number of directories ...

Hi and thanks for the reply's. I am using vi editor and trying to create a script using if statements. I will see what I can do with the suggestions.

Thanks, Red

---------- Post updated at 02:15 PM ---------- Previous update was at 12:05 PM ----------

Hi
Tried different combination's. One of the things I tried is;

#!/bin/sh

var=ls -l | grep "^d" | wc -l
if [ $1 = o ]
then
    echo $var
fi

When I try to run it, I get an error [: 9: =: unexpected operator

I have 9 directories when I run ls -l and that is what I am try to get it to show when I run the program.

Stumped
Thanks, Red

---------- Post updated at 02:28 PM ---------- Previous update was at 02:15 PM ----------

I found that I should be posting this in a different forum.

Sorry, Red

var=$(ls -l | grep "^d" | wc -l)

or

var=`ls -l | grep "^d" | wc -l`

...

#!/bin/sh

var=$(ls -l | grep "^d" | wc -l)
if [ "$1" = "o" ]
then
echo "$var"
fi

Hi

I tried using code from bottom code box. When I executed code, I got and error 3: syntax error "(" unexpected. I tried using back ticks, no errors but did not have any output. I also tried using # instead of 1 in if statement.

Thanks, Red

Make sure you run it from the directory you want to ls -l and make sure you run it with it's argument o

The OP is reporting a syntax error on line 3, probably his shell is not Posix compliant and only supports backticks. Most likely he is on Solaris and he would need to run /usr/xpg4/bin/sh

Hi
Found my solution.

#!/bin/sh
if [ $1 = -d ]
then 
     ls -l | grep "^d" | wc -l
     exit
fi

Type the name of the file and add -d will output the number of directories
.
Thanks, Red