If you're using a recent bash (v3+) suggest bash regex comparison operator =~
, i.e.
if [[ "$HOST" =~ ^user.* ]]; then
echo "yes"
fi
To match this or that
in a regex use |
, i.e.
if [[ "$HOST" =~ ^user.*|^host1 ]]; then
echo "yes"
fi
----------
The =~ Regular Expression matching operator within a double brackets test expression. (Perl has a similar operator.)
#!/bin/bash variable="This is a fine mess." echo "$variable" # Regex matching with =~ operator within [[ double brackets ]]. if [[ "$variable" =~ T.........fin*es* ]] # NOTE: As of version 3.2 of Bash, expression to match no longer quoted. then echo "match found" # match found fi |
Or, more usefully:
#!/bin/bash input=$1 if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]] # ^ NOTE: Quoting not necessary, as of version 3.2 of Bash. # NNN-NN-NNNN (where each N is a digit). then echo "Social Security number." # Process SSN. else echo "Not a Social Security number!" # Or, ask for corrected input. fi |
refer the link
http://tldp.org/LDP/abs/html/bashver3.html#REGEXMATCHREF