If condition in shell script fails [duplicate] - bash

This question already has answers here:
How to compare strings in Bash
(12 answers)
Closed last year.
I would like to what is the issue in this script , why this if condition is getting passed
i see that HI is printed , but type = A this if condition should not pass
[omega#ctsedgenode 1.1.11-SNAPSHOT_V12]$ type="A"
[omega#ctsedgenode 1.1.11-SNAPSHOT_V12]$ if [[ $type -eq "C" || $type -eq "D" ]]; then echo
"HI" ; fi
HI
[omega#ctsedgenode 1.1.11-SNAPSHOT_V12]$

Try with this instead:
if [[ $type == "C" || $type == "D" ]]; then echo "HI" ; fi

Related

Bash conditional pipe to select statement [duplicate]

This question already has an answer here:
Conditionally add a component to a shell pipeline
(1 answer)
Closed 5 days ago.
I want to have a conditional pipe to a select loop, I have found several answers on conditional pipes but haven't been able to get any to work. Is something like this possible?
if [ $condition -eq 0 ]; then
select action in "${actions[#]}"; do
elif [ $condition -eq 1 ]; then
sleep 0.1 | yes 1 | select action in "${actions[#]}"; do
fi
[[ ! "$REPLY" =~ $isNum ]] && continue
# Edit because someone is getting upset
#"${action[#]}"
done
This give the error syntax error near unexpected token elif
Block statements need to be nested properly, you can't use a conditional around just the statement that starts a block.
If you want to avoid writing the select block twice, put it in a function.
do_select() {
select action in "${actions[#]}"; do
[[ ! "$REPLY" =~ $isNum ]] && continue
"${action[#]}"
done
}
if [ $condition -eq 0 ]; then
do_select
else
sleep 0.1 | yes 1 | do_select
fi

bash if string starts with character [duplicate]

This question already has answers here:
Checking whether a string starts with XXXX
(5 answers)
Closed 6 months ago.
This is very basic but eluding me. I have an array. I am checking each element of this array to see if it starts with given character. I think it what i have written is right but not getting desired response.
My code
arr=("etpass-foo" "etpass-bar" "pass-foo" "pass-abc" "etpass-abc")
for i in "${arr[#]}"
do
if [[ $i == et* ]]; then
printf "$i"
fi
done
I get below output
etpass-foo
etpass-bar
pass-foo
pass-abc
etpass-abc
What is expect is
etpass-foo
etpass-bar
etpass-abc
I have also tried below if conditions
1. if [[ $i == et* ]]; then
2. if [[ "$i" == "et"* ]]; then
3. if [[ "$i" == "et*" ]]; then
Please let me know where i am wrong?
Try to use bashregex instead, like this:
...
if [[ $i =~ ^et.* ]]; then
echo "$i"
fi
...

How to call functions correctly and pass parameter through them using bash [duplicate]

This question already has answers here:
How to call a function in shell Scripting?
(7 answers)
Closed 1 year ago.
I'm trying to construct a basic calculator, but using functions. I'm getting the error line 17: multiplication: command not found over and over again. So I guess I'm calling the functions wrong? What's the correct way of calling the function and passing parameters then?
#!/bin/bash
echo "Enter the operation:"
read operation
echo "Operand 1:"
read operand_1
echo "Operand 2:"
read operand_2
if [[ "$operation" == "+" ]]
then
addition
elif [[ "$operation" == "-" ]]
then
subtraction
elif [[ "$operation" == "*" ]]
then
multiplication
elif [[ "$operation" == "/" ]]
then
division
fi
addition()
{
result = $((operand_1+operand_2))
result $result
}
subtraction()
{
result = $((operand_1-operand_2))
result $result
}
multiplication()
{
result = $((operand_1*operand_2))
result $result
}
division()
{
result = $((operand_1/operand_2))
result $result
}
result()
{
echo "The result is $1"
}
You need to define the functions before they are being called in the bash script

Using if conditions in shellscripting [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
read A
if [["$A" == 'Y' -o "$A" =='y']]
then echo "YES"
else echo "NO"
fi
I am very new to shell scripting.basically, i am trying to check if the input is Y or y.
I am getting the following error which I am not able to debug.
solution.sh: line 2: [[Y: command not found
Thanks.
You are missing space after [[
The if should be like
if [[ "$A" == 'Y' || "$A" == 'y' ]]
The [[ ]] is an extended test command, like any command it should be separated from others by spaces
Add proper space inside the square brackets and after the ==:
read A
if [ "$A" == 'Y' -o "$A" == 'y' ]; then
echo "YES";
else
echo "NO";
fi
Note that to use -o you ought to use test command, which uses single brackets.
If you use [[ ]], with double brackets, you must use || instead of -o:
read A
if [[ "$A" == 'Y' || "$A" == 'y' ]]; then
echo "YES";
else
echo "NO";
fi

bash string equality [duplicate]

This question already has answers here:
What is the difference between operator "=" and "==" in Bash?
(2 answers)
Closed 7 years ago.
In bash, what's the difference, if any, between the equal and double equal test operators?
[[ "a" = "a" ]] && echo equal || echo not-equal
[[ "a" == "a" ]] && echo equal || echo not-equal
[[ "a" = "b" ]] && echo equal || echo not-equal
[[ "a" == "b" ]] && echo equal || echo not-equal
results in:
equal
equal
not-equal
not-equal
There's no difference, == is a synonym for = (for the C/C++ people, I assume). See here, for example.
You could double-check just to be really sure or just for your interest by looking at the bash source code, should be somewhere in the parsing code there, but I couldn't find it straightaway.

Resources