Web lists-archives.com

Re: Weird shell script behavior in a cron job




James H. H. Lampert:

Could it be that cron is running it an entirely different shell, that doesn't understand the if statement?

Despite what others have said, the answer to this question is no.  Whilst you are running two different shells, the problem is not the if statement.  Both of those shells understand that if statement.  The difference between the shells involves the fact that you have assumed a particular exit status for the cd command for non-existent directories.  That's the exit status that the cd command results in, in one of your shells.  But it is not the exit status that results in the other.

Ironically, you are using the [ command anyway, and that command has a direct method, its -d operator, for testing for the non-existence of a directory.  So you are going around the houses a bit in order to achieve what you could be achieving directly, and portably (without assumptions about exit statuses), with the [ command itself.