0

How do I make something like:

!#/bin/bash
if (system name == Jessie)
#script
if (system name == Wheezy)
#script
else
echo "It won't work on your Raspberry Pi version, please update"

if (kernel version == >= 4.4)
#script
if (kernel version == <= 4.4)
#script
else
echo "Bad kernel version"
3

You can try something like this. You may have to enhance this but the below script will work for you syntactically and also will tell which files to lookup and verify.

#!/usr/bin/bash

debian_version=`egrep "jessie|wheezy" /etc/os-release`
if [ `echo $debian_version|grep -c "jessie"` == 1 ]
then
   echo "jesse"
elif [ `echo $debian_version|grep -c "wheezy"` == 1 ] 
then
   echo "wheezy"
else
   echo "Some other version"
fi 


kernel_version=`uname -a|awk '{print $3}'`
if [ $kernel_version == "4.4" ]
then
    echo "version is 4.4"
fi
  • If your shebang is specifically bash (and not sh, since this is a bashism and not standard shell speak), you can use if [[ "$debian_version" =~ "jessie" ]] instead of that echo -> grep pipeline. Note the double brackets. Won't it be wheezy, BTW? – goldilocks Aug 30 '16 at 12:41
  • @goldilocks . That's cool. I have used matching with the operator '~' when I used to write scripts in awk. I never knew this could be used like this in shell scripts. Can you please go ahead and correct. Always the best option should be in posts is what I believe. – Varad A G Aug 30 '16 at 12:45
  • 2
    I corrected the "wheeze" to "wheezy" since I'm pretty sure that's wrong. The other bit I'll leave up to the reader; seeing multiple ways of doing something doesn't hurt and yours is safer if someone is doing this with !#/bin/sh (bash will behave differently when invoked as sh, and on Raspbian sh is actually linked to dash, not bash). – goldilocks Aug 30 '16 at 12:49
  • What -eq in $kernel_version -eq "4.4" means? – k073l Aug 30 '16 at 16:58
  • @k073l In shell script while comparing integers you need to use -eq, -ne -gt. and "==" is used for comparing strings. – Varad A G Aug 30 '16 at 17:02
4

Here's something which takes a different twist on /etc/os-release, which has a standardized format based around setting shell variables, meaning you can source it and use them. First have a look at the actual file to get the idea, then consider:

#!/bin/bash

source /etc/os-release

case $VERSION_ID in
"7")
    echo "Raspbian 7 is wheezy."
    ;;
"8")
    echo "Raspbian 8 is jessie."
    ;;
"9")
    echo "Raspbian 9 is stretch."
    ;;
*)
    echo "This is $NAME, not Raspbian."
    ;;
esac

It doesn't include anything about the kernel for which parsing uname -r (or /proc/version) is more appropriate. Also, it could be made more robust by actually checking $NAME to make sure this isn't some other Debian/Raspbian derived distro using the same version numbering (although what you are doing will likely work with them anyway as they are parallel, which is why there's a Debian 6 but no Raspbian 6).

BTW stay away from Raspbian 9, it is still considered "testing".

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.