4 Replies Latest reply on Feb 12, 2013 10:45 AM by Bill Robinson

    Error running a for loop in a nexec command on a Windows

    JeanMichel GUY

      Hello,

       

      I would like to run some windows commands through a nexec session and I get the following error message :

      2 was unexpected at this time.

       

      This is my command :

       

      % nexec XYZ cmd /c "for /f "tokens=2" %a in ('echo Paris London') do echo %a"
      2 was unexpected at this time.
      
      % nexec XYZ cmd /c "for /f \"tokens=2\" %a in ('echo Paris London') do echo %a"
      2 was unexpected at this time.
      
      
      

       

      How can I display the second parameter of the echo command ?

       

      Thanks for you help.

       

      Best regards,

       

      Jean-Michel GUY

        • 1. Re: Error running a for loop in an nexec command on a Windows
          suresh Balla

          when we run a command like below we get output on windows machine.

          cmd /c "for /f "tokens=2" %a in ('echo Paris London') do echo %a"

           

          here we have double quotes inside double quotes.  windows accept this. but on nsh or Unix flaour machines this will be treated in different. this will be split into the following

          cmd /c

          for /f

          tokens=2

          %a in ('echo Paris London') do echo %a

           

          when a same part are rejoined we will get the following

          cmd /c for /f tokens=2 %a in ('echo Paris London') do echo %a

          if we ran the same on windows box we get error:

          2 was unexpected at this time.

           

          if escape double quotes we should get proper result.

          from agent logs i am ble to see proper command is fired but getting incorrect result.

           

          this can be nsh issue.

           

          workaround: a small script can be written

           

          echo "for /f \"tokens=2\" %a in ('echo Paris London') do echo %a" > "//XYZ/PATH/test.bat"

          nexec XYZ cmd /c PATH/test.bat

          rm "//XYZ/PATH/test.bat"

           

           

           

           


          • 2. Re: Error running a for loop in an nexec command on a Windows
            Bill Robinson

            why not run the for loop in nsh ?

            for i in Paris London

            do

            nexec <command>

            done

             

             

            ?

            • 3. Re: Error running a for loop in an nexec command on a Windows
              JeanMichel GUY

              My real aim is to run a command on a Windows machine in order to collect the result in an Extended Object. So my first step is to run this command through nexec. So the echo Paris London is only an example to know how can be run a for loop using nexec in order to extract the 4th field.

               

              % nexec -i XYZ cmd /c "for /f "tokens=4" %a in ('c:\BLACL1\NSH\bin\version.exe ^| findstr Shell') do echo NETWORK_SHELL=%a"
              4 was unexpected at this time.
              

               

              Thanks.

              • 4. Re: Error running a for loop in an nexec command on a Windows
                Bill Robinson

                it's going to be diffcult to pass in the command you want due to the double quotes and how nsh handles them vs cmd.  it would be eaiser to do your loop in nsh and make a call to nexec only when you need to actually run something on the target. or you could echo your cmd bit into a batch file and run that.

                 

                i find the loops and other structures in nsh/shell much easier to work w/ than cmd.

                1 of 1 people found this helpful