0

In my program I have to compare a string to the integer like

            temp=$(taskset -p $pid)
            if[ ${temp:35:1} -eq $cpu || ${temp:35:2} -eq $cpu ] ; then

Here I am comparing a substring from temp with integer cpu. But when I run this script I get following error

line 16: syntax error near unexpected token then' line 16: if[ ${temp:35:1} -eq $cpu || ${temp:35:2} -eq $cpu ] ; then'

How can I compare this string with integer?

0

3 Answers 3

2

|| is for the shell, use -o for the test command:

if [ ${temp:35:1} -eq $cpu -o ${temp:35:2} -eq $cpu ]

You might replace the whole thing by

if [ $(taskset -p $pid|awk '{print $NF}') = $cpu ]
Sign up to request clarification or add additional context in comments.

2 Comments

It's hard to tell without knowing what taskset returns and what the cpu variable contains. Answer updated anyway.
NF = number of fields, $NF = the value of the last field
0

Syntax error.

if [ condition1 ] || [ condition2 ] && [ condition3 ] ; then

Comments

0

to compare the value between an integer and a string you should compare the 2 vars inside double quotes: "$a" == "$b"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.