I have a very simple Bash Script. The goal of this bash script is to take in a parameter of a port number, and kill all processes running on that port.
#!/bin/bash
PID_NUMBER=$(netstat -a -n -b -o | grep $1 | sed -e "s/[[:space:]]\+/ /g" | cut -d ' ' -f5)
echo "$PID_NUMBER"
taskkill /F /PID "$PID_NUMBER"
However, running the bash script in Cygwin echoes the PID_NUMBER, correctly, but throws an "ERROR: Invalid query" for taskkill
Output:
$ ./KillProcessesAtPort.sh 546
1540
ERROR: Invalid query
If I run "taskkill /F /PID 1540" in Cygwin, the process is sucessfully terminated.
$ taskkill /F /PID 1540
SUCCESS: The process with PID 1540 has been terminated.
I am running this script in Windows 10 on Cygwin terminal. Why does my script throw an ERROR for taskkill whereas just executing taskkill in Cygwin works?
taskkill /F /PID $PID_NUMBER
with no quotes?echo "$PID_NUMBER"
? I encounter problems when the line is repeated more than once.