papampi
papampi

Reputation: 127

bash script syntax error in expression

In a bash script I have to find the running time of a proccess, I do it with this command:

PROCCESS_TIME=$(ps -eo pid,etimes | grep $(ps -ef | grep SCREEN | grep PROCCESS | head -1 | awk '{print $2}') | awk '{print $2}')

Which gives me running time in seconds,then I need to convert it to minutes

PROCCESS_TIME_MIN=$(( $PROCCESS_TIME / 60 ))

It works most of the time but sometimes give me this error and script exits

0 / 60 : syntax error in expression (error token is "0 / 60 ")

or:

1893 / 60 : syntax error in expression (error token is "1893 / 60 ")

Where is my problem, How to solve it?

Upvotes: 1

Views: 1166

Answers (2)

Walter A
Walter A

Reputation: 20022

You will get that error message in this situation:

PROCCESS_TIME="0
0"
PROCCESS_TIME_MIN=$(( $PROCCESS_TIME / 60 ))

Check

PROCCESS_TIME=$(ps -eo pid,etimes | grep something | awk '{print $2}')

This can return more than 1 result. You can add |head -1 in the command, or change the awk: awk '{print $2; exit}'.
When you change your variables to lowercase, correct the spelling of proccess_time and use braces, the command will look like

process_time=$(ps -eo pid,etimes | 
   grep $(ps -ef | grep SCREEN | grep PROCCESS | awk '{print $2;exit}') |
   awk '{print $2; exit}')

I would try to change this command so that you would need 1 ps and 1 awk, but I have a different ps command (without -o etimes but with -o etime, returning time as hh:mm:ss).
Also try to use grep -E "SCREEN.*PROCCESS" or grep -E "PROCCESS.*SCREEN"

Upvotes: 2

RavinderSingh13
RavinderSingh13

Reputation: 133590

Try using a variable for 60(I am simply giving it name as sixty, you could change it as per your need too) and try following then:

sixty=60
var=$((PROCCESS_TIME / sixty))

Upvotes: 0

Related Questions