I made the following script:
# !/bin/bash
# OUTPUT-COLORING
red='\e[0;31m'
green='\e[0;32m'
NC='\e[0m' # No Color
# FUNCTIONS
# directoryExists - Does the directory exist?
function directoryExists {
cd $1
if [ $? = 0 ]
then
echo -e "${green}$1${NC}"
else
echo -e "${red}$1${NC}"
fi
}
# EXE
directoryExists "~/foobar"
directoryExists "/www/html/drupal"
The script works, but beside my echoes, there is also the output when
cd $1
fails on execution.
testscripts//test_labo3: line 11: cd: ~/foobar: No such file or directory
Is it possible to catch this?
test -d /path/to/directory
( or[[ -d /path/to/directory ]]
in bash ) will tell you whether a given target is a directory or not, and it will do it quietly. – Patrick Oct 22 '13 at 12:36cd
into it. – Stephane Chazelas Oct 22 '13 at 12:54directoryExists
. – Patrick Oct 22 '13 at 13:57