I am trying to match two strings (IpAddress) as below. But it's not matching.
i=192.168.2.29
ipCheckInConfig="SG_1=192.168.2.24,192.168.2.29
> SG_2=192.168.2.20,192.168.2.23,192.168.2.31"
if echo "$i" | egrep -q "$ipCheckInConfig" ; then
echo "Matched"
else
echo "Not Matched"
fi
Could someone please help?
You don't need to call egrep for that. Use bash's internal regex capabilities:
if [[ "$ipCheckInConfig" =~ $i ]]; then
echo "Matched"
else
echo "Not Matched"
fi
Related
How do I check if a string is contained in the text produced by a cat command in bash? I want to execute a certain action if the string is found in the text.
Like this, using the pipe :
cat my_file | grep my_string
if [ ! $(cat my_file.txt | grep my_text) == "" ]
then echo "exists"
else
echo "not exists"
fi;
Piping to grep is fine if it's a one-time match. Otherwise in bash you can match strings using patterns (==) or regexes (=~) like this:
# date looks like "Mon, Jun 22, 2020 11:04:54 AM"
today=$(date)
[[ $today == Mon* ]] && echo "It's the start of the week"
[[ $today =~ ^(Tue|Wed|Thu) ]] && echo "It's the middle of the week"
[[ $today == Fri* ]] && echo "Thank God It's Friday!"
This is really a comment on the other answer - you can just directly test the exit status of a command
# grep succeeds (exit status 0) if there's a match
# grep -q is to suppress the output of the matched line
# we just care whether there is a match or not
if grep -q my_text my_file.txt; then
echo "exists"
else
echo "not exists"
fi
Simply:
if grep -q my_text file; then
echo "exists"
else
echo "not exists"
fi
I want to match a condition in bash containing "whitespaces" and "OR" condition within strings. I am unable to do as i am new to shell scripting, please help as it going to else loop where it is matching ${myarrlin[1]} . I am getting "Centrify is disabled" but i want the condition to be true here. Here is my code :
FILE_CENT="/etc/nsswitch.conf"
OS=`uname`
if [[ $OS = 'Linux' ]]; then
if [[ -e $FILE_CENT ]]; then
echo "nsswitch.conf found, Proceeding further..."
while read -r LINE
do
if [[ $LINE =~ ^passwd ]]; then
myarrlin=($LINE)
if [[ ${myarrlin[1]} =~ ^(centrify)|(centrifydc)[[:space:]]* || ${myarrlin[1]} =~ [[:space:]]+(centrify)|(centrifydc)[[:space:]]* ]]; then
echo "Centrify is enabled"
else
echo "Centrify is disabled"
fi
fi
done < $FILE_CENT
else
echo "nsswitch.conf does not exist in $OS, cannot fetch CENTRIFY information!"
fi
fi
nsswitch.conf >>>
passwd: centrify files
or
passwd: centrifydc files
or
passwd: files centrify
or
passwd: files centrifydc
Why are you doing this: myarrlin=($LINE) ?
If you just want to know if the line contains centrify:
while read -r LINE
do
if [[ ${LINE} =~ ^passwd ]]; then
if [[ ${LINE} == *"centrify"* ]]; then
echo "Centrify is enabled"
else
echo "Centrify is disabled"
fi
fi
done < $FILE_CENT
trying to validate a string at command line. each character should be among A-Z, a-z, 0-9 , special char(comma, underscore, period). If there are any other characters, display "invalid" else valid"
eg:
echo "hello123.txt" returns "valid"
echo "hello?.txt" returns "invalid"
echo "HEllo_hello" returns "valid"
Thank you.
If you have a suitable version of grep, you can use grep -v to determine this:
echo "test" | grep -v "^[A-Za-z0-9,_.]*$" > /dev/null
echo $? # 1
echo "#test" | grep -v "^[A-Za-z0-9,_.]*$" > /dev/null
echo $? # 0
In bash, you can use pattern matching on the right hand side of the == operator in [[ ... ]]:
#!/bin/bash
for string in 'hello123.txt' 'hello?.txt' 'HEllo_hello' ; do
if [[ $string == +([A-Za-z0-9,_.]) ]] ; then
echo valid
else
echo invalid
fi
done
You could create a script such as:
#!/bin/bash
if [[ $1 = "" ]] ; then
echo "Please run the following command with a string at the end...\
Example= ./script.bash testing"
exit 2
echo "$1" | grep -qi "^[a-z0-9.,_]*$"
if [[ $? = "0" ]] ; then
echo "Valid"
else
echo "Invalid"
fi
exit 0
I am trying to use the grep command however i do not know how to continue after the "grep $word $file" or even if that will work. What i need to do is get the program so that after they have typed in a word and a file if the word is in the file the program echo's "The word you have entered is in the file you have entered." and i also need it to print "I am sorry but the word you have entered is not in the file you have entered" if you can help it would be really helpful thank you
#!/bin/bash
echo "Welcome what please type in what you would like to do!"
echo "You can:"
echo "Search for words in file type (S)"
echo "Quit (Q)"
read option
while $option in
do
S) echo "What is the name of the file you would like to search for?"
read file
echo "What word would you like to find in the file?"
read word
grep $word $file
Q) echo "Goodbye!"
exit
esac
done
Use grep -q option to disable printing of the matching words and use echo $? to get the return value of grep. 0 is the return value if match found else 1 will be returned.
#!/bin/bash
echo "Welcome what please type in what you would like to do!"
echo "You can:"
echo "Search for words in file type (S)"
echo "Quit (Q)"
read option
case $option in
S) echo "What is the name of the file you would like to search for?"
read file
echo "What word would you like to find in the file?"
read word
grep -q $word $file
if [ $? -eq 0 ]; then
echo "$word found in $file"
else
echo "$word NOT found in $file"
fi
;;
Q) echo "Goodbye!"
exit
esac
Also you can use grep -w in case you want to match whole word. So your grep will look like
grep -wq $word $file
if [ $? -eq 0 ]; then
echo "$word found in $file"
else
echo "$word NOT found in $file"
Simple one liner using && and ||
grep -q $word $file && echo "found the word" || echo "not found the word"
eg
$ echo hello | grep -q hell && echo "found the word" || echo "not found the word"
found the word
$ echo hello | grep -q help && echo "found the word" || echo "not found the word"
not found the word
The line Hi=("Hi" "Hello" "Hey") has the possible inputs. I've tried adding a comma between the words and that does not work either. If hi, hello, or hey are typed in, I need it to echo "Hi". Right now only Hi works. I guess what I'm looking for is a way to make "synonyms" for a word. The ability to substitute a one word for another.
clear; echo
shopt -s nocasematch
echo; read -p " > " TextInput
Hi=("Hi" "Hello" "Hey")
if [[ "$TextInput" == $Hi ]]; then
clear; echo; echo
echo -e " >> Hi"
echo
else
clear; echo; echo
echo -e " >> Error"
echo
fi
I know I could use
if [[ "$TextInput" == "Hi" ]] || [[ "$TextInput" == "Hello" ]] || [[ "$TextInput" == "Hey" ]]; then
but that will get to be way too long.
If your target is bash 4.0 or newer, an associative array will work:
TextInput=Hello
declare -A values=( [Hi]=1 [Hello]=1 [Hey]=1 )
if [[ ${values[$TextInput]} ]]; then
echo "Hi there!"
else
echo "No Hi!"
fi
This is an O(1) lookup, making it faster than an O(n) loop-based traversal.
That said, if the list of items you're matching against is hardcoded, just use a case statement:
case $TextInput in
Hi|Hello|Hey) echo "Hi there!" ;;
*) echo "No Hi! ;;
esac
This also has the advantage of being compatible with any shell compliant with POSIX sh.
Take a look at this variant:
TextInput="Hello"
Hi=("Hi" "Hello" "Hey")
flag=0
for myhi in ${Hi[#]}; do
if [[ "$TextInput" == "$myhi" ]]; then
flag=1
break
fi
done
if [[ $flag == 1 ]]; then
echo "Hi there!"
else
echo "No Hi!"
fi
The thing is: use a flag + a for loop. If the flag is set (=1), then the TextInput is equal to something in your Hi array.
Depending on your needs, you can also use a switch:
case "$input" in
"Hi"|"He"*)
echo Hello
;;
*)
echo Error
;;
esac
This allows you to also specify patterns.
Using bash's pattern matching:
$ Hi=(Hi Hello Hey)
$ input=foo
$ if (IFS=:; [[ ":${Hi[*]}:" == *:"$input":* ]]); then echo Y; else echo N; fi
N
$ input=Hey
$ if (IFS=:; [[ ":${Hi[*]}:" == *:"$input":* ]]); then echo Y; else echo N; fi
Y
I use parentheses here to spawn a subshell, so changes to the IFS variable don't affect the current shell.