Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line string Variable comparison

Tags:

command-line

I've been trying to compare my computername to some pre-set string. From reading around on google, namely http://commandwindows.com/batchfiles-branching.htm, I've attempted the following and many variants of the same line with /I, "%ComputerName", A513242 etc

IF (%ComputerName% == "A513242") (
  EXIT) ELSE (
    ECHO "else taken")

where "A513242" is the result of calling ECHO %ComputerName% this seems to always take the "else taken" branch.

Any help as to why the (EXIT) case is not being taken/ what syntactical mistake I am making would be appreciated.

like image 587
Kevin Zhou Avatar asked Jun 13 '12 15:06

Kevin Zhou


People also ask

How do you compare variables to strings?

2) You should use equals() method to compare String variables if you want to check equality, this is the right way of comparing String in Java. String overrides equals() from Object and changes it's default behavior which was same as the == operator.

Can we use == for string comparison?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

Can you use comparison operators on strings?

The comparison operators also work on strings. To see if two strings are equal you simply write a boolean expression using the equality operator.

Can you compare strings with == C?

You can't compare strings in C with ==, because the C compiler does not really have a clue about strings beyond a string-literal.


1 Answers

Try this:

if "%ComputerName%"=="A513242" (exit) else (echo "else taken")
like image 86
dcp Avatar answered Oct 22 '22 20:10

dcp