Home > other >  Converting a for loop from Batch to Git Bash [closed]
Converting a for loop from Batch to Git Bash [closed]

Time:09-17

I've been stuck on a particular for loop in a Windows script and I can't find any help on how to translate it to its git Bash equivalent. The code is here:

FOR /F "usebackq tokens=2" %%i IN (`tasklist /v ^| findstr /c:"IRCam_Data_Collector"`) DO taskkill /pid %%i

if I change it to the standard shell, it gives out an error

syntax error near unexpected token `"tokens=2 delims=,"'

my version of converted shell code:

for //f "tokens=2 delims=," %i in (`tasklist //v | findstr //c:"IRCam_Data_Collector"`); do taskkill //pid %i; done

Any help would be greatly appreciated. Yes, I know that Git Bash is different from bash. I figured out that it has to do something with the exploded variable iteration "For /f". Since I have no experience with Git Bash, thus I asked for the help

CodePudding user response:

This should achieve what you expected :

#!/usr/bin/env bash

for pid in $(tasklist //v | awk '/IRCam_Data_Collector/{print $2}'); do
    echo taskkill //F //pid $pid
done

Remove echo once you see it does what you wanted.

  • Related