Reputation: 145
I have a variable like below.
variable = This script is not found
if [[ "$variable" = ~ "not found" ]];
then
echo "Not Found"
else
echo "Its there"
if
while executing im getting below err,
line 4: syntax error in conditional expression
./test.sh: line 4: syntax error near `found"'
./test.sh: line 4: `if [[ "$variable" = ~ "not found" ]]; '
could anyone point me, What im missing here?
Upvotes: 14
Views: 57501
Reputation: 11
Input:
line="There\'s a substring to be found!"
if [[ "$line" =~ *"substring"* ]]; then
echo "Not Found";
else
echo "Found!"
fi
Output:
Found!
Upvotes: 0
Reputation: 69
I have tried below codes which always return same result either in true or false
if [[ "$variable" =~ "not found" ]]; then
echo "Not Found";
else
echo "Its there";
fi
Upvotes: -1
Reputation: 133
here is a correct construction of your if statement
if [[ "$variable" =~ "not found" ]]; then
echo "Not Found";
else
echo "Its there";
fi
Upvotes: 3
Reputation: 4925
Compare this with your version at the indicated points:
variable="This script is not found" # <--
if [[ "$variable" =~ "not found" ]] # <--
then
echo "Not Found"
else
echo "Its there"
fi # <--
You can't put spaces around = in an assignment, and you need to quote a string literal that has spaces. You don't need a trailing ; if you're going to put then on its own line. And an if-then ends with "fi" not "if".
Upvotes: 10
Reputation: 2903
LIST="some string with a substring you want to match"
SOURCE="substring"
if echo "$LIST" | grep -q "$SOURCE"; then
echo "matched";
else
echo "no match";
fi
Good Luck ;)
Upvotes: 28