0

I am trying to use the i2cset command to display characters on my LCD screen. For example i2cset -y 1 0x3e 0x40 0x49 displays I and so on. Now I want to try and display my ip address. I had posted this question earlier in a forum where it was suggested that I could use the following bash script:

#!/bin/bash

IP=$(hostname -I)

function hex()
{
printf "0x%02x\n" "'$1"
}

for i in "echo $IP | grep -o .";
do
hx=$(hex $i)
i2cset -y 1 0x3e 0x40 $hx
done

I execute the script using the command :

sudo bash -x ip.sh

ip.sh being the filename..

I get the following information printed on my laptop screen:

root@beaglebone:~# sudo bash -x ip.sh
++ hostname -I
+ IP='192.168.2.111 192.168.7.2 '
+ for i in '"echo $IP | grep -o ."'
++ hex echo 192.168.2.111 192.168.7.2 '|' grep -o .
++ printf '0x%02x\n' ''\''echo'
+ hx=0x65
+ i2cset -y 1 0x3e 0x40 0x65

Updated Script:

#!/bin/bash

IP=$(hostname -I)

function hex()
{
printf "0x%02x\n" "'$1"

 }

for i in $(echo $IP | grep -o .)
hostname -I | while read -n1 i
do
  hx=$(hex $i)
  i2cset -y 1 0x3e 0x40 $hx
done

It gives an error now saying : try1.sh: line 12: syntax error near unexpected token hostname' try1.sh: line 12:hostname -I | while read -n1 i'

Kindly help.

  • How are you executing that script? The script runs fine on that part with bash, but with sh, it will give the error shown. – muru Feb 2 '15 at 13:33
  • Is this a copy or a re-type? I ask, because it works for me - not that I have an LCD, but adding an echo to the i2cset line gives me a result - only one digit though. – Mark Williams Feb 2 '15 at 13:34
  • @muru: I run it using the following command from the command line : 'sudo sh ip.sh' – Goldengirl Feb 2 '15 at 13:35
  • @markwilliams: Yes. It is from somebody who suggessted this on a different forum.. – Goldengirl Feb 2 '15 at 13:36
0

If you run the command as sh ip.sh, it will be run using sh, not with bash (making the shebang line pointless). Either run it with bash:

bash ip.sh

Or make it executable and run it directly:

./ip.sh

The traditional Bourne shell (sh) doesn't support the function keyword for function definition.


Aside from that, "echo $IP | grep -o ." doesn't run that command, it simply is that string. You need to do:

for i in $(echo $IP | grep -o .)

It might be easier to read in one character at a time:

hostname -I | while read -n1 i
do
    hx=$(hex $i)
    i2cset -y 1 0x3e 0x40 $hx
done
  • @NamitaRaju please edit your question to add more information. As you can see, that comment is barely readable. – muru Feb 2 '15 at 13:48
  • I have edited the original question. Sorry for the unreadable comment previously! – Goldengirl Feb 2 '15 at 13:55
  • @NamitaRaju see update. – muru Feb 2 '15 at 14:13
  • I put in the code that you suggested so that the characters are read one at a time.. But now it gives me an error saying : ,try1.sh: line 12: syntax error near unexpected token hostname', try1.sh: line 12: hostname -I | while read -n1 i'. – Goldengirl Feb 2 '15 at 14:27
  • @NamitaRaju can you post the updated script? – muru Feb 2 '15 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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