You need something like this:
if
CONDITION_SEE_BELOW
then
server_var_shortname=$server_shared_shortname
server_var=$server_shared
server_var_bare=$server_shared_bare
else
server_var_shortname=$server_vps_shortname
server_var=$server_vps
server_var_bare=$server_vps_bare
fi
In Bash (and other shells), the CONDITION_SEE_BELOW
part has to be a command. A command returns a numerical value, and by convention 0
means "true" and any non-zero value means "false". The then
clause will execute if the command returns 0
, or the else
clause in all other cases. The return value is not the text output by the command. In shells, you can access it with the special variable expansion $?
right after executing a command.
You can test that with commands true
and false
, which do one thing: generate a zero (true
) and non-zero (false
) return value. Try this at the command line:
true ; echo "true returns $?"
false ; echo "false returns $?"
You can use any command you want in a condition. There are two commands in particular that have been created with the idea of defining conditions: the classic test command [ ]
(the actual command only being the opening bracket, which is also available as the test
command), and the double-bracketed, Bash-specific [[ ]]
(which is not technically a command, but rather special shell syntax).
For instance, say your switch
variable contains either nothing (null string), or something (string with at least one character), and assume in your case you mean a null string to be "false" and any other string to be "true". Then you could use as a condition:
[ "$switch" ]
If you are OK with a string containing only spaces and tabs to be considered empty (which will happen as a result of standard shell expansion and word splitting of arguments to a command), then you may remove the double quotes.
The double-bracket test command is mostly similar, but has some nice things about it, including double-quoting not being needed most of the time, supporting Boolean logic with &&
and ||
inside the expression, and having regular expression matching as a feature. It is, however a Bashism.
You can use this as a reference to various tests that can be performed with both test commands:
6.4 Bash Conditional Expressions
If at all interested in shell programming, be sure to find out about the various tests you can use, as you are likely to be using many of them frequently.