schedule running a bash shell script in windows (q

2019-09-19 10:59发布

问题:

Apreciate any help and excuse me if my terminology is incorrect.
building on from this question here:

i am trying to run a .sh file, and I do this by using the answer in the question above, which is basically
1 - associating my file filename.sh with the filename.bat
filename.bat looks like this:

@echo off

c:
chdir c:\cygwin\bin

bash --login %*

This generally works for most of my *.sh files with an example being this:

#!/bin/bash

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

#this is the DIR i want to copy to 
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#this is the DIR i want to copy from
DIR2=/cygdrive/v/where/file/is

#cd to the dir where I want to copy the files to:
cd "$DIR"

#cp files and subdirectories of interest
#include the subdirs 
cp -r "$DIR2"/* .

# 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 {} \;

# this will delete row 1-6 and the last row of all the csv files 
find ./ -iname '*.csv' -exec sed -i '1,6d;$ d' '{}' ';'

So the above works when I double click on it.

Some instances I have noted that do not work when I double click on them, is when the script contains something like below:
1->

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/PointB
    echo $DIR > test.txt #the text.txt file will not be created


2->

#this renames files in a directory, see [here][2]
for fname in Combined*.csv ; do mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" ; done

But 1 & 2 above will work if i run them from cygwin with the command ./filename.sh

What I am wondering is why is 1 and 2 above not work for me, by double clicking on them, when they do work by invoking the command ./filename.sh?

Note: I am running cygwin on windows XP

EDIT: This is what I get when I say it does not work by double clicking it in that the cmd window opens up and displays this.

FormatCSVFiles.sh is the file I am double clicking on.

回答1:

Correction: this actually works. I was not changing directory before giving the echo command. So a silly mistake by me . tks.