-1

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 Bash equivalent. The code is here:

for f "tokens=2 delims=#" %%a in $step; do set devnum=%%a
set $devnum=$devnum:)=%

I understand that it's looping through the variable $step I just don't know how to do the tokens and delimiters in Bash. Any help would be greatly appreciated.

Biffen
  • 6,249
  • 6
  • 28
  • 36

1 Answers1

0

In DOS the for loop syntax is FOR %A IN (list) DO command [ parameters ] (%%A is used in a batch-file compared to %A used on the command line). In bash the syntax is for var in list; do command; done In your case you have:

for f "tokens=2 delims=#" %%a in $step; do set devnum=%%a set $devnum=$devnum:)=%

The loop provides some function or executable f using "tokens=2 delims=#" for the variable %%a where %%a will take on the values in $step each time the loop executes setting devnum=%%a (the present value from $step and setting $devnum=$devnum:)= (whatever the modification to the initial devnum value).

The translation to a bash for loop will not only require the translation of the for loop syntax, but will also require translation of the $step list and the translation of the commands. The for loop (without list and command translation) becomes:

for $a in (whatever list $step is); do
    tmpvar=$(f "tokens=2 delims=#" $a)  # the translated f will be done inside the loop
    devnum=$tmpvar
    (whatever $devnum=$devnum:)=% translates to)
done

Nobody can tell you what $step translates to until you show what $step is. The same goes for the translation of f. That being said, the generic translation of the loop structure will be as shown above unless f is something other than a function or executable. I hope that helps. Drop an additional comment or edit your question to provide more information and I'm glad to help further.

Edit per comment regarding /f filenameset

That makes more sense. In that case you will use an embedded while read ln; do ..stuff.. done loop to process the files and break each line into tokens:

for $a in (whatever list $step is); do
    while IFS= read line || [ -n "$line" ] ; do
        (parse $line as needed to get 2 tokens)
        devnum=(some $tmpvar from the token parsing) # these two line may move outside the
        (whatever $devnum=$devnum:)=% translates to) # while loop, depending on the tokens
    done <"$a"
done

Since with further information it appears $step holds filenames to be read line-by-line and processed into 2 tokens separated with # %%a will become $a holding a filename from $step which will be fed into the while read loop at the closing done <"$a". IFS= unset IFS (but you may use it to break on # as well, but I can't tell without seeing your input) the statement read line || [ -n "$line" ] will read each line in the filename provided by $a and pass $line to the inside of the while loop to be processed as required into tokens. You will most likely use parameter expansion/substring extraction to get the tokens you need from $line. Once you have your tokens, you can assign devnum as required.

If you will provide the file contents and example of the tokens you need, I'm happy to show you how to extract the tokens with substring extraction (and revisit the IFS setting, if needed).

Note: if $step is a list of files as it appears to be, then there is no need for a for loop at all. All processing can be done with the while loop in bash. It is difficult to know exactly what you are attempting to do without you providing examples of what is included in $step along with the contents of the files that will be parsed into the tokens.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • OK, I have a typo. It should read: `for /f "tokens=2 delims=#" %%a in $step; do set devnum=%%a` so that it loops through the list contained in $step using the token and the delimiter and assigning them to %%a. Thats what I haven't been able to figure out in bash. How to do the loop with the params above. – sooperkool Aug 23 '14 at 03:45
  • I have updated the answer based on your edit regarding ` /f filenameset`. Take a look. If you provide the additional file/token information, I'm happy to show you how to translate the token extraction as well. – David C. Rankin Aug 23 '14 at 16:29