1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to? Put logic into an alias...

Discussion in 'Support' started by mathewsdw, Jun 14, 2013.

  1. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    I'm keeping this simple to start. Is there any way to do "If(somecondition,dosomething,dosomethingelse)" in an alias?
     
  2. Frank

    Joined:
    Aug 2, 2011
    Messages:
    258
    Likes Received:
    4
    Hi Mathew,

    Code:
    (system)  C:\TEMP >%@if[%_dow eq Thu,echo have a nice weekend,echo day of week is: %_dow]
    day of week is: Fri
     
    (system)  C:\TEMP >%@if[%_dow eq Fri,echo have a nice weekend,echo day of week is: %_dow]
    have a nice weekend
    
     
  3. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    Frank, I must have done something wrong (I'm half blind), but @If didn't work for me. So extending it a little bit: can you code the "true" and "false" clauses as a sequence of commands?
     
  4. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    I have many aliases structured thusly:
    alias reciprocal=`iff %1 NE 0 then %+ echo %@eval[ 1 / %1] %+ else %+ echo 0 has no reciprocal %+ endiff`

    The advantage over @IF is that only the desired branch is executed, the "false" branch can contain expressions causing failure.
     
  5. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    Thank you Steve; somehow I had the idea that IFF couldn't be used in an alias the way you are using it. I obviously should have tried it before making that assumption.
     
  6. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,971
    Likes Received:
    30
    Code:
    v:\> alias zz `%@if[%q==1,echo foo,echo bar]`
     
    v:\> set q=1
     
    v:\> zz
    foo
     
    v:\> set q=2
     
    v:\> zz
    bar
     
  7. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,971
    Likes Received:
    30
    Code:
    v:\> alias zz `if %q==1 (echo foo) else (echo bar)`
     
    v:\> set q=1
     
    v:\> zz
    foo
     
    v:\> set q=2
     
    v:\> zz
    bar
     

Share This Page