tags:

views:

1532

answers:

4
#!/bin/bash
echo "Testing"
cd "/cygdrive/x/Internal Communications/Riccardo/"
filename=htdocs-`date +%A`.tar.gz
tar cvzf $filename "/cygdrive/c/Program Files/Zend/Apache2/htdocs"

The above script is working when it is called inside cygwin console, but when I try to call it from a bacth file is get command not found for date and tar command. I think that bash.exe does not have the PATH set up.

I need to run that script from that bath file because I want to add the script to the task scheduler.

Thanks.

+2  A: 

Put your cygwin bin directory (likely C:\cygwin\bin) on your PATH environment variable.

This would also give you the benefit of being able to use commands like tar, ls, rm, etc. from a regular console windows and not just a Cygwin console.

matt b
Why I didn't think to add cygwin to my win PATH!!! Thanks.
rtacconi
+3  A: 

FWIW, Cygwin has cron.

Are you calling your script like this?

bash --login -i ./myscript.sh
seth
Of the people who have posted answers so far, seth is the only person who recognizes that putting C:\cygwin\bin on the path doesn't allow Windows to magically understand bash scripts. You need to send your script to bash.exe.
rob
The batch is: C:\cygwin\bin\bash.exe "C:\backup.sh"I think I have not installed cron in cygwin. Do I need to install it only or do I have to do some more config to use cron? Thanks mate!
rtacconi
@rtacconi - it's more than just installing. The link I have in my answer goes through the steps of installing/configuring cron
seth
+1  A: 

If this script is invoked from a Windows command shell, the first line will result in an error since #!/bin/bash is not a recognized Windows command and # is not a valid comment delimiter in a batch file.

So, the bottom line is that this script runs as a regular batch file rather than from within Cygwin's bash. As noted by matt b, you likely do not have the Cygwin executable path in your PATH environment variable. Without this, the batch file cannot find the Cygwin utilities (tar and date).

Tim Henigan
+2  A: 

As has already been said, you need to add the Cygwin binaries to your path. To do so, right click on "My Computer", click "Properties", then "Advanced", then "Environment Variables".

Create a new environment variable with name "CYGWIN_HOME" and value "C:\cygwin" (or wherever you installed cygwin. The default location is "C:\cygwin\" so this should probably work for you).

Then edit the environment variable named "PATH", and tack on the following to the end:

;%CYGWIN_HOME%\bin;%CYGWIN_HOME%\sbin;%CYGWIN_HOME%\usr\bin;%CYGWIN_HOME%\usr\sbin;%CYGWIN_HOME%\usr\local\bin;%CYGWIN_HOME%\usr\local\sbin

Close your command prompt, then reopen it. The cygwin binaries should now be available. You can double-check this by typing "which bash". It should report the location of your bash executable.

Michael Aaron Safyan