process monitor code - string comparison errors
My System details are :
NAME="Red Hat Enterprise Linux Server" VERSION="7.2 (Maipo)" Shell = bash I am trying to build a process monitor, that prints the status of the process. In the below code , I am trying to compare the process state pid_status with strings we get from command /proc/$pid/status But it throws error at the comparison if ["$pid_status" =~ .*"sleeping"*. ] || ["$pid_status" == *"stopped"* ] || ["$pid_status" == *"running"* ]; The error is: Quote:
Following is the output I am getting: Quote:
Code:
|
you must add space before/after [ and ].
From the other hand you can try shellcheck to analyze your script. That will find more errors and also will suggest solutions. |
Thanks pan64 for the reply.
I new modified statement is: Code:
if [ "$pid_status" =~ .*"sleeping"*. ] || [ "$pid_status" =~ .*"stopped"*. ] || [ "$pid_status" =~ .*"running"*. ]; then Quote:
|
use double [[ and ]] instead of [ and ]. that may help.
|
Thanks again pan64.
Its so simple , am not frequent user of shell script. |
The =~ operator only works for the [[ ]] compound, not the [ command.
Its right hand side is an extended regular expression (ERE, like egrep or grep -E). A .* means "any character any times" and is useless at the beginning or end of an ERE because it is "floating". The == operator within a [[ ]] is a shell glob. A * means "any character any times" and make sense at the beginning or end of the glob, because the glob is "anchored". Code:
if [[ "$pid_status" =~ "sleeping" ]] || [[ "$pid_status" == *"stopped"* ]] || [[ "$pid_status" == *"running"* ]]; then You better have Code:
if [ "$pid" ]; then Code:
if [ -n "$pid" ]; then No quotes needed in Code:
if [[ $pid] ]; then Code:
if [[ -n $pid ]]; then |
All times are GMT -5. The time now is 06:47 AM. |