Announcement

Collapse
No announcement yet.

rsh batch

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • rsh batch

    I'm trying to script an rsh command, but have it only echo a certain line/string if the "search string" is found. If I run the command alone outside of a batch, then it runs fine.

    I can't quite figure out what I'm doing wrong here...

    Code:
    @echo off & setlocal & Title NetApp Sysconfig-R
    
    SET "hostname=10.1.10.103"
    SET "command=sysconfig -r"
    SET /P "searchstring=Please enter a string to search: "
    
    IF "%searchstring%"=="" goto :error
    
    For /F %%G in (
       'rsh %hostname% -n "%command%" | find /I "%searchstring%"'
    ) DO SET percent="%%G:~-15%"
    
    echo %percent%
    pause
    
    
    :error
    echo No search string was entered!
    goto:eof

  • #2
    Re: rsh batch

    Originally posted by ekrengel View Post
    I'm trying to script an rsh command, but have it only echo a certain line/string if the "search string" is found. If I run the command alone outside of a batch, then it runs fine.

    I can't quite figure out what I'm doing wrong here...

    Within the for-do set the "pipe"-sign needs to be escaped.

    This will not work: SET percent="%%G:~-15%" you'll have to accomplish this with two steps.

    Code:
    @echo off & setlocal & Title NetApp Sysconfig-R
    
    SET "hostname=10.1.10.103"
    SET "rmcmd=sysconfig -r"
    
    SET /P "searchstring=Please enter a string to search: "
    
    CALL:TRIM searchstring
    IF "%searchstring%"=="" goto :error
    
    For /F %%G in (
       'RSH.exe %hostname% -n "%rmcmd%" ^| find.exe /I "%searchstring%"'
    ) DO SET "percent=%%G"
    SET percent="%percent:~-15%"
    
    echo\%percent%
    pause & goto:eof
    
    :error
    echo No search string was entered!
    pause & goto:eof
    
    goto:eof  - subroutines -
    :TRIM
    Set "$variable$=%~1"
    Call:GOTRIM %%%~1%% & exit /b 0
    :GOTRIM
    Set "%$variable$%=%*"
    exit /b 0
    \Rems
    Last edited by Rems; 21st April 2010, 23:23.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: rsh batch

      Ah ok thanks. I'll remember those tips for next time...

      Can you explain what the "trim" subroutines are actually doing? I know the "%~1" will expand the search string removing any surrounding quote...but what about the rest? Mainly these two lines:

      Parts I don't understand in red...
      Code:
      :TRIM
      Set "$variable$=%~1"
      Call:GOTRIM %%%~1%% & exit /b 0
      :GOTRIM
      Set "%$variable$%=%*"
      exit /b 0

      Comment


      • #4
        Re: rsh batch

        Originally posted by ekrengel View Post
        I know the "%~1" will expand the search string removing any surrounding quote...but what about the rest?
        Within a batch it is possible to call a ":label", similar to calling another "batch file".
        You can also provide command line arguments when calling a label.
        In the sample above the labels called are :TRIM and :GOTRIM

        These Labels contain subroutines (which I've written in a way that it can be used as a function that is also usable for other batches). End each subroutine with goto :eof or exit /b .

        In the sample, the word "searchstring" is provided as command line argument. The "word" is equal to the name of a variable set earlier.
        In the subroutine that word is set as the value of a new variable (it does not expand here the value of the variable that was set earlier!). In the sample %%%~1%% (what would be the same as %%%$variable$%%%) actually expand the value.

        the inner %-sign(s) (from %~1 or, %$variable$%) will expand the name of the variable, then the outer %-sign make expand the value of that variable. The outer %-signs are dubbled (doubling is the way how to escape a percentage sign).
        For a normal command to be able to double expand you should also use CALL.

        Here's basically what happen
        Code:
        SET searchstring=MyString
        
        
        SET $variable$=searchstring
        SET $value$=%searchstring%
        Call ECHO "%%%$variable$%%%" is equal to "%$value$%"
        
        :: AND,
        Call SET %$variable$%=%%%$variable$%%%
        ECHO The name of the variable is '%$variable$%' and the value is "%searchstring%"
        Finally, the fully expanded search string is on the command line that is calling the second routine at :GOTRIM. Here %* captures all arguments at once (not just one argument like %1 and %2 do), this actually make any existing leading and trailing spaces disapear.



        Here are samples of LeftTrim, RightTrim and Trim functions for batches,
        Code:
        goto :subroutines begin
        
        :LTRIM
           Set "$variable$=%~1"
           :GOLTRIM
           call Set "$value$=%%%$variable$%%%"
           If "%$value$:~0,1%"==" " (
           Set "%$variable$%=%$value$:~1%" 
           Goto:GOLTRIM)
           exit /b
        
        :RTRIM
           Set "$variable$=%~1"
           :GORTRIM
           call Set "$value$=%%%$variable$%%%"
           If "%$value$:~-1%"==" " ( 
           Set "%$variable$%=%$value$:~0,-1%" 
           Goto:RTrim)
           exit /b
        
        :TRIM
           Set "$variable$=%~1"
           Call:GOTRIM %%%~1%% & exit /b 0
           :GOTRIM
           Set "%$variable$%=%*"
           exit /b 0
        
        :subroutines End
        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: rsh batch

          Very cool! Thanks this helps a lot!

          I will never be confused by % signs again

          Comment

          Working...
          X