Check the exist status of the cd command

Hi,
As in scripting , some cd commands getting failed, so we do check the exist status as 0 or 1. But every time we call to function for it. But does any single line exist will do the job with || , && ? i.e

ls -l
Logs
cd Logss | exit
echo hi

as Logss is not exist , Before printing "hi" we have to exit with some friendly output i.e "directory is not exist"

Thanks

I'm not sure I understand your question, but you can use the control operators || and && with cd.

cd $somedirectory 2>/dev/null || echo "directory $somedirectory does not exist"; exit
echo hi
 sh test.sh
cd Temp > /dev/null || echo "direcotry not exist" ; exit
+ cd Temp
+ exit

For failure case it works fine but for true cases also exit is executing. I had tested as earlier in this case
Linux-x86_64 GNU/Linux

Try

cd $somedirectory 2>/dev/null || echo "directory $somedirectory does not exist" && exit

There were curly braces missing :

cd $somedirectory 2>/dev/null || { echo "directory $somedirectory does not exist"; exit; }
echo hi

EDIT: this is bash syntax

@cero
it works fine for me. Thanks. Actually i missed the exit ;(semicolon) so failed to getting the exact result. :slight_smile:
@jim
Already i quoted same thing in my script but not working for me. So trying multiple combination of the Logical AND , OR and Bitwise AND, OR condition to achieve it.