0
for svc in a b c d
 do
 service_status=$(service xy-mp-$svc status)
 if echo $service_status | egrep -i 'xy-mp-"$svc" dead but pid file exists|xy-mp-"$svc" is   stopped' &> /dev/null
 then
      service xy-mp-$svc restart
 fi             
 done

With "$svc",the variable substitution isn't taking place in the egrep.Does anyone have ideas on how I could do the variable substitution in this example?

Appreciate any pointers.

linuxuser26
  • 11
  • 2
  • 4
  • 3
    Variables aren't expanded inside single quotes, only double quotes. – Barmar Sep 02 '14 at 00:10
  • 1
    possible duplicate of [Difference between single and double quotes in bash](http://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – Barmar Sep 02 '14 at 00:11
  • [if echo $service_status | egrep -i "xy-mp-"$svc dead but pid file exists"|"xy-mp-$svc is stopped" &> /dev/null then service xy-mp-$svc restart fi]The above segment doesn't seem to work,but this seems to work:[if echo $service_status | egrep -i "xy-mp-$svc is stopped" &> /dev/null then service xy-mp-$svc restart fi ] – linuxuser26 Sep 02 '14 at 01:17
  • @Barmar,could you comment on this?I'm still unable to egrep for multiple phrases.Pls refer to this fpaste link – linuxuser26 Sep 02 '14 at 01:28
  • http://fpaste.org/130237/62193114/ – linuxuser26 Sep 02 '14 at 01:40
  • The format to `grep` for alternatives is `grep "alt1\|alt2\|alt3" input`. The `or` in `grep` `alt1 or alt2 or alt3` is the pipe `|`. **Note:** within double-quotes you must escape the pipe with `\\` to prevent it from being interpreted literally as a pipe by the shell **and** you must use double-quotes to enclose your alternatives. – David C. Rankin Sep 02 '14 at 03:35

0 Answers0