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: 3299
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
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

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

tony@orangepi ~ $ uname -o
GNU/Linux
tony@orangepi ~ $ uname -r
3.6.11+
tony@orangepi ~ $ 

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

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: 4249
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: 5959
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

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

pi@Pi3:~ $ cat /proc/device-tree/model
Raspberry Pi 3 Model B Plus Rev 1.3

LewisCowles1986
Posts: 13
Joined: Fri Mar 18, 2016 9:24 pm

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

Sun May 31, 2020 9:05 pm

2020 Update Here. It's looking like I'm going to have to implement quirks for some software on rpi4 to overcome aarch64 binaries failing to run with
"No such file or directory"

Code: Select all

egrep -q "BCM(283(5|6|7)|270(8|9)|2711)" /proc/cpuinfo
if
$?
is zero once this has run, then you should be on a Pi or compatible SoC

trejan
Posts: 2342
Joined: Tue Jul 02, 2019 2:28 pm

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

Sun May 31, 2020 9:17 pm

LewisCowles1986 wrote:
Sun May 31, 2020 9:05 pm
It's looking like I'm going to have to implement quirks for some software on rpi4 to overcome aarch64 binaries failing to run with
"No such file or directory"
Huh? Never seen that before.
LewisCowles1986 wrote:
Sun May 31, 2020 9:05 pm

Code: Select all

egrep -q "BCM(283(5|6|7)|270(8|9)|2711)" /proc/cpuinfo
if
$?
is zero once this has run, then you should be on a Pi or compatible SoC
The Linux kernel has shown only BCM2835 in /proc/cpuinfo for every Pi SoC for several years now.

User avatar
Milliways
Posts: 550
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

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

Mon Jun 01, 2020 1:38 am

Code: Select all

cat /proc/device-tree/model
should report model, which is easily parsed.


I use the following script to display useful info about Pi - it should work on most Linux OS (although some Raspbian specific entries don't work)
about

bls
Posts: 728
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

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

Mon Jun 01, 2020 2:46 am

You could also look at /etc/os-release. On the very latest 2020-05-27 release, /etc/os-release has

Code: Select all

rpi44~# cat /etc/os-release 
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
rpi44~# uname -a
Linux rpi44 4.19.118-v7l+ #1311 SMP Mon Apr 27 14:26:42 BST 2020 armv7l GNU/Linux
I'm guessing that some of these fields will change in a future release from "Raspbian" to something else, but for now, this should work. Most distros seem to have this file, and if it doesn't, you'll know that it's not Raspbian or or RaspiOS32.
Pi tools:
Free your network from your router's DHCP/DNS and run it on a Pi:https://github.com/gitbls/ndm
Quickly and easily build customized-just-for-you SD Cards: https://github.com/gitbls/sdm
Easy strongSwan VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

Return to “Other programming languages”