Check if the string contains Asterisk (*)

I want to check if my string contains one or more stars.

I tried this:

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

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

Any idea suggestion?

thanks

+5
source share
7 answers

Always specify lines.

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

if echo x "$ date_alarm" | grep '*'> / dev / null; then
    ...
fi 
+2
source
expr "$date_alarm" : ".*\*.*"
+2
source
case "$date_alarm" in
*\**)
  ...
  break
  ;;
*)
  # else part
  ...
  ;;
esac

, , /bin/sh, .

+1

,

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

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

:

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

...

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

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

Strange thing = ~. *. does not work only in the init context during shutdown, but works fine if it starts in the bash context ....

0
source

No need to redirect stdout like others do. Instead, use the -q option to grep:

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

0
source

All Articles