Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple string comparison in bash

Tags:

bash

I'm looking to get the number of subdirectories within a directory and compare that with a predefined number.

Ex:

cd /Applications/
x=echo ls -d */ | wc -l | sed 's/^ *//g'
y="52"

if [ "$x" == "$y" ]; then
    echo "equal"
else
    echo "not equal"
fi

It will give me a number for x (52 in my case), but will always say "not equal". What am I doing wrong? Any help would be greatly appreciated.

Thanks

like image 386
user3089044 Avatar asked Aug 07 '26 04:08

user3089044


1 Answers

For bash, do this:

cd /Applications/
dirs=( */ )   # dirs is an array with the names of directories here
if (( ${#dirs[@]} == $y )); then
    echo "equal"
else
    echo "not equal"
fi
like image 129
glenn jackman Avatar answered Aug 09 '26 01:08

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!