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.
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 ...
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.
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