I can across some code in a bash script that has confused me a little.
The code checks to see if the directory/file exists. If the return code is 0 ... success, otherwise the file doesn't exist:
if [ -d /usr/local/bin ]
then
echo "Exists"
else
echo "Does not exist"
fi
What program is -d /usr/local/bin
using to check if the file exists? The other example in the book is:
test -d /usr/local/bin
if [ "$?" -eq 0 ]
then
echo "Exists"
else
echo "Does not exist"
fi
Which leads me to believe the first example us using test
within the if
. And if so, why does this happen automatically without having to specify the test
program?