if condition not working comparing item to __ in bash - bash

When I do an echo $item, it gives me the result as:
__
after which I do
if [ "${item}" = "__" ]; then
$item = ""
fi
and again echo $item it still gives me __
and an error : __: command not found
and its not getting changed to empty string, can someone plz help

Hummm... why it should be changed? $item contains __. You compare x$item, which is x__ against __. The strings are different, so you don't enter the if body and the $item variable is not changed.
Code behaves correctly, why do you think it should be different?
UPDATE (after your question rewriting):
There are several errors in the code:
In the condition, you are not using the comparison operator. You are using the assignment operator. Change = for ==.
You are trying to assign a variable usng a dollar sign in the left hand side of the operation. Don't use $ for the LHS.
You are adding spaces to the sides of the assigment operator. Wrong again.
Proper code should be:
if [ "${item}" == "__" ]; then
item=""
fi

Related

variable is not getting set to AMER

Below is the code :
if [ "$p_geo_code" = S America ]; then
'$p_geo_code'=AMER; fi
Unable to get the value as AMER instead getting 'S America'.
can you let me know where i am going wrong,
Comparison with a multi-word string should be in quotes otherwise the shell won't know what you're asking it to compare, and variable assignment does not use the $ symbol, just the variable name:
if [ "$p_geo_code" = "S America" ]; then
p_geo_code=AMER
fi

Bash script to pass variable across functions

