Question

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

Was it helpful?

Solution

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 

OTHER TIPS

expr "$date_alarm" : ".*\*.*"
case "$date_alarm" in
*\**)
  ...
  break
  ;;
*)
  # else part
  ...
  ;;
esac

The syntax is, well, /bin/sh, but it works.

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

if echo $date_alarm|perl -e '$_=<>;exit(!/\*/)'
then
    ...
fi

Finally

if echo x"$date_alarm" | grep '*' > /dev/null; then

did the trick

Strange thing =~ .*. doesn't work only in init context during shutdown, but work perfectly if launch in bash context....

No need to redirect stdout like others do. Use the -q option of grep instead:

if echo x"$date_alarm" | grep -q '*' ; then

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top