How do I get Linux commands and scripts to work on Cygwin?

3

I'm back with a follow up question. How do I get Linux commands to work on Cygwin? First I asked you what tool should I use to help me run my Linux scripts on Windows and you suggested Cygwin and I installed it plus some packages. Now I am trying some commands which I use in the Linux environment like the iostat, free and ifconfig but they won't work. So my questions are:

  1. Why won't some commands work?
  2. Where should my scripts reside? And should they have an extension of .bat or should I let them keep their extension .sh?

Thanks guys! I really hope you could help me with this one.

endlesslearning

Posted 2014-02-26T16:25:13.633

Reputation: 51

Answers

3

Let me clarify terminology first.

  • Linux = operating system kernel
  • Linux distribution = Linux kernel + user-space utilities (bash, ifconfig etc.)
  • Windows = NT kernel + user-space utilities
  • Cygwin = user-space utilities

Because of fundamental differences between NT and Linux kernels you cannot count with your scripts working unmodified on Windows. Some things, which are less specific for OS kernel, work mostly the same way, and Bash is one of them. On the other hand, ifconfig is quite OS-specific and is a part of net-tools project, which hasn't been updated since 2001 and is deprecated since 2004. I believe, there is a cygwin version of ifconfig, and even though it most likely would not work to update configuration, it should work fine to print it. Bryan C. has pointed out how to make sure it is installed.

As for your second question, extension doesn't matter as long as you start your scripts from Bash (it relies on execute permission and shebang line). However, if you want to start them from outside Bash, you'd better make them end with .sh (see this answer for details).

mkalkov

Posted 2014-02-26T16:25:13.633

Reputation: 366

2

Linux ifconfig translates (loosely) to the ipconfig command in windows. In a Cygwin-inside-Windows, you have access the full range of windows programs including any batch files and additional user added programs that are along the PATH=

Try "ipconfig.exe /all" which is the c:\windows\system32\ipconfig.exe program.
Which is actually at /cygdrive/c/Windows/System32/ipconfig.exe

Yes this is not ideal replacement for ifconfig

Until someone finds a true ifconfig for Cygwin.com try the following:
alias ifconfig=/cygdrive/c/Windows/System32/ipconfig.exe

Get your machine IP address:
ipconfig.exe -all | grep -i "IPv4 address" | head -n 1
you can sed/awk pretty up this to get just the IP address only

Ajax4Hire

Posted 2014-02-26T16:25:13.633

Reputation: 21

1

First off. Some Linux system level monitors are not present since you are running in an emulation layer you may not get as accurate a result as you would from the Windows native version of the command (where available).

Next, make sure you have the packages installed that include these commands. You can see search the Cygwin Package List for more info. In some cases you may need to compile the command from within Cygwin if pre-compiled builds are not available.

As for the second part of your question, if these are bash scripts, us the .sh extension. You can run them from anywhere, but remember that you'll need to be executing them from the Cygwin shell, and not from a Windows command prompt. Of course, you may also need to fix permissions to allow execution.

Bryan C.

Posted 2014-02-26T16:25:13.633

Reputation: 226

0

Even various linux versions aren't compatible with one another. For example, you could install a program on it with e.g. apt-get and one distribution of linux gets the program from one depository, another gets it from another depository, as it should, but there could be differences in the programs. A program like ssh might store configuration files in /etc/ssh or might store it in just /etc you have to find out on your linux system where it is.

And even one linux version depending on how it is configured, a script might not be compatible with both. For example, in Debian(and no doubt not just Debian), the 'mail' command runs one mail program, but can be configured to run another. Different mail programs have different parameters.

And then there's the question of what programs you have installed. If the script accesses a program that isn't installed then it won't work. This applies to cygwin or linux. And it could just be a tiny little program.

And in linux there are different shells. And also a shell could be configured in an unusual way, for example, does * include . and .. or not. Linux opens you to a sea of complexity you clearly have not even imagined.

As to your question of whether to use .bat or .sh well. If you're running in just plain cmd.exe no cygwin then the script would be .bat (though there are other scripts or programs though they may run as a parameter to another program acting as an interpreter) If you were running within cygwin the extension won't change anything.

You should think a bit about how much you want to and can know about a script before using it. If it's a one liner from a website then it's reasonable to know at least something about how it works. Don't run things completely blind. If it's a huge script which is part of a complex program you downloaded, then you might want to look at the script before you run it but you might be ok not understanding everything or much in it. Linux can distract you with complexity, but I wouldn't suggest avoiding the complexity. Asking if 'a' is compatible with 'b' is such a general question you might get a more useful answer if when you're faced with a specific case, make sure you're doing it the right way for your distro or for cygwin. The readme or website you're getting it from or the installation file should say. Each thing can have its own way of doing things.

barlop

Posted 2014-02-26T16:25:13.633

Reputation: 18 677