Double Quotes with sh -c

Hi,

I am using the following command to create a log file.

echo "`date` Starting the workflow"   >>  MYLOG_`date '+%d%m%Y'`.log

My application (Informatica) takes the above command and issues the following to the UNIX server.

sh -c "echo "`date` Starting the workflow"   >>  MYLOG_`date '+%d%m%Y'`.log"

But the problem is, sometimes The above command fails on UNIX server Since i used double Quotes in echo command.

Then if i remove double Quotes from echo command then Informatica issues the following command and it executes fine.

sh -c "echo `date` Starting the workflow   >>  MYLOG_`date '+%d%m%Y'`.log"

Can somebody tell me what is the coding standard when using UNIX commands with sh -c ?

What exactly sh -c means ?

I searched online but not able to find sufficient information.

Thanks in advance :b:

The -c option tells the shell to read the commands from the given string: In your case:

sh -c "echo "`date` Starting the workflow" >> MYLOG_`date '+%d%m%Y'`.log"

The problem is, sh thinks "echo " is that string. When I run your command, I get:

$ sh -c "echo "`date` Starting the workflow" >> MYLOG_`date '+%d%m%Y'`.log"   
Mon

If you escape the inside double quotes, it should fix your problem:

$ sh -c "echo \"`date` Starting the workflow\" >> MYLOG_`date '+%d%m%Y'`.log"

$ ls -lrt MYLOG_*
-rw-r--r-- 1 jsmith jsmith 153 2010-01-11 04:59 MYLOG_11012010.log

Read the man page "man sh" regarding the -c parameter for more information.