I have the following commands run in a command prompt:
C:\>set abc=
C:\>if "%abc%"=="" (echo abc is null)
C:\>
I dont understand why the above command would not echo "abc is null".
Thanks a lot!
Regards
Check that the "set abc=" line doesn't have a trailing space.
I have the following commands run in a command prompt and the result
is as follow:
C:\>set abc=
C:\>if "%abc%"=="" (echo abc is null)
C:\>
I don't understand why it would not echo "abc is null" finally.
Thanks a lot.
Regards,
> I have the following commands run in a command prompt:
>
>C:\>set abc=
>C:\>if "%abc%"=="" (echo abc is null)
>
>I dont understand why the above command would not echo "abc is null".
It fails here too at the command line but works in a batch file.
XP Pro SP2
Hmm.
Try, at the prompt:
echo %abc%
now - isn't that interesting?
The 'apparent' variable is not being expanded as a variable at the
command prompt. It would appear that expansion only occurs for
variables that are actually SET - clearly, a flaw. Therefore it
remains a literal string in the evaluation of the IF statement - and
"%abc%" is, in fact, NOT equal to "".
If you really need it to work at the command prompt (in NT/2K/2003/XP/
Vista?/2008?) use the other syntax ...
if not defined abc (echo abc is null)
However, there is little reason for this to be required to work at the
command prompt, so I suppose it's of little consequence. It does work
correctly in batch procedures, as has been pointed out earlier.
Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
That's normal for an undefined variable, although now that you point it out
it is odd.
c:\>set abc=
c:\>echo %abc%
%abc%
c:\>set abc=a
c:\>echo %abc%
a
I'm glad you guys put a reason to the issue - I had shrugged my shoulders
and put it down to the ways of Windows.
I don't understand either, but rather than waiting for MS to fix the
code of CMD.EXE, pragmatically try something equivalent:
if not defined abc echo abc is null
(Yes, not %abc%...)
I am sure that "set abc=" line doesnt have a trailing space. You may
copy and paste my code to try. Anyway, thank a lot for your reply :)
Hi foxidrive. But I have tried in Windows 2003 Server with SP2, it
does not work in batch file
Well here's my dos window - so there is no issue here.
You can use "if not defined abc (echo abc is null)" as has been suggested.
===[screen capture]===
M:\>a
abc is null
Press any key to continue . . .
M:\>type a.bat
@echo off
set abc=
if "%abc%"=="" (echo abc is null)
pause
M:\>
===[screen capture]===
This works for me in a batch in XP Pro SP2 ...
set abc=something
(set abc=)
if "%abc%"=="" (echo abc is null)
I must surmise, therefore, the issue is that trailing space that
foxidrive mentioned earlier.
It does.
89} All of a sudden "echo." doesn't work any more. What's wrong?
209940 Mar 6 2008 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
Http link format http://garbo.uwasa.fi/pub/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
No, Timo, it really doesn't - you have got to try it to believe it. I
didn't think it was true either, until I checked it out. It is a
definite bug in the way the command processor works - at the command
prompt, IMHO. The item you recommend is not really applicable here.
"what's wrong with my _script_?"
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful script tricks http://www.netikka.net/tsneti/info/tscmd.htm
>Tom Lavedas <tglb...@cox.net> wrote:
>> On Apr 18, 2:30 pm, Timo Salmi <t...@uwasa.fi> wrote:
>>> mander.l...@gmail.com wrote:
>>>> C:\>set abc=
>>>> C:\>if "%abc%"=="" (echo abc is null)
>>>> I don't understand why it would not echo "abc is null" finally.
>>> It does.
>>> 89} All of a sudden "echo." doesn't work any more. What's wrong?
>
>> No, Timo, it really doesn't - you have got to try it to believe it. I
>> didn't think it was true either, until I checked it out. It is a
>> definite bug in the way the command processor works - at the command
>> prompt, IMHO. The item you recommend is not really applicable here.
>
>"what's wrong with my _script_?"
The OP was speaking of command line usage though, Timo. :)
set abc= unassigns the env-var abc so it 'doesn't exist'
if "%abc%"=="" (echo abc is null) then tries to test an env-var that is not assigned and fails
Yes, but the point is that the test is supposed to report that the
variable is not set. In a batch procedure, that is exactly what
happens. However, the problem arises at the command prompt, where the
reverse is true - the result of the IF indicates that the variable is
SET, when it is not. Clearly, an IF statement is not a common
occurrence at the command prompt, but, still, the inconsistency is
troubling.
Welcome to the fun of cmd.exe. There are a few things that it treats
differently at the command prompt than it does in batch files, this is
just one of them.
--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
Don't walk behind me, I may not lead. Don't walk in front of me, I
may not follow. Just walk beside me and be my friend.
-- Albert Camus
>Tom Lavedas <tglb...@cox.net> wrote:
>>Yes, but the point is that the test is supposed to report that the
>>variable is not set. In a batch procedure, that is exactly what
>>happens. However, the problem arises at the command prompt, where the
>>reverse is true - the result of the IF indicates that the variable is
>>SET, when it is not. Clearly, an IF statement is not a common
>>occurrence at the command prompt, but, still, the inconsistency is
>>troubling.
>
>Welcome to the fun of cmd.exe. There are a few things that it treats
>differently at the command prompt than it does in batch files, this is
>just one of them.
It's not by design though - it's clearly a bug. A reproducible one. :)
OTOH, since IF DEFINED var is available, perhaps one should use that method.
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
I don't like this behavior (nor any of the other differences), but why
is it a bug? Is there's documentation that claims it shouldn't act
this way?
(No doubt it was a bug at one time, but much of cmd.exe's processing
is trying to emulate older versions correctly)
--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
Never underestimate the bandwidth of a station wagon full of tapes
hurtling down the highway.
-- Andrew S. Tanenbaum
>>>Welcome to the fun of cmd.exe. There are a few things that it treats
>>>differently at the command prompt than it does in batch files, this is
>>>just one of them.
>>
>>It's not by design though - it's clearly a bug. A reproducible one. :)
>
>I don't like this behavior (nor any of the other differences), but why
>is it a bug?
Because it breaks standard batch logic, as the OP demonstrated.
Here is how Win98se handles it
===[screen capture]===
Microsoft(R) Windows 98
(C)Copyright Microsoft Corp 1981-1999.
C:\>set abc=123
C:\>echo.%abc%
123
C:\>set abc=
C:\>echo.%abc%
C:\>
===[screen capture]===
Can anybody say if NT or W2K works in the same was as XP?
E:\>ver & rem win95cmd.exe in Windows 98
Microsoft Windows 2000 [Version 4.10.0000]
E:\>set abc=123
E:\>echo %123%
%123%
E:\>set abc=
E:\>echo %123%
%123%