Quantcast
Channel: CodeSection,代码区,Linux操作系统:Ubuntu_Centos_Debian - CodeSec
Viewing all articles
Browse latest Browse all 11063

Batch: Delete the line flow from the variable containing the first f ...

$
0
0

I have a variable containing an ID. This ID I want to write into a file and later retrieve it for further processing. Other stuff can be written to the file afterwards, thus I use the /P to retrieve only the first line.

SET ID=%RANDOM% ECHO %ID% >> my_file.txt SET /P LINE=< my_file.txt ECHO %ID%==%LINE%? IF "%ID%" == "%LINE%" ( ECHO Equal! ) ELSE ( ECHO Not equal! )

Output:

15770==15770 ? Not equal

I noticed the blank character before the question mark. My thought was that it was the linefeed , so I extended my script to:

SET ID=%RANDOM% ECHO %ID% >> my_file.txt SET /P LINE=< my_file.txt REM Try to remove the linefeed. SET LINE2=%LINE:~0,-1% ECHO %ID%==%LINE2%? IF "%ID%" == "%LINE2%" ( ECHO Equal! ) ELSE ( ECHO Not equal! )

Output:

16332==16332 ? Not equal

I still get a blank character, but remove two characters from the raw input, removes one of the digits.

How can I convert the input from the first line in my_file.txt so I get a value I can compare to the original value I wrote to the file?

if you use this format then there is no need for a trailing character

and there will not be a linefeed in normal use:

>>my_file.txt ECHO %ID%

When you use this there is a space after the variable contents, which is often used because a single digit like 1 before a redirection character will fail to echo the numeral.

ECHO %ID% >> my_file.txt


Viewing all articles
Browse latest Browse all 11063

Trending Articles