Domanda

I have a simple bash script that throw errors on a Windows machine in the Cygwin xterm terminal when I call it like so: ./myscript.bat. It runs fine when I call it like this: /cygdrive/c/cygwin/bin/bash.exe myscript.bat. I am thinking that my shell is not using bash by default. How can I set it to bash so that the next time I open the shell, I can execute my script using ./myscript.bat?

È stato utile?

Soluzione

When you execute a file, Windows (or some component within Windows) decides how to execute it based on the extension part of the file name.

Cygwin inherits this functionality, letting you run Windows commands from within Cygwin. Cygwin also implements most of the usual UNIX functionality (running commands based on their content), but the combination of UNIX and Windows semantics can't always be perfectly clean.

The .bat suffix refers to a Windows batch file, so when you try to execute myscript.bat, the system treats it that way rather than as a bash script.

Change the file name from myscript.bat to myscript.bash or myscript.sh -- or just drop the extension altogether (since someone running your script shouldn't need to care how it's written).

There are several other filename extensions you should avoid (like .cmd), depending on how Windows is configured. A few quick experiments show that a .sh extension is safe, but really you don't need to use an extension at all for a shell script.

And, as R Sahu's answer says, you also need to make sure the script has execute permission if you haven't already done so:

mv myscript.bat myscript
chmod +x myscript

Altri suggerimenti

You'll probably need to change permissions of the file to make it an executable.

Try

chmod +x myscript.bat
./myscript.bat
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top