User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

BASH script to check for Pi hardware and/or Raspbian

Fri Feb 22, 2013 2:08 am

I have a few routines in my software that are very Pi specific, but I would like to make the rest of it okay for x86 Debian based systems too.

I can check for the arm hardware with:

Code: Select all

#! /bin/bash
WHAT_AM_I=$(uname -m)
echo "$WHAT_AM_I"
Which returns "armv6l".

Is that good enough to reliably determine we are running on a Pi?

I would also like to get more adventurous, and make my script work for different OSes on the Pi too - it is currently aimed at Raspbian, but if I can detect a different OS, then I can run routines specific to that OS (i.e. using pacman or yum instead of apt).

How can I reliably determine the OS breed. Preferably with a routine that will survive OS version number updates...

e.g. I'd like to create a variable who's contents are "Arch" or "Fedora" or "Raspbian" etc.

User avatar
jojopi
Posts: 3041
Joined: Tue Oct 11, 2011 8:38 pm

Re: BASH script to check for Pi hardware and/or Raspbian

Fri Feb 22, 2013 4:05 am

To check specifically for a Pi (or very authentic clone) look at the Hardware and Revision lines in /proc/cpuinfo. But "uname -m" is better if you just want i686/x86_64/arm*.

You might want to know if the user-space ARM binaries are compiled for hard float. "readelf -a /proc/self/exe |grep Tag_ABI_VFP_args" will be non-empty if readlink is.

Debian/Raspbian systems will have a file /etc/debian_version (giving 7.0 for wheezy). Fedora-based systems have a slightly more verbose /etc/redhat-release (actually a symlink to fedora-release, unless you are running Red Sleeve). Arch has an empty file /etc/arch-release.

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: BASH script to check for Pi hardware and/or Raspbian

Fri Feb 22, 2013 6:59 am

So massively incredibly helpful - thankyou!

User avatar
[email protected]
Posts: 1983
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: BASH script to check for Pi hardware and/or Raspbian

Fri Feb 22, 2013 10:30 am

I used this for a project recently:

grep -q BCM2708 /proc/cpuinfo

Seemed to work well enough to let my project build script know if it was on a Pi or not. (check the exit code)

-Gordon
--
Gordons projects: https://projects.drogon.net/

dr_d_gee
Posts: 84
Joined: Fri Jan 04, 2013 1:30 pm

Re: BASH script to check for Pi hardware and/or Raspbian

Fri Feb 22, 2013 3:05 pm

There are also other options to uname, as you may already be aware.

Code: Select all

uname -o
prints the operating system (I'm not on a Linux box at the moment so can't say exactly what it prints) and

Code: Select all

uname - r
prints the kernel release. There are others; see http://linux.about.com/library/cmd/blcmdl1_uname.htm (or other sources). I *think* the kernel release output usually prints the distribution name (among other things).

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: BASH script to check for Pi hardware and/or Raspbian

Fri Feb 22, 2013 9:42 pm

Thanks. For those playing at home, here is the output on a Pi.

Code: Select all

[email protected] ~ $ uname -o
GNU/Linux
[email protected] ~ $ uname -r
3.6.11+
[email protected] ~ $ 

User avatar
[email protected]
Posts: 1983
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: BASH script to check for Pi hardware and/or Raspbian

Fri Feb 22, 2013 9:44 pm

dr_d_gee wrote:There are also other options to uname, as you may already be aware.

Code: Select all

uname -o
prints the operating system (I'm not on a Linux box at the moment so can't say exactly what it prints) and

Code: Select all

uname - r
prints the kernel release. There are others; see http://linux.about.com/library/cmd/blcmdl1_uname.htm (or other sources). I *think* the kernel release output usually prints the distribution name (among other things).
I went through that too - I didn't find uname that enlightening - uname -o yeilds "GNU/Lunux" on a Pi, and the same on my x86 workstation.. -r isn't much help either - unless you're targetting a particular kernel release.

Code: Select all

uname -m
Might be the closest though - you get armv6l on the Pi and i686 in my workstation, however I also get x86_64 on a 64-bit machine I am also using in my build process for the same project which wasn't what I was after, so I stuck with grepping for the hardware type in /proc/cpuinfo

I'm not even sure if there is a standard way to determine if your on Debian, Raspbian, RedHat Centos, Ubuntu either.. I saw some scripts a while back that simple looked for various files in existance to try to work out what the distro was.

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
ukscone
Forum Moderator
Forum Moderator
Posts: 3959
Joined: Fri Jul 29, 2011 2:51 pm
Contact: Website

Re: BASH script to check for Pi hardware and/or Raspbian

Fri Feb 22, 2013 10:03 pm

you could try

cat /etc/*-release

and/or

python -c "import platform; print platform.dist()"


won't work everywhere & probably not too trustworthy but might be enough

ahundt
Posts: 1
Joined: Sun Aug 12, 2018 5:46 pm

Re: BASH script to check for Pi hardware and/or Raspbian

Sun Aug 12, 2018 5:49 pm

This isn't 100% bash, but python comes on the pi so this should work on the pi & I have a check if python exists so it should not run on anything that does not have python! So hopefully this is pretty cross platform compatible! worked for me, ymmv.

Code: Select all

# check if it is a raspberry pi, because we'll need a special ruby first
if [ -x "$(command -v python)" ] ; then
    R_PI=`python -c "import platform; print 'raspberrypi' in platform.uname()"`

    if [ "$R_PI" = "True" ] ; then
        # put your raspberry py code here, in my case I upgrade the ruby version:
        # run ruby upgrade script. source: https://gist.github.com/blacktm/8302741
        yes | bash <(curl -s https://gist.githubusercontent.com/blacktm/8302741/raw/install_ruby_rpi.sh)
    fi
fi

jahboater
Posts: 3043
Joined: Wed Feb 04, 2015 6:38 pm

Re: BASH script to check for Pi hardware and/or Raspbian

Mon Aug 13, 2018 7:13 am

Slightly OT, but you might also be interested in:

/proc/device-tree/model

which provides the full model name as a NUL terminated string.

Code: Select all

[email protected]:~ $ cat /proc/device-tree/model
Raspberry Pi 3 Model B Plus Rev 1.3

Return to “Other programming languages”