I have removed parentheses, but still I was not able to fetch ENV_NODE value in second function scpTAR. Please let me know what is wrong.
set -x
MASTER_HOSTNAME=`hostname | cut -d . -f1`
TARGET_ENVIRONMENT = it
evaluateEnvProp(){
if [ ${TARGET_ENVIRONMENT} = it ]; then
ENV_NAME=it && ENV_NODE=1cf62108e084
fi
}
scpTAR() {
echo ENV_NODE
echo ${ENV_NODE}
if [ ENV_NODE = ${MASTER_HOSTNAME} ] ; then
echo "scpTAR ENV_NODE = ${MASTER_HOSTNAME} "
else
"echo 'scpTAR ssh other node than jenkins server ENV_NODE=${MASTER_HOSTNAME}'"
fi
}
main(){
scpTAR
}
main
As #cyrus said, variables are global by default. What you did is setting the variables in a subshell:
( ENV_NAME=it && ENV_NODE=xyx && ENV_WLS_DOMAIN=user1 && ENV_NODE_PATH=path )
Because of that, these are gone (not propagated to calling script's environment) once the subshell exists. This is why you do not see their values set in scpTAR function. Remove the parentheses and your code should start working.
Update
Updated version of your code (based on answer by itChi) has another major error. You put spaces around the assignment operator when setting TARGET_ENVIRONMENT = it. This syntax is invalid and as a result TARGET_ENVIRONMENT is not assigned the specified value, thus the condition inside evaluateEnvProp function evaluates to false and ENV_NODE variable is not being set. Removing the spaces should solve the problem. You also did not call evaluateEnvProp as pointed out in update to #itChi's answer.
I'd highly recommend that you start using ShellCheck to verify correctness of your scripts.
As mentioned, variables are global by default, so if you reference them in your scpTAR function, you will get a return value.
However, as a second method, should you wish, you can reference it like so:
scpTAR $ENV_NAME $ENV_NODE $ENV_WLS_DOMAIN $ENV_NODE_PATH
Then in your scpTAR function reference them as:
echo "$1 $2 $3 $4"
it xyx user1 path
Particularly useful should you wish to run code on another machine, run a remote script, or set your script up to pass variables as arguments from bash.
EDIT:
Sorry if I tread on someone's toes, but here is your answer without subshell:
evalEnvProp(){
if [ ${TARGET_ENVIRONMENT} = "it" ]; then
ENV_NAME=it
ENV_NODE=xyx
ENV_WLS_DOMAIN=user1
ENV_NODE_PATH=path
fi
}
scpTAR() {
echo $ENV_NODE_PATH
}
main(){
evalEnvProp
scpTAR
}
main
Update2:
#!/bin/bash
set -x
MASTER_HOSTNAME=`hostname | cut -d . -f1`
TARGET_ENVIRONMENT=it
evaluateEnvProp(){
if [ ${TARGET_ENVIRONMENT} = "it" ]; then
ENV_NAME=it && ENV_NODE=1cf62108e084
fi
}
scpTAR() {
echo ENV_NODE
echo ${ENV_NODE}
if [ ENV_NODE == ${MASTER_HOSTNAME} ] ; then
echo "scpTAR ENV_NODE = ${MASTER_HOSTNAME} " else
"echo 'scpTAR ssh other node than jenkins server >ENV_NODE=${MASTER_HOSTNAME}'"
fi
}
main(){
evaluateEnvProp
scpTAR
}
main
MASTER_HOSTNAME needs `` for the command. you are calling hostname. You can also accomplish this with $()
Spacing in the IF statement either side of the = sign. otherwise you are not evaluating, you are setting a variable.
In your edit you missed out a function call to evaluateEnvProp which failed because you didn't set the variable.

How can I write if/else with Boolean in Bash? [duplicate]

This question already has answers here:
How can I declare and use Boolean variables in a shell script?
(25 answers)
Closed 5 years ago.
How can I write an 'if then' statement to switch between these to variables as in the following?
if(switch){
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
}
I'm not familiar with Bash syntax and basically just need an 'if/else' statement on a Boolean. Also, can I use true / false values as such? Also how do I do the 'else' statement?
$switch=true;
if $switch
then
server_var_shortname=$server_shared_shortname
server_var=$server_shared
server_var_bare=$server_shared_bare
fi
First, shells (including Bash) don't have Booleans; they don't even have integers (although they can sort of fake it). Mostly, they have strings.
Bash also has arrays... of strings. There are a number of ways of faking Booleans; my favorite is to use the strings "true" and "false". These also happen to be the names of commands that always succeed and fail respectively, which comes in handy, because the if statement actually takes a command, and runs the then clause if it succeeds and the else clause if it fails. Thus, you can "run" the Boolean, and it'll succeed if set to "true" and fail if set to "false". Like this:
switch=true # This doesn't have quotes around it, but it's a string anyway.
# ...
if $switch; 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
Note that the more usual format you'll see for if has square-brackets, like if [ something ]; then. In this case, [ is actually a command (not some funny sort of grouping operator) that evaluates its argument as an expression; thus [ "some string" = "some other string" ] is a command that will fail because the strings aren't equal. You could use if [ "$switch" = true ]; then, but I prefer to cheat and use the fake Boolean directly.
Caveat: if you do use the cheat I'm suggesting, make sure your "Boolean" variable is set to either "true" or "false" -- not unset, not set to something else. If it's set to anything else, I take no responsibility for the results.
Some other syntax notes:
Use $ on variables when fetching their values, not when assigning to them. You have $switch=true; up there, which will get you an error.
Also, you have a semicolon at the end of that line. This is unnecessary; semicolons are used to separate multiple commands on the same line (and a few other places), but they aren't needed to end the last (/only) command on a line.
The [ command (which is also known as test) has a kind of weird syntax. Mostly because it's a command, so it goes through the usual command parsing, so e.g. [ 5 > 19 ] is parsed as [ 5 ] with output sent to a file named "19" (and is then true, because "5" is nonblank). [ 5 ">" 19 ] is better, but still evaluates to true because > does string (alphabetical) comparisons, and "5" is alphabetically after "19". [ 5 -gt 19 ] does the expected thing.
There's also [[ ]] (similar, but cleaner syntax and not available in all shells) and (( )) (for math, not strings; also not in all shells). See Bash FAQ #31.
Putting commands in variables is generally a bad idea. See Bash FAQ #50.
shellcheck.net is your friend.
Bash doesn't have any concept of Boolean - there are no true / false values. The construct
[ $switch ]
will be true except when switch variable is not set or is set to an empty string.
[ ] && echo yes # Nothing is echoed
[ "" ] && echo yes # Nothing is echoed
unset switch && [ $switch ] && echo yes # Nothing is echoed
switch=1 && [ $switch ] && echo yes # 'yes' is echoed
switch=0 && [ $switch ] && echo yes # 'yes' is echoed - the shell makes no distinction of contents - it is true as long it is not empty
See also:
How can I declare and use Boolean variables in a shell script?
Here is a good guide for If else. But I want to show a different approach (which you will find also in the link on page 3).
Your coding looks like JavaScript, so I think with Switch you could also mean the case command instead of if. Switch in JavaScript is similar to case within a shell, but there isn't any method to check for Booleans. You can check string values for like true and false, and you can check for numbers.
Example...
#!/bin/bash
case "$Variable" in
false|0|"")
echo "Boolean is set to false."
;;
*)
echo "Boolean is set to true."
;;
esac
Addition
Keep in mind, there are many programs and tools that uses Boolean values in different forms.
Two examples...
SQL in general uses numbers as Boolean.
JavaScript uses true and false values.
Meaning: Your Bash script has to know the format of Booleans, before processing them!
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.
As addition to Gordon's excellent answer, in Bash you can also use the double-parentheses construct. It works for integers, and it is the closest form to other languages. Demo:
for i in {-2..2}; do
printf "for %s " "$i"
if (( i )) # You can omit the `$`
then
echo is nonzero
else
echo is zero
fi
done
Output:
for -2 is nonzero
for -1 is nonzero
for 0 is zero
for 1 is nonzero
for 2 is nonzero
You can use any arithmetic operations inside, e.g.:
for i in {1..6}; do
printf "for %s " "$i"
if (( i % 2 )) #modulo
then
echo odd
else
echo even
fi
done
Output
for 1 odd
for 2 even
for 3 odd
for 4 even
for 5 odd
for 6 even

Evaluate variable in if statement

