Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count files in directory and place count into Variable

Status
Not open for further replies.

nicatt

Programmer
Apr 2, 2001
43
US
I have the following code:

ls *.bat | wc -l

What this does is to count the number of files in the home directory that have the 'bat' extension. I want to run an IF statement off the result. I want to place the results (in this case 5) into the variable but can't quite accomplish it. I can pipe or redirect it to a file and read the file but that seems a bit much.

I want to essentially accomplish the following:

countvariable="ls *.bat | wc -l"

if
$countvariable -ge 2
then
do stuff
else
do other stuff
elif

Ideas? Thanks in advance!
 
#!/bin/sh

variable=`ls *.bat | wc -l`

echo $variable

after this you can do your 'IF' condition..
 
if [ "`ls -1 *.bat | wc -l`" -ge 2 ] ; then
do stuff
else
do other stuff
fi; vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top