Menu

#377 Cannot suppress command shell pipe when using ECHO.

freedos13
closed
Jim Hall
None
5
2023-06-16
2023-05-15
Oliver
No

I have a batch file that has to compare if A is greater than or equal B.
I also want to have a nice Text, printing something like this:

Is A >= B?

Thus i have something like this in my batch file:

@echo Is %A% >= %B%?

And here's the problem. The >= is interpreted as a pipe.
Trying to suppress it with a @ before echo or putting the output string in all sorts of quotation marks like

@echo `Is %A% >= %B%?`
@echo 'Is %A% >= %B%?'
@echo 'Is %A% >= %B%?'
@echo "Is %A% >= %B%?"

or trying to escape the ">" char with a backslash or slash doesn't work:

@echo %A% />= %B%
@echo %A% \>= %B%

The command is always trying to pipe A into B.
A workaround like swapping the two expressions into:

B < A

Also doesn't work. It gives a similar error. This time the pipe is used in the other direction.

It would be nice if pipes are not used, when @ is used in front of echo.
Or as an alternative, if echo understands the use of quotation marks like the GNU echo counterpart.
Or is there a solution for this?

The only solution I can think of right now would be to write out the >= as "greater than or equal". But I would like to do without that and prefer to use the above mathematical expression.

Discussion

  • Jim Hall

    Jim Hall - 2023-05-15

    You have several "bugs" here:

    FreeDOS doesn't allow > or < in statements

    This is not a bug, as MS-DOS (and no other DOS) supports > or < to compare values. Even Microsoft Powershell (Windows) uses -lt for "less than" and -gt for "greater than."

    Using > or < in a REM statement causes redirection

    This is standard MS-DOS behavior too. The MS-DOS 5.0 user manual (see p. 550) says:

    Restrictions on batch-file comments
    You cannot use a redirection character (> or <) or pipe (|) in a batch-file comment.

    Neither of these are bugs, but expected behavior on DOS.

    The IF batch file conditional is documented to only support:

    if [NOT] expr command [command-options]
    

    where:

    NOT negates an expression

    And valid expressions include:

    ERRORLEVEL n to test if an error level (exit value) is equal to n

    s1==s2 to test if the string s1 is equal to the string s2

    EXIST d:path\file to test if the file exists at d:path\file

     
  • Shidel

    Shidel - 2023-05-16

    If you are using FreeDOS and do not need to stick to built-in shell commands, consider using VECHO. It is part of V8Power Tools and installed under FreeDOS 1.2 and later. V8Power Tools is what makes a a lot of magic happen. It is also what powers the user interface of FreeDOS Installer using batch files. VECHO is also what provides the colored text for the FreeDOS welcome message when booted.

    You can use it to do things like: vecho "Is %A% >= %B%?"
    Or, vecho Is %A% ">=" %B%?
    Or, add a little color like: vecho Is %A% /fGreen ">=" /fGray %B%?
    Or, use double quotes by wrapping them in single quotes: vecho ' ">" '
    Or, use single qoutes by wrapping them in double qoutes: vecho "Bob's house!"
    And, much much more. See vecho /?

     
    👍
    1

    Last edit: Shidel 2023-05-16
  • fritz.mueller

    fritz.mueller - 2023-05-15

    I am sure you find a reason why it is not identical with https://sourceforge.net/p/freedos/bugs/145/ .
    I must admit that I also do not understand its sense.

     
  • Jim Hall

    Jim Hall - 2023-05-15

    @ fritz : Thanks for the link to bug 145 with the same answer about redirection inside a REM statement

    If Oliver wants to implement "less than" or "greater than" tests in DOS, the standard way to do it is with a separate command line utility to do the test for you and return the result in the error level (exit code). For example, let's say you had a CMPR ("compare") command line tool like:

    CMPR n expr m
    

    where expr was something like:

    LT : n is less than m

    LE : n is less than or equal to m

    GT : n is greater than m

    GE : n is greater than or equal to m

    EQ : n is equal to m

    Then you could do something like:

    @echo off
    CMPR 1 LT 2
    if ERRORLEVEL 0 echo 1 is less than 2
    

    or:

    @echo off
    CMPR 3 GE 2
    if ERRORLEVEL 0 echo 3 is greater than or equal to 2
    

    .. but you would need to write the CMPR program to do that. There is nothing like this included in FreeDOS, because this need is so incredibly rare in batch programming.

     
  • Jim Hall

    Jim Hall - 2023-05-15

    If you need a CMPR program, I wrote a very simple implementation for you. You can find it at:
    https://www.ibiblio.org/pub/micro/pc-stuff/freedos/files/util/user/

    I will close this bug, since it is not really a bug.

     
  • Jim Hall

    Jim Hall - 2023-05-22

    The CMPR tool is a very simple "one-off" program that fit this specific need. I don't think it's something that we would include in FreeDOS - certainly not in the form it is right now. Adding text output like you suggest actually adds to the size and complexity of the program (because of internationalization, etc). But maybe the functionality of CMPR could be added to some other "batch tool."

     
  • Jim Hall

    Jim Hall - 2023-05-15
    • status: open --> closed
    • assigned_to: Jim Hall
     
  • Jim Hall

    Jim Hall - 2023-05-15

    Closed because this is expected DOS behavior

     
  • Shidel

    Shidel - 2023-06-16

    Hi Oliver,

    There is now a new tool called VTEST that is part of V8Power Tools. It supports 64-bit signed numerical and string comparisons as well as file system tests.

    Things like this are possible:

    vtest /tf %A /lt %B /gt %C /and %B /ne %D

    Which would evaluate as:
    /tf Display TRUE, FALSE or ERROR when finished.
    %A /lt %B is %A less than %B
    %B /gt %C is %B greater than %C
    /and End of First Expression, Evaluate next expression.
    %B /ne %D is %B not equal to %D
    TRUE if both the first and second parts are true.

    It is available in the latest V8Power Tools package at https://fd.lod.bz/repos/current/pkg-html/v8power.html

    :-)

    Jerome

     
    👍
    1

    Last edit: Shidel 2023-06-16

Log in to post a comment.

MongoDB Logo MongoDB