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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple scripting problem - convert `wc -l` to an int

Status
Not open for further replies.

redsevens

IS-IT--Management
Aug 11, 2000
40
0
0
US
I'm trying to run a script that will do one of two things, depending on whether or not a directory is empty. The script so far is as follows:
Code:
#!/bin/bash
num_sii=`ls /home/sii/incoming/ | wc -l`;
if [$num_sii -eq 0]
then
        echo "There aren't any new files.\n"
else
        echo "There are $num_sii new responses."
        ...
fi
The problem is that $num_sii is a string with leading spaces, and I need to convert that to an integer. This is probably very simple, but I don't know how to do it. Any help would be appreciated!

-Joe
 
Or:
Code:
num_sii=$(expr $(ls /home/sii/incoming | wc -l))
if [ $num_sii -eq 0 ]
then
    echo "There aren't any new files.\n"
else
    echo "There are $num_sii new responses."
    ...
fi
Cheers, Neil :cool:
 
Beautiful! I knew it would be something simple. Thanks!

-Joe
 
Hi Red,

#/bin/csh
num_sii=`ls /home/sii/incoming/ | wc -l`;
if [$#num_sii != "0"]

Now It will check wheather it is 0 or >.

Patel
 
:)

Or ...
Code:
#!/bin/bash
typeset -i num_sii=`ls /home/sii/incoming/ | wc -l`;
if [ $num_sii -eq 0 ]
then
        echo "There aren't any new files.\n"
else
        echo "There are $num_sii new responses."
        ...
fi
Greg.
 
The best way i think is this

#!/bin/bash
num_sii=`ls /home/sii/incoming/ | wc -l | tr -d ' '`;
if [$num_sii -eq 0]
then
echo "There aren't any new files.\n"
else
echo "There are $num_sii new responses."
...
fi



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top