Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem with a bash script on raspberry pi:

x='gpio -g read 22'

if [ $x -ge 1 ]
then
gpio -g write 23 1
fi

The error is integer expression expected. Why?

share|improve this question
up vote 9 down vote accepted

That's because you are checking whether the string gpio -g read 22 is greater than 1. Since gpio -g read 22 is not a number, you get that error.

You don't explain what you are trying to do but I'm guessing you want to compare the output of the gpio command. To do that, you need to enclose the command in $() or backticks (``):

x=$(gpio -g read 22)

if [ "$x" -ge 1 ]
then
   gpio -g write 23 1
fi

Or, more simply:

[ "$(gpio -g read 22)" -ge 1 ] && gpio -g write 23 1

The assignment foo='command' doesn't run command. The variable foo takes the value of the string command and not its output.

share|improve this answer

The above answer works most of the time but take the following script:

#!/bin/bash

a='foo: '
b='44494949494'

if [ ${a} -eq ${b} ]
then
   echo "a matches b"
else
   echo "a is different than b"
fi

Instead of cleanly echoing one of the above choices, it does the following:

./test.sh: line 6: [: foo:: integer expression expected
a is different than b

To make the script work as expected (e.g. compare values as strings) you need to change the comparison to:

if [ ${a} = ${b} ]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.