47

I have this shell script that isn't working.

Input:

Server_Name=1 if [ $Server_Name=1 ]; then echo Server Name is 1 else echo Server Name is not 1 fi 

Output:

Server Name is 1 

But, if i change Server_Name=2, the output is:

Server Name is 1 

When I change Server_Name to 2, I want it to say: Server Name is 2.

I know it is the if [ $Server_Name=1 ]; part.

How do i fix it?

1
  • As David points out below, you must use "-eq" to test numeric values. You might also want to check for a blank variable to avoid errors; if [ ! "x$var" = "x" ]; then\n if [ $var -eq 1 ]; then ... Commented Dec 16, 2013 at 1:41

5 Answers 5

71

Your script indicates you are using string comparisons.

Assume server name could be a string instead of number only.

For String comparisons:
if [[ "$Server_Name" == 1 ]]; then

Notes:

  • Spacing around == is a must
  • Spacing around = is a must
    if [ $Server_Name=1 ]; then is WRONG

  • [[ ... ]] reduces errors as no pathname expansion or word splitting takes place between [[ and ]]

  • Prefer quoting strings that are "words"

For Integer comparisons:
if [[ "$Server_Name" -eq 1 ]]; then


More information:

2
  • 9
    [[ is bash syntax and the OP was asking about shell, where it wouldn't work Commented Feb 16, 2018 at 16:56
  • I agree with @gilad905. Please use if [ "$Server_Name" = "1" ] or even if [ "$Server_Name" -eq "1" ] for comparing numeric value (would still work if Server_Name is defined with =01). Commented Nov 4, 2022 at 9:10
12

Try this:

if [ $Server_Name -eq 1 ];then 
1
  • 1
    Maybe write with quotes "$Server_Name" to be safe from any corner cases where the variable would contain spaces or other wild data. Commented Nov 4, 2022 at 9:12
2
[ $Server_Name=1 ] 

does not work as intended because the syntax inside the single brackets isn't special to Bash. As usual, the variable $Server_Name gets substituted by 1, so all the test ([) command sees is a single argument: the string 1=1. Since that sting has a non-zero length, test returns true.

For POSIX-compliant shells, you can use the following test commands:

[ "$Server_Name" = 1 ] 

checks is the $Server_Name is equal to the string 1.

[ "$Server_Name" -eq 1 ] 

checks is the $Server_Name is equal to the number 1, i.e., it does a numeric comparison instead of a string comparison.

The return value of the two command will differ, e.g., if you define Server_Name=01. The first one will return false, the second will return true.

Note that if the possibility exists that the variable $Server_Name is undefined, it must be quoted or test will display an error when invoked.

-1

Try,

 #!/bin/bash Server_Name=50 if [ $Server_Name = 49 ] then echo "Server Name is 50" else echo "Server Name is below 50" fi 

output:

 #./scriptname.sh Server Name is below 50 
1
  • 1
    This code messages are just untrue: if is 49, print "is 50"?!!, if not equal to 49, print "is below 50"?!!! Commented Nov 4, 2022 at 9:17
-1

Simple answer. Your code is correct - almost. the only thing you are missing is spaces... (and well maybe an extra "=")

if [ $Server_Name=1 ]; then 

will not compute correctly.

if [ $Server_Name == 1 ]; then 

is what you seek.

And now the statement about string versus numbers. Whenever you are searching for comparison like is / is-not, then == will always be fine.

And i assume you always have a server name as a string, not a number - right? ;-)

Good luck with your coding sturdy apprentice.

Ciao

1
  • 1
    == is not a standard way to test the value of a variable in shell! Use the test command (aka [). Commented Nov 4, 2022 at 9:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.