So I have an array like:
al_ap_version=('ap_version' '[[ $data -ne $version ]]')
And the condition gets evaluated inside a loop like:
for alert in alert_list; do
data=$(tail -1 somefile)
condition=$(eval echo \${$alert[1]})
if eval "$condition" ; then
echo SomeAlert
fi
done
Whilst this generally works with many scenarios, if $data returns something like "-/-" or "4.2.9", I get errors as it doesn't seem to like complex strings in the variable.
Obviously I can't enclose the variable in single quotes as it won't expand so I'm after any ideas to expand the $data variable (or indeed the $version var which suffers the same possible fate) in a way that the evaluation can handle?
Ignoring the fact that eval is probably super dangerous to use here (unless the data in somefile is controlled by you and only you), there are a few issues to fix in your example code.
In your for loop, alert_list needs to be $alert_list.
Also, as pointed out by #choroba, you should be using != instead of -ne since your input isn't always an integer.
Finally, while debugging, you can add set -x to the top of your script, or add -x to the end of your shebang line to enable verbose output (helps to determine how bash is expanding your variables).
This works for me:
#!/bin/bash -x
data=2.2
version=1
al_ap_version=('ap_version' '[[ $data != $version ]]')
alert_list='al_ap_version'
for alert in $alert_list; do
condition=$(eval echo \${$alert[1]})
if eval "$condition"; then
echo "alert"
fi
done
You could try a more functional approach, even though bash is only just barely capable of such things. On the whole, it is usually a lot easier to pack an action to be executed into a bash function and refer to it with the name of the function, than to try to maintain the action as a string to be evaluated.
But first, the use of an array of names of arrays is awkward. Let's get rid of it.
It's not clear to me the point of element 0, ap_version, in the array al_ap_version but I suppose it has something to do with error messages. If the order of alert processing isn't important, you could replace the list of names of arrays with a single associative array:
declare -A alert_list
alert_list[ap_version]=... # see below
alert_list[os_dsk]=...
and then process them with:
for alert_name in ${!alert_list[#]}; do
alert=${alert_list[$alert_name]}
...
done
Having done that, we can get rid of the eval, with its consequent ugly necessity for juggling quotes, by creating a bash function for each alert:
check_ap_version() {
(($version != $1))
}
Edit: It seems that $1 is not necessarily numeric, so it would be better to use a non-numeric comparison, although exact version match might not be what you're after either. So perhaps it would be better to use:
check_ap_version() {
[[ $version != $1 ]]
}
Note the convention that the first argument of the function is the data value.
Now we can insert the name of the function into the alert array, and call it indirectly in the loop:
declare -A alert_list
alert_list[ap_version]=check_ap_version
alert_list[os_dsk]=check_op_dsk
check_alerts() {
local alert_name alert
local data=$(tail -1 somefile)
for alert_name in ${!alert_list[#]}; do
alert=${alert_list[$alert_name]}
if $alert "$data"; then
signal_alert $alert_name
fi
done
}
If you're prepared to be more disciplined about the function names, you can avoid the associative array, and thereby process the alerts in order. Suppose, for example, that every function has the name check_<alert_name>. Then the above could be:
alert_list=(ap_version os_dsk)
check_alerts() {
local alert_name
local data=$(tail -1 somefile)
for alert_name in $alert_list[#]; do
if check_$alert_name "$data"; then
signal_alert $alert_name
fi
done
}

Shell script test

I'm tring to update a bash script written by someone else and I've come accross a line I'm not sure about.
Can anyone tell me what the following check does:
if [ :$RESULT != :0,0 ]
I assume it's checking for some value in $RESULT, possibly with a substring?
Any help appreciated!
The command [ is just an alias of the command test, the closing square bracket just being sytax sugar (the command [ ignores the last argument if it's a closing bracket), so the line actually reads
if test :$RESULT != :0,0
It compares if the string :$RESULT equals to the string :0,0. The colon is prepended for the case that the variable $RESULT is empty. The line would look like the following if the colon was omitted and $RESULT was an empty string:
if test != 0,0
This would lead to an error, since test expects an argument before !=. An alternative would be to use quotes to indicate that there is an argument, which is an empty string:
if test "$RESULT" != 0,0
# Will become
if test "" != 0,0
The variation you posted is more portable, though.
I think the : is a common trick people use in case the variable is empty.
If it's empty, then the shell would have this:
if [ != 0,0 ]
which would be a syntax error. Putting the : in front means that if the variable is empty the shell has this:
if [ : != :0,0 ]
which is not a syntax error and would (correctly) report false.
Sometimes you'll see an x used in the way that the colon is used in your example.
The preferred way to do this type of test in Bash is to use the double square bracket:
if [[ $RESULT != 0,0 ]]
The double bracket form allows more flexibility, improved readability, reduced need for escaping and quoting and a few more features. See this page for more information.
If you want to test numeric values, instead of strings or files, use the double parentheses:
if (( 3 + 4 > 6 ))

Resources