1

I am working on a project and have an application shell script. For function part, I need the GPIO access, and for that, I am using the standard wiringpi package.

The issue I am getting is with the #!/bin/bash vs #!/bin/sh use

I have been using the #!/bin/bash in past in conjunction with the wiringpi function - Raspberry Pi A+

but now when I have switched to the Raspberry Pi Jessie buster July 2019, I am having an issue running the wiring Pi function

for example

gpio mode 21 out
gpio -g write 21 0

working fine inside script if I use #!/bin/sh

but I get warnings for the associative array

Warning:

./dataLog.sh: 292: ./dataLog.sh: declare: not found
./dataLog.sh: 295: ./dataLog.sh: Bad substitution

Code:

Line 291: declare -A status="($(upsc myups | sed 's/\(.*\):\(.*\)/[\1]="\2"/'))"

Line 295: if [[ ${status[device.model]} != "" ]] ; then

if I use the #!/bin/bash, I do not get any error related to the associative array, but now my GPIO is not working.

I am trying to find the solution to the issue - Where I can use #!/bin/bash shell and wiringpi functions together

Thank you

| improve this question | | | | |
  • I can't see any relevance to wiringPi or the Raspberry Pi. his seems to be a general script problem. – joan Sep 17 '19 at 14:55
  • Yes, but I can see how the OP might not be able determine that, since there is the mystery of gpio not working in one version. Which is a real mystery. – goldilocks Sep 17 '19 at 14:57
  • @joan The issue is that I was using the same script with the minibian buit, and never had any issue – Kunal Sonone Sep 17 '19 at 15:01
  • Kunal, you need to clarify exactly what you mean by "not working". How are you running the script? Are there any errors, or is this deduction based on the physical behaviour? If the latter, exactly what is that (LEDs, etc)? – goldilocks Sep 17 '19 at 15:01
  • now I have upgraded to the Jessie img and now the same script breaks – Kunal Sonone Sep 17 '19 at 15:02
3

Finally, after fiddling around I am able to resolve the issue.

As per the WiringPi setup guide to set the direction of a gpio u should use

    gpio mode 21 out

This method works if you are using #!/bin/sh shell

But if you want to use #!/bin/bash shell then you have to use

    gpio -g mode 21 out

After, setting up the direction with use of '-g' I am able to control the GPIOs

| improve this answer | | | | |
  • In other words this question has nothing to do with bash! gpio mode 21 out would set BCM 5. It is a simple failure to read the documentation. – Milliways Sep 18 '19 at 23:58
  • Thank you for letting me know. – Kunal Sonone Sep 19 '19 at 12:28
  • Official Guide States : gpio -g … The optional -g flag causes pin numbers to be interpreted as BCM_GPIO pin numbers rather than standard wiringPi pin numbers. And it is true for GPIO R/W, the document does not have anything about the MODE – Kunal Sonone Sep 19 '19 at 12:31
2

Check out this post on the difference between sh and bash:

https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash

In summary of key points, sh is really a standard and bash was originally an implementation of that standard that evolved to have a lot of extensions, some of which caused it to deviate from the standard.

When you do #!/bin/sh that usually actually directs you to a bash shell, but that's implementation / configuration dependent on your flavor of Linux. (Note the recent trend toward having sh point to dash in the answer at the link.) Even when it directs to bash, however, bash may know that you called it as sh and restrict access to the extensions.

So bottom line is that sh and bash are not equivalent and they may differ in different ways depending on the exact version of Linux that you have. If you are using bash extensions, then specify that you need bash. (This seems to be your case.)

| improve this answer | | | | |
  • So far the search leads me to the conclusion that the "wiringpi" can only be accessed through the sh shell. Which is making it difficult for me to use the bash functions, which I need for batter part of my script – Kunal Sonone Sep 17 '19 at 16:46
  • I don't know if WiringPi does or does not work from various shells, but the premise of your question seems to be wrong to the extent that you seemed to expect different shells to have the same behavior. From what I understand in your question, it doesn't seem to be working for you properly in either shell, but it's giving you different symptoms in the two cases. – Brick Sep 17 '19 at 16:51
  • I have to edit my question, I want to resolve the issue but not sure about the solution I can use – Kunal Sonone Sep 17 '19 at 16:54
  • As of last month, WiringPi is no longer supported: wiringpi.com/wiringpi-deprecated. You can use this answer as a clue to debug what's happening under the hood, but if some script in there is using extensions (bash or otherwise), then you may need to sort that out yourself. Else post more details, Else use something other than WiringPi. – Brick Sep 17 '19 at 17:12
  • 1
    "When you do #!/bin/sh that usually actually directs you to a bash shell" that used to be the case, but nowadays Debian-based distros use dash as the default /bin/sh implementation. – Peter Green Sep 18 '19 at 15:35

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.