Comparing variables in shell scripts - shell

I have got a project that involves shell scripts and comparing values/variables within them. I have looked here and elsewhere on comparing variables and I have tried all the various examples given but I am running into something that is not as advertised. OS is Solaris10
I have created the following script as a learning experience-
#!/bin/ksh
stest()
{
if $X = $Y
then echo they're the same
else echo they're notthe same
fi
}
X=a
Y=a
stest
echo completed
I keep getting some variation of the following-
using shell sh or ksh-
#./test.sh
./test.sh[2]: a: not found
completed
using shell bash-
#./test.sh
./test.sh: line 5: a: command not found
completed
I have tried enclosing the if $X = $Y line in brackets and double brackets and I get back
[a: not found
or
[[a: not found
If I change the variables X and Y to the numeral "1" I get the same thing-
./test.sh[2]: 1: not found
I have tried enclosing things in single quotes, double quotes & backwards quotes.
Any help is appreciated.

After if, you need a shell command, like anywhere else. $X = $Y is parsed as a shell command, meaning $X is interpreted as a command name (provided that the value of the variable is a single word).
You can use the [ command (also available as test) or the [[ … ]] special syntax to compare two variables. Note that you need spaces on the inside of the brackets: the brackets are a separate token in the shell syntax.
if [ "$X" = "$Y" ]; then …
or
if [[ "$X" = "$Y" ]]; then …
[ … ] works in any shell, [[ … ]] only in ksh, bash and zsh.
Note that you need double quotes around the variables¹. If you leave off the quotes, then the variable is split into multiple words and each word is interpreted as a wildcard pattern. This doesn't happen inside [[ … ]], but the right-hand side of = is interpreted as a wildcard pattern there too. Always put double quotes around variable substitutions (unless you want the value of the variable to be used as a list of filename matching patterns, rather than as a string).
¹ Except on $X the [[ … ]] syntax.

This KornShell (ksh) script should work:
soExample.ksh
#!/bin/ksh
#Initialize Variables
X="a"
Y="a"
#Function to create File with Input
#Params: 1}
stest(){
if [ "${X}" == "${Y}" ]; then
echo "they're the same"
else
echo "they're not the same"
fi
}
#-----------
#---Main----
#-----------
echo "Starting: ${PWD}/${0} with Input Parameters: {1: ${1} {2: ${2} {3: ${3}"
stest #function call#
echo "completed"
echo "Exiting: ${PWD}/${0}"
Output :
user#foo:/tmp $ ksh soExample.ksh
Starting: /tmp/soExample.ksh with Input Parameters: {1: {2: {3:
they're not the same
completed
Exiting: /tmp/soExample.ksh
ksh version:
user#foo:/tmp $ echo $KSH_VERSION
#(#)MIRBSD KSH R48 2013/08/16

Related

bash function to greet user differently based on number of arguments given [duplicate]

My whole script is currently this:
#!/bin/sh
clear;
blanko="";
# Dummy-Variablen
variable=Testvariable;
if [[$variable == $blanko]];
then
echo "Nichts da!"
else
echo $variable
fi
and if I enter
TestSelect.sh
I get
/usr/bin/TestSelect.sh: line 6: [[Testvariable: command not found
Testvariable
How can I fix this?
This is problem:
if [[$variable == $blanko]];
Spaces are required inside square brackets, use it like this:
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
On a related note, spaces are required around [ ] as well:
if [ "$variable" = "$blanko" ]; then
# more code here
fi
Note that variables do need to be enclosed in double quotes inside [ ] to prevent word splitting and globbing. Double quotes also help when either of the variables being compared is not set - shell will throw a syntax error otherwise.
Look at the following post to understand why we need spaces around [ ]:
Why should there be a space after '[' and before ']' in Bash?
Another related post that talks about other syntax elements that need spaces as well:
Why is whitespace sometimes needed around metacharacters?
Finally, this post talks about the difference between [[ ]] and [ ]:
Difference between single and double square brackets in Bash
Related:
“0: command not found” in Bash
Just use #!/bin/bash on tope of script if you are using bash scripting like: if [[ $partition == "/dev/sda2" ]]; then to compare string and run script with ./scriptname.sh or bash scriptname.sh
If your script runs on your local with /bin/bash but not on your container with sh, then consider adding bash to your container by apk add --no-cache bash.

"command not found" error when using a variable [duplicate]

My whole script is currently this:
#!/bin/sh
clear;
blanko="";
# Dummy-Variablen
variable=Testvariable;
if [[$variable == $blanko]];
then
echo "Nichts da!"
else
echo $variable
fi
and if I enter
TestSelect.sh
I get
/usr/bin/TestSelect.sh: line 6: [[Testvariable: command not found
Testvariable
How can I fix this?
This is problem:
if [[$variable == $blanko]];
Spaces are required inside square brackets, use it like this:
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
On a related note, spaces are required around [ ] as well:
if [ "$variable" = "$blanko" ]; then
# more code here
fi
Note that variables do need to be enclosed in double quotes inside [ ] to prevent word splitting and globbing. Double quotes also help when either of the variables being compared is not set - shell will throw a syntax error otherwise.
Look at the following post to understand why we need spaces around [ ]:
Why should there be a space after '[' and before ']' in Bash?
Another related post that talks about other syntax elements that need spaces as well:
Why is whitespace sometimes needed around metacharacters?
Finally, this post talks about the difference between [[ ]] and [ ]:
Difference between single and double square brackets in Bash
Related:
“0: command not found” in Bash
Just use #!/bin/bash on tope of script if you are using bash scripting like: if [[ $partition == "/dev/sda2" ]]; then to compare string and run script with ./scriptname.sh or bash scriptname.sh
If your script runs on your local with /bin/bash but not on your container with sh, then consider adding bash to your container by apk add --no-cache bash.

Error running if statements in bash script [duplicate]

My whole script is currently this:
#!/bin/sh
clear;
blanko="";
# Dummy-Variablen
variable=Testvariable;
if [[$variable == $blanko]];
then
echo "Nichts da!"
else
echo $variable
fi
and if I enter
TestSelect.sh
I get
/usr/bin/TestSelect.sh: line 6: [[Testvariable: command not found
Testvariable
How can I fix this?
This is problem:
if [[$variable == $blanko]];
Spaces are required inside square brackets, use it like this:
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
On a related note, spaces are required around [ ] as well:
if [ "$variable" = "$blanko" ]; then
# more code here
fi
Note that variables do need to be enclosed in double quotes inside [ ] to prevent word splitting and globbing. Double quotes also help when either of the variables being compared is not set - shell will throw a syntax error otherwise.
Look at the following post to understand why we need spaces around [ ]:
Why should there be a space after '[' and before ']' in Bash?
Another related post that talks about other syntax elements that need spaces as well:
Why is whitespace sometimes needed around metacharacters?
Finally, this post talks about the difference between [[ ]] and [ ]:
Difference between single and double square brackets in Bash
Related:
“0: command not found” in Bash
Just use #!/bin/bash on tope of script if you are using bash scripting like: if [[ $partition == "/dev/sda2" ]]; then to compare string and run script with ./scriptname.sh or bash scriptname.sh
If your script runs on your local with /bin/bash but not on your container with sh, then consider adding bash to your container by apk add --no-cache bash.

Bash script conditionals simple error [duplicate]

My whole script is currently this:
#!/bin/sh
clear;
blanko="";
# Dummy-Variablen
variable=Testvariable;
if [[$variable == $blanko]];
then
echo "Nichts da!"
else
echo $variable
fi
and if I enter
TestSelect.sh
I get
/usr/bin/TestSelect.sh: line 6: [[Testvariable: command not found
Testvariable
How can I fix this?
This is problem:
if [[$variable == $blanko]];
Spaces are required inside square brackets, use it like this:
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
On a related note, spaces are required around [ ] as well:
if [ "$variable" = "$blanko" ]; then
# more code here
fi
Note that variables do need to be enclosed in double quotes inside [ ] to prevent word splitting and globbing. Double quotes also help when either of the variables being compared is not set - shell will throw a syntax error otherwise.
Look at the following post to understand why we need spaces around [ ]:
Why should there be a space after '[' and before ']' in Bash?
Another related post that talks about other syntax elements that need spaces as well:
Why is whitespace sometimes needed around metacharacters?
Finally, this post talks about the difference between [[ ]] and [ ]:
Difference between single and double square brackets in Bash
Related:
“0: command not found” in Bash
Just use #!/bin/bash on tope of script if you are using bash scripting like: if [[ $partition == "/dev/sda2" ]]; then to compare string and run script with ./scriptname.sh or bash scriptname.sh
If your script runs on your local with /bin/bash but not on your container with sh, then consider adding bash to your container by apk add --no-cache bash.

Multiple If Statements in Bash Script

I am trying to make a bash script with the output based on the input.
My code looks like this:
#!/bin/bash
echo "Letter:"
read a
if a=3
then
echo "LOL"
fi
if a=4
then
echo "ROFL"
fi
But when I enter 3 or 4, I get both LOL and ROFL.
Is there a way for me to get LOL for 3 and ROFL for 4?
Sorry if I'm using incorrect terms and stuff, I'm new to bash scripting.
In bash, a=3 is an assignment, not a test. Use, e.g.:
if [ "$a" = 3 ]
Inside [...], the equal sign tests for string (character) equality. If you want to test for numeric value instead, then use '-eq` as in:
if [ "$a" -eq 3 ]
The quotes around "$a" above are necessary to avoid an "operator" error when a is empty.
bash also offers a conditional expressions that begin with [[ and have a different format. Many like the [[ format better (it avoids, for example, the quote issue mentioned above) but the cost is loss of compatibility with other shells. In particular, note that dash, which is the default shell (/bin/sh) for scripts under Debian-derived distributions, does not have [[.
Bash thinks you're trying to assign a variable by saying a=3. You can do the following to fix this:
Use the = operator whilst referencing the variable with a $, like so: if [[ $a = 3 ]]
Use the -eq operator, which is special and doesn't require you to reference the variable with a $, but may not be compatible with all sh-derived shells: if [[ a -eq 3 ]]. If you wish to use -eq without Bash reference the variable: if [[ $a -eq 3 ]]
Note:
The double square brackets [[ ... ]] are a preferred format with specifically Bash conditionals. [ ... ] is good with any sh-derived shell (zsh, tcsh, etc).
if a=3 will assign value 3 to variable a
unless a is readonly variable, if a=3 always returns TRUE
same for if a=4
To compare variable a with a value, you can do this if [ $a = 3 ]
so the script should change to
#!/bin/bash
echo "Letter:"
read a
if [ $a = 3 ]
then
echo "LOL"
fi
if [ $a = 4 ]
then
echo "ROFL"
fi
Since a is read from user input, there is possibility user key in:
non numeric value
a string with empty space
nothing, user may just press Enter key
so a safer way to check is:
if [ "x$a" = "x3" ]

Resources