schedule running a bash shell script in windows

2020-01-29 19:08发布

问题:

Apreciate any help and excuse me if my terminology is incorrect.

this is a script(*.sh file) that:
1-goes to a specific dir A
2-copies files from another dir B to dir A
3-#comented out# it also unzips the files in dir A and its subdirectories
4-#comented out# it also removes rows 1-6 and the last row of all *.csv files in dir A

#!/bin/bash

# Configure bash so the script will exit if a command fails.
set -e 

#cd to the dir you want to copy to:
cd /cygdrive/c/path/I/want/to/copy/to
#echo Hello

#cp the files i want 
#include the subdirectories 
cp -r /cygdrive/c/path/I/want/to/copy/from/* .

# This will unzip all .zip files in all subdirectories under this one.
# -o is required to overwrite everything that is in there
#find -iname '*.zip' -execdir unzip -o {} \;

#find ./ -iname '*.csv' -exec sed -i '1,6d;$ d' '{}' ';'

Now I can get this script to work in cygwin by going to the dir where the file is stored and giving the following commands:

./filename.sh

or

/cygdrive/c/path/where/the/file/is/filename.sh

or

bash filename.sh



I can also do this in CMD/Windows DOS by doing the following:

C:\cygwin\bin\bash.exe -l 

to get into a bash terminal and then give the following command:

/cygdrive/c/path/where/the/file/is/filename.sh

In task scheduler(in Windows) I have tried to schedule the following:

C:\cygwin\bin\bash.exe -l /cygdrive/c/path/where/the/file/is/filename.sh

but this does not work, even though the seperate commands work in CMD/Windows DOS as I have said above

Now what I want to do is be able to schedule this script(filename.sh) like I would a .vbs or .bat file in windows using task scheduler? Can anyone advise on this?

Note I have tried to write a Windows batch file(.bat) to do this(see below), but I could not get my unzip and sed commands to work,see here. So I have tried to write the Bash shell script above.

chdir C:\pointA
C:\cygwin\bin\cp.exe /cygdrive/v/pointB/* .
::find -iname *.zip -execdir unzip {} \;
::find ./ -iname '*.csv' -exec sed -i '1,6d;$ d' '{}' ';' 

回答1:

A solution is to associate .sh files with a batch file that runs bash. That way whenever you tell windows to execute an sh file it'll use the correct launcher - whether that's via a double click or a scheduled task. Here's mine:

@echo off

d:
chdir d:\cygwin\bin

bash --login %*

Associating a file type means that when you try to execute a file of that type, windows will use the path to that file as an argument passed to the program you've specified. For example, I have LibreOffice4 associated with .ods files. So if I doubleclick a .ods file, or just enter the path to a .ods file at the command prompt, windows will run open office calc, with the first parameter being the ods file. So if I have Untitled.ods on my desktop. I doubleclick it. That's effectively the same as opening up command prompt, typing

D:\Program Files (x86)\LibreOffice 4\program\scalc.exe" "C:\Users\Adam\Desktop\Untitled.ods".

and hitting enter. Indeed, if I do it, the expected happens: open office calc starts up and loads the file.
You can see how this works if you change the association to echo.exe (which I found in D:\cygwin\bin). If I change the association to echo, open up the command prompt and type

"C:\Users\Adam\Desktop\Untitled.ods"

I'll just see echo.exe echo the filename back to me.

So what I'm suggesting you do is this:

  1. create a batch file to run bash scripts using cygwin's bash (or use mine).
  2. change the association for .sh files to that batch file
  3. execute those .sh files directly, as though they were .exe or .bat files.


回答2:

why not creating a batchfile (.bat) that loads your cygwin bashscript and schedule this batchfile? like this you dont have to deal with the way M$ handles paramters