Search code examples
bashshellcygwinipconfig

Shell script to get IPV4 address dynamically and add to route table using cygwin console for windows


I am writing a shell script which runs on cygwin console for windows, my scritp looks like this

#!/usr/bin/bash

cmd /c start /d "C:\cygwin\bin" ipconfig | findstr /R /C:"IPv4 Address"
ipconfig #to print 
route add 10.2.139.1 192.168.104.1

But when i execute this script on cygwin console it shows below error and even i changed to ipconfig \all it doesn't work

Error: unrecognized or incomplete command line.

USAGE:
    ipconfig [/allcompartments] [/? | /all |

i am trying to get ip address dynamically by executing the script and adding to the route table

Thanks, Rohith


Solution

  • I don't know why you did this with cygwin instead of cmd.exe, what you use it for and why use start, but, all you missing is one option /b:

    cmd /c start /d "C:\cygwin\bin" /b ipconfig | findstr /R /C:"IPv4 Address" 
    

    And start is redudant as follows:

    cmd /c ipconfig | findstr /R /C:"IPv4 Address"
    

    /b just suppresses the newly created cmd.exe background window.