Shell If else with regex - shell

I am using shell to simple String regex match. Here is my shell
#!/bin/sh
MSG="ANK"
PATTERN="([A-Z]{3,5}[-][0-9]{2,5})"
if [ "$MSG" =~ "$PATTERN" ]; then
echo "MATCHED";
else
echo "not";
fi
It is giving error
abc.sh: 6: [: ANK: unexpected operator
How should I fix this?

Making the changes proposed by several contributors in the comments yields:
#!/bin/bash
MSG="ANK"
PATTERN="([A-Z]{3,5}[-][0-9]{2,5})"
if [[ "$MSG" =~ $PATTERN ]]; then
echo "MATCHED";
else
echo "not";
fi
Note the change to bash, the change to [[ and removal of the quotation marks around $PATTERN.

Related

Patter matching variable and string

bash script
Hi! I would like to make a bash script that contolli if the content of a var variable does pattern matching with the string ending with ABC?
You can use the bash builtins:
# with glob patterns
if [[ $var == *ABC ]]; then echo "$var ends with ABC"; fi
# with regular expression
if [[ $var =~ ABC$ ]]; then echo "$var ends with ABC"; fi
You are almost there
var="hellowordABC"
echo $var | grep ".*ABC$"
Or using builtin conditions
[[ $var =~ ABC$ ]] && echo "var ends with ABC"

Bash Escaping # Symbol In Variable

I'm having an issue with an if statement in a Bash script. Here's the code:
if [[ "$AppleID" -ne "" ]]; then
echo "<result>${AppleID}</result>"
else
echo "No user logged in."
fi
Assuming $AppleID is a string with a value of "test#email.com", the error message is as follows:
[[: test#email.com: syntax error: invalid arithmetic operator (error token is "#email.com")
I've tried using sed to escape the characters, like this:
if [[ `echo $(printf '%q' $AppleID) | sed -e 's/[\/&]/\\&/g'` -ne "" ]]; then
But I get the same error. How do I escape the # symbol?
-ne is for comparing integers. You want !=, or better yet, just use -n (which tests if its argument is a non-empty string):
if [[ -n "$AppleID" ]]; then
echo "<result>${AppleID}</result>"
else
echo "No user logged in."
fi

[[Bash]] Search for combined Expressions in every row

I am very new to Bash Scripting and I have a question regarding my CheckOurCodingRules.sh script:
I want to search for every 'hPar,' in a textfile and if found it should be checked if there is a also a 'const' in the same row.
Thats what I got so far but there is something wrong here:
while read line
do
if [[ $line == *hPar\,* ]] && [[ $line == *const\*]];then
DOCUMENTATION_TEST_A=1
else
echo DOCUMENTATION_TEST_A=0
fi
done < $INPUT_FILE
if [[DOCUMENTATION_TEST_A=0]];then
echo "error: Rule1: No const before hpar"
fi
There are a couple of issues with your script, see the code below which works for me:
DOCUMENTATION_TEST_A=0 # initial value
while read line
do
# spaces between conditional and brackets, no backslashes
if [[ $line == *hPar,* ]] && [[ $line == *const* ]]
then
DOCUMENTATION_TEST_A=1
break # optional, no need to scan the rest of the file
fi
done < $INPUT_FILE
# spaces and $, -eq is used for numerical comparisons
if [[ $DOCUMENTATION_TEST_A -eq 0 ]];
then
echo "error: Rule1: No const before hpar"
fi
A cleaner solution would be to use grep:
if ! grep "hPar," $INPUT_FILE | grep "const" >/dev/null
then
echo "error: Rule1: No const before hpar"
fi

Why does this if statement give me an error

Can someone explain why this simple bash script:
#!/bin/bash
myvar="Hello"
if [[ -z "$myvar" ]]; then
# echo "It's an unfilled string"
else
echo "It's a filled string!"
fi
gives me the error
./testscript: line 7: syntax error near unexpected token `else'
./testscript: line 7: `else'
However, if I remove the comment on the echo line, the script runs fine. Obviously, there is an issue with having commented lines within empty if statements. This this in mind, how do I fix it so I can have an empty if statement with comments?
There are no statements between then and else, so this is a syntax error. If you really want to do nothing in the if branch, then you can use a : (or true) as a placeholder:
#!/bin/bash
myvar="Hello"
if [[ -z "$myvar" ]]; then
# echo "It's an unfilled string"
:
else
echo "It's a filled string!"
fi
Better yet, reverse your logic:
#!/bin/bash
myvar="Hello"
if [[ -n "$myvar" ]]; then
echo "It's a filled string!"
fi
This is the way not to use if else statement.
#!/bin/bash
myvar="Hello"
[[ -n "$myvar" ]] && echo "It's a filled string!"
Also you can use this.
#!/bin/bash
myvar="Hello"
[[ -z "$myvar" ]] || echo "It's a filled string!"

How to check if string begins with a square bracked in bash

I need to go through my ini file and check if the line begins with a bracket, to figure out that the new config has started. Is there a way of checking this in bash? I tried
line="[test]"
if [[ "$line" =~ [.* ]]; then
echo "Got it!"
else
echo "Nothing found"
fi
but it doesn't work for me. I presume that the bracket needs to be somehow escaped, but I can't find any info as to how. Any help will be much appreciated.
To do this, you should backslash the special character [, so :
line='[test]'
if [[ $line =~ ^\[ ]]; then
echo "Got it!"
else
echo "Nothing found"
fi
EXPLANATIONS
[ is the starting character to opening a REGEX class, like [0-9]
quotes are needed everywhere but not inside bash [[ ]] tests
It's terrible to use a regexp for that! please use a bash glob instead:
if [[ $line = [* ]]; then
echo 'Got it!'
else
echo "Nothing found"
fi
Hope this helps use bash more efficiently.

Resources