4

I get a syntax error when using the following if condition in a bash script.

if [ -f $pid_file ] && [ $check_run_proc == *"my_proc"* ] && kill -0 $(cat "$pid_file"); then

Can you please identify what the problem is?

Thank you.

2
  • the star symbol... you mean *?
    – chaos
    Jun 12, 2015 at 14:42
  • thanks for replying, yes indeed, because I am trying to match the string my_proc inside the variable $check_run_proc which gets the PID of the process while running..
    – croussou
    Jun 12, 2015 at 14:44

1 Answer 1

3

You need to use bash's [[ special word and the =~ operator to match regular expressions. That if-clause should do it:

if [ -f "$pid_file" ] && [[ "$check_run_proc" =~ "my_proc" ]] && 
   kill -0 $(cat "$pid_file"); then
2
  • It's fixed now, thank you so much! I am having a bit issue with kill -0 $(cat "$pid_file") though basically if the pid file does not exist, I get error on the terminal, of course the script works is there any way to avoid it?
    – croussou
    Jun 12, 2015 at 15:03
  • it seems I didn't have this one before, maybe some of the symbols added have something to do with it?
    – croussou
    Jun 12, 2015 at 15:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .