Bash compare if two strings match by length [duplicate] - bash

This question already has answers here:
String length of bash
(3 answers)
Closed 1 year ago.
I have two string variables that I need to compare by length inside of if. So I need to do something like this:
if [ length($string_one) != length($string_two) ]:
fi

if [[ ${#string} != ${#string_two} ]]; then
run command
fi

Related

How to only accept input that is a valid index of an array in bash [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Difference between single and double square brackets in Bash
(7 answers)
Why is "[[ 10 < 2 ]]" true when comparing numbers in bash? [duplicate]
(1 answer)
Closed 6 months ago.
I am trying some simple input validation in bash. Basically this part of my script outputs "press [index] to select [array element]." However I cannot seem to get it to stop and exit gracefully when an invalid input is entered. From my research so far this SHOULD work:
declare -a scenarios=()
scenarios+=("Scenario_123")
scenarios+=("Scenario_456")
scenarios+=("Scenario_789")
for i in ${!scenarios[#]}; do
echo -e "select $i for ${scenarios[$i]}"
done
read ScenInd
echo ${#scenarios[#]} #[${ScenInd}=~^[0-9]+$, =~ ^[[:digit:]]+
if ! [${ScenInd}=~ ^[[:digit:]]+$ ] || [${ScenInd} < 0] || [${ScenInd} >= ${#scenarios[#]}];
then
echo "INVALID SELECTION"
exit
fi
but when I run it and enter 8, I get '[8=~: command not found'
What have I done wrong and how do I fix this? I have tried this both with [${ScenInd}=~^[0-9]+$ and =~ ^[[:digit:]]+ yet the results are the same.
Thanks in advance

Why is bash behaving differently when it takes argument in braces? [duplicate]

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 1 year ago.
Input: range 6
function range {
echo {0..$1}
echo {0..6}
if [[ $1 =~ 6 ]]
then
echo "Equal"
fi
}
Output:
{0..6}
0 1 2 3 4 5 6
Why is the output different whereas $1 and 6 is equal?
The curly-braces get expanded before $-variables are expanded, and the curly-brace range syntax simply doesn't recognize anything other than a pair of numbers with dots in between.

Check number of arguments in a script [duplicate]

This question already has answers here:
Check that there are at least two arguments given in a bash script
(2 answers)
Closed 1 year ago.
How can I check in my script if I have at least 3 arguments, it must be something like:
if #number_of_arguments < 3
then
echo "Not enough arguments"
if [ $# -lt 3 ]; then
...
fi
In bash an alternative is to use an arithmetic expression.
if (( $# < 3 )); then
...
fi

How to merge two if in shell script [duplicate]

This question already has answers here:
Compound 'if' statements with multiple expressions in Bash
(4 answers)
An if statement with AND operator [duplicate]
(2 answers)
Closed 2 years ago.
My shell script looks like below.
#!/bin/bash
STR1="STRING1"
STR2="STRING2"
if [[ "sample string contains some value" == *"contains"* ]]; then
if [ "$STR1" != "$STR2" ]; then
echo "do something"
fi
fi
How do I can merge two if line into single if?
My script searches for a substring in a string and compares the other two strings to do something.

bash script if and while conditions [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
I am having trouble with executing the following bash script:
#!/bin/bash
response=" "
while ["$response" != "q"]; do
echo -n "Please enter a response"; read response
done
ALSO
!/bin/bash
response="x"
if ["$response" = "x"]
then
echo "the value is x"
fi
What could the possible errors be?
Your while and if statements are spaced wrong.
while [ "$response" != "q" ]; do etc
You need a space between the bracket and the double quote.

Resources