Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string contains Asterisk (*)

I want to check if my string contain one or more asterisk.

I have tried this :

if [[ $date_alarm =~ .*\*.* ]]
then
    ...
fi

It worked when I launch directly the script, but not if this script is called during shutdown (script installed in run level 0 and 6 via update-rc.d)

Any idea, suggestion ?

Thanks

like image 204
voidAndAny Avatar asked Jul 28 '09 12:07

voidAndAny


3 Answers

Always quote strings.

To check if the string $date_alarm contains an asterisk, you can do:

if echo x"$date_alarm" | grep '*' > /dev/null; then
    ...
fi 
like image 117
William Pursell Avatar answered Oct 28 '22 12:10

William Pursell


expr "$date_alarm" : ".*\*.*"
like image 42
BreizhGatch Avatar answered Oct 28 '22 12:10

BreizhGatch


what happens if you replace

if [[ $date_alarm =~ .*\*.* ]]

with

if [[ "$date_alarm" =~ .*\*.* ]]

you might also want to try:

if [[ "$date_alarm" =~ '\*+' ]]

not sure about that one...

regards

like image 35
Atmocreations Avatar answered Oct 28 '22 12:10

Atmocreations