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

11,516

Solution 1

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).

Solution 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

Solution 3

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.

Share:
11,516

Related videos on Youtube

endlesslearning
Author by

endlesslearning

Updated on September 18, 2022

Comments

  • endlesslearning
    endlesslearning over 1 year

    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.