Bash checking if string does not contain other string - bash

I have a string ${testmystring} in my .sh script and I want to check if this string does not contain another string.
if [[ ${testmystring} doesNotContain *"c0"* ]];then
# testmystring does not contain c0
fi
How can I do that, i.e. what is doesNotContain supposed to be?

Use !=.
if [[ ${testmystring} != *"c0"* ]];then
# testmystring does not contain c0
fi
See help [[ for more information.

Bash allow u to use =~ to test if the substring is contained.
Ergo, the use of negate will allow to test the opposite.
fullstring="123asdf123"
substringA=asdf
substringB=gdsaf
# test for contains asdf, gdsaf and for NOT CONTAINS gdsaf
[[ $fullstring =~ $substring ]] && echo "found substring $substring in $fullstring"
[[ $fullstring =~ $substringB ]] && echo "found substring $substringB in $fullstring" || echo "failed to find"
[[ ! $fullstring =~ $substringB ]] && echo "did not find substring $substringB in $fullstring"

As mainframer said, you can use grep, but i would use exit status for testing, try this:
#!/bin/bash
# Test if anotherstring is contained in teststring
teststring="put you string here"
anotherstring="string"
echo ${teststring} | grep --quiet "${anotherstring}"
# Exit status 0 means anotherstring was found
# Exit status 1 means anotherstring was not found
if [ $? = 1 ]
then
echo "$anotherstring was not found"
fi

Related

Regarding Bash substring comparison

I try to test if a string starts with a certain prefix. But my script seems not work (I would expect the "if" branch will not get run). Can some Bash expert help to take a look? thanks!
Here is my code and test result:
$ cat testb.bash
#!/bin/bash
my_var="abcdefg";
if [[ "${my_var:0:5}"=="order" ]]; then
echo "value of my_var is ${my_var}.";
fi;
if [[ "${my_var:0:5}" -eq "order" ]]; then
echo "value of my_var is ${my_var}.";
fi;
if [ "${my_var:0:5}"="order" ]; then
echo "value of my_var is ${my_var}.";
fi;
$ bash -x testb.bash
+ my_var=abcdefg
+ [[ -n abcde==order ]]
+ echo 'value of my_var is abcdefg.'
value of my_var is abcdefg.
+ [[ abcde -eq order ]]
+ echo 'value of my_var is abcdefg.'
value of my_var is abcdefg.
+ '[' abcde=order ']'
+ echo 'value of my_var is abcdefg.'
value of my_var is abcdefg.
$
Whitespace is significant in this case. As you can see in the -x output, it understands the first condition as
[[ -n "${my_var:0:5}==order" ]]
Moreover, to test for a prefix, you can use a pattern:
[[ $my_var == order* ]]
To test the existence of substring, you can use either of these:
if [[ "$j" =~ string1 ]]; then
if [[ $j == *string1* ]]; then
In your particular case, you miss a space surounding ==, so instead of
if [[ "${my_var:0:5}"=="order" ]]; then
it should be
if [[ "${my_var:0:5}" == "order" ]]; then
^ ^
Finally, note that your condition was evaluated as true because it was evaluating if [ "string" ], which is true if string is not empty:
$ [ "a" ] && echo "yes"
yes
Test
$ cat a
#!/bin/bash
my_var="abcdefg";
if [[ "${my_var:0:5}" == "order" ]]; then
echo "value of my_var is ${my_var}."
elif [[ "${my_var:0:5}" == "abcde" ]]; then
echo "yeahaa"
else
echo "is not"
fi
$ ./a
yeahaa
Ok, i tested your code, you shoud such as the following code:
prefix="pre_order";
pre="pre_"
len=${#pre}
echo $len
if [[ "${prefix:0:len}" == "blahvlah" ]] ; then
echo "dddd"
fi;
Notes:
use == for string comparation
for ${} you should initilize a string variable before ${}
use len=${#pre} for lenght of string.
A POSIX-compliant way to test for a prefix is to attempt to remove the prefix, and compare the result to the original string. If the two are the same, the prefix is not present, the removal fails, and the expression expands to the original string.
prefix=foo
string=foobar
if [ "${string#$prefix}" = "$string" ]; then
printf "$string does not start with $prefix\n"
else
printf "$string starts with $prefix\n"
fi

Checking if char is within set

I'm trying to check if some string from length 1 and has only following chars: [RGWBO].
I'm trying the following but it doesn't work, what am I missing?
if [[ !(${line[4]} =~ [RGWBO]) ]];
This is what you want:
if [[ ${line[4]} =~ ^[RGWBO]+$ ]];
This means that the string right from the start till the end must have [RGWBO] characters one or more times.
If you want to negate the expression just use ! in front of [[ ]]:
if ! [[ ${line[4]} =~ ^[RGWBO]+$ ]];
Or
if [[ ! ${line[4]} =~ ^[RGWBO]+$ ]];
This one would work with any usable version of Bash:
[[ -n ${LINE[0]} && ${LINE[0]} != *[^RGWB0]* ]]
Even though I prefer the simplicity of extended globs:
shopt -s extglob
[[ ${LINE[0]} == +([RGWBO]) ]]
Use expr (expression evaluator) to do substring matching.
#!/bin/bash
pattern='[R|G|W|B|O]'
string=line[4]
res=`expr match "$string" $pattern`
if [ "${res}" -eq "1" ]; then
echo 'match'
else
echo 'doesnt match'
fi
Approach
Test the string length with ${#myString}, if it's egal to 1 proceed to step 2 ;
Does is contains your pattern.
Code
re='[RGWBO]';
while read -r line; do
if (( ${#line} == 1 )) && [[ $line == $re ]]; then
echo "yes: $line"
else
echo "no: $line"
fi
done < test.txt
Resources
You may want to look at the following links:
Bash: Split string into character array's answer ;
Length of a string, use ${#myString} ;
Extracting parts of strings, use ${myString:0:8} ;
Data
The test.txt file contains this
RGWBO
RGWB
RGW
RG
R
G
W
B
O
V

Check if string contains non digit characters

How can I check if a given string contains non numeric characters, examples :
x11z returns > 0
x$1 also returns > 0
1111~ also returns > 0
By character I mean everything not between 0-9. I saw similar threads but non of them talks about "non 0-9" except they show if its a-z or A-Z.
Just use a negated character class:
grep [^0-9]
This will match any non-numeric character, and not strings composed of only digits.
Just by using bash pattern matching:
[[ "$MY_VAR" =~ ^[^0-9]+$ ]] && echo "no digit in $MY_VAR"
Something like this:
if [[ "xf44wd" =~ [0-9]+ ]]; then
echo "contains $?"
else
echo "does no contains $?"
fi
or
if [[ ! "xf44wd" =~ [0-9]+ ]]; then
echo "does not contains $?"
else
echo "contains $?"
fi
Most of these suggestions return true if the first character is a digit, but don't find errors within the string. The function below returns true if the entire string is digits, false if any non-digits are detected in the string.
function isdigit () {
[ $# -eq 1 ] || return 1;
[[ $1 = *[^0-9]* ]] && return 1
return 0
}
Another option, a bash "containment" check
[[ "xf4fgh" = *[^0-9]* ]]
echo $?
0
[[ "1234" = *[^0-9]* ]]
echo $?
1

Repetitive Sequence in Bash

I'm stuck in something in my bash script, I have a string that composes of a repetitive sequence of 20s, e.g. 202020, there might be more or less 20s, e.g. 2020 or 2020202020, I want to create an if condition that if finds any strange number inside, e.g. 30 in 20203020, gives an error.
Any ideas?
this should do the test:
[[ "$var" =~ "^(20)+$" ]]
check this:
kent$ [[ "202" =~ "^(20)+$" ]] && echo "y" || echo "n"
n
kent$ [[ "20203" =~ "^(20)+$" ]] && echo "y" || echo "n"
n
kent$ [[ "20202002" =~ "^(20)+$" ]] && echo "y" || echo "n"
n
kent$ [[ "20202020" =~ "^(20)+$" ]] && echo "y" || echo "n"
y
how about this example, can you use it?
if [ "`echo "202020302020" | sed -e 's/20//g'`" ];
then echo "there is something";
fi
Extended patterns are a tiny bit more compact than regular expressions.
shopt -s extglob
if [[ $str = +(20) ]]; then
echo "twenties"
else
echo "mismatch"
fi
At some point, bash changed to treat all patterns in [[...]] expressions as extended patterns, so the shopt command may not be necessary.

How do I validate decimal numbers?

Hi I'm working on an assignment and got stuck on this part, how do I validate decimal numbers/numbers in shell?
It can accept numbers but not decimal numbers. I want it to be able to accept both.
This is what I have so far
if echo $value | egrep '^[0-9]+$' >/dev/null 2>&1 ; then
echo "OK"
else
echo "There Is An Error"
echo "Please Try Again"
fi
Instead of using grep, you can use the bash to check expression:
#!/bin/bash
value=98.23
if [[ "$value" =~ ^[0-9]+(\.[0-9]+)?$ ]]
then
echo good
else
echo bad
fi
use this regex instead ^[0-9]*(\.[0-9]+)?$
Using bash's pattern matching:
shopt -s extglob
while read line; do
if [[ $line == ?([-+])+([0-9])?(.*([0-9])) ]] ||
[[ $line == ?(?([-+])*([0-9])).+([0-9]) ]]
then
echo "$line is a number"
else
echo "$line NOT a number"
fi
done << END
1
-1
a
1a
1.0
1.
.0
.
-.0
+
+0
+.0
END
outputs
1 is a number
-1 is a number
a NOT a number
1a NOT a number
1.0 is a number
1. is a number
.0 is a number
. NOT a number
-.0 is a number
+ NOT a number
+0 is a number
+.0 is a number
The patterns:
optional sign, followed by one or more digits, followed optionally by a dot and zero or more digits
optional sign, followed by zero or more digits, followed by a mandatory dot, followed by one or more digits.
How about this:
if [ ! -z $(echo "$value" | grep -o "^[1-9][0-9]*\.\?[0-9]*$") ]; then echo ok; fi
-z tests for an empty string. So the negation [ ! -z "" ] will be fulfilled if the given string starts with a matching pattern.
In standard shell ([[ is non-standard) test will do the validation for you.
if test "$value" -eq 0 -o "$value" -ne 0 2> /dev/null; then
: # $value is an integer
else
: # $value is not an integer
fi
Try this: It checks negative and decimal number also.
echo $value | egrep '^-[0-9]+$|^[0-9]+$|^[0-9].[0-9]+$|^-[0-9].[0-9]+$' > /dev/null
Works in both Bash 3.0 and 4.0.
isInteger() {
[[ $1 =~ ^[0-9]+$ ]];
}
isDecimal() {
[[ $1 =~ ^[0-9]+\.[0-9]+$ ]] && ! isInteger $1;
}
computer:~ # isDecimal 123 && echo true || echo false
false
computer:~ # isDecimal 12.34 && echo true || echo false
true
computer:~ # isDecimal 12.34a && echo true || echo false
false
computer:~ # isDecimal 0.0000001 && echo true || echo false
true
To check if number, simply test against both functions.

Resources