Fedora Linux Support Community & Resources Center
  #1  
Old 25th October 2011, 05:47 PM
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Confoederatio Helvetica (Swissh)
Age: 34
Posts: 3,483
linuxfirefox
BASH: testing for distro / based on?

Heyas

Guess its time again to use more brainpower and experience
As i try to write my script collection to be able to use (many) diffrent distros, not only fedora, i face very diffrent syntax for some commands.. example.. yum, apt-get, pacman

My first attempt in getting soem basic system recognation, was to parse /etc for files named *-release or *-version.
That was semi successfull, all redhat based system worked well, as well as debian itself and arch.
However, Ubuntu and its derivates dont work the same way.
Well, in ubuntu i would find a debian-version, would would let me know i had to use apt-get rather than yum.
However, it doesnt tell me its ununtu, or which version of ubuntu.

lsb-release was another approach, but, since i want to run my scripts without installation required (yet it still needs), i'd have to implement some sort of checking if its available. and install it then 'accordingly' which results a problem, as i dont know which installation syntax is required.

Now to give you an impression of how confused i'm currently are regarding this topic, here are the current code segments: ( / refers to the script base, not the root dir)

/system/filesystem:
PHP Code:
(....)
seayum() {
    if 
isHelpy "$1" then echo $HelpYum && return fi
    desc
=$1
    pack
=$2
    
    
#isFedora 
    
    
if isFedora
        then seaaction 
"Installing" "sudo yum install" "$desc" "$optYum" "$pack&& return || echo "Fedora wrong system?"
    
elif isUbuntu
    then seaaction 
"Installing" "sudo apt-get install" "$desc" "$optAptGet" "$pack&& return || echo "Ubuntu wrong system?"
    
else seaecho "Cant examine system..." "Assuming ARCH"
        
seaaction "Installing" "sudo pacman" "$desc" "-S" "$pack&& return || echo "# | Cant handle this system"
    
fi

(.....) 

/system/release
PHP Code:
    redhatstyle=/etc/system-release
isFedora
() {
    if 
test -/etc/fedora-release
        then     
return 0
        
else    return 1
    fi
}

isUbuntu() {  # FAILS, obsiously if lsb-release is not isntalled
    
if [[ "$(release)" grep "buntu" ]]
        
then     return 
        
else    return 1
    fi 
}
release() {
    
    
getReleaseID() {    echo $(lsb_release -is) ; }
    
getReleaseNum(){     echo $(lsb_release -rs) ; }
    
getReleaseCN() {     echo $(lsb_release -cs) ; }
    
#Desc=$(getReleaseDescr)     >/dev/null 2>&1
    
Distro=$(getReleaseID)    >/dev/null 2>&1
    Ver
=$(getReleaseNum)     >/dev/null 2>&1
    CN
=$(getReleaseCN)        >/dev/null 2>&1
    
if test "" "$Distrotest "n/a" "$Distro"
        
then    output="$Distro " 
        
elif     -/etc/fedora-release
        then     cat 
/etc/fedora-release
            
return 0
        elif    
-/etc/system-release
        then    cat 
/etc/system-release
            
return 0
        elif     
[[ ! "$isUbuntu"1" ]]
        
then     output="Ubuntu "
            
return 0
        fi
    
if test "" "$Vertest "n/a" "$Ver"
        
then    output="$output$Ver " fi
    
if test "" "$CNtest "n/a" "$CN"
        
then    output="$output$CN " fi
    
if is64bit then output="$output(64 bit) " fi
    
echo $output
    
return 0
}
releaseVer() {
    echo $(
release) | grep -ow -[[:digit:]]

/system/sea.gui
PHP Code:
 # just while posting an idea came up, creating a file named..
#      a.sh or something, so this part *should* be read as first??
    
if ! [[ tmplsb=$(lsb_release -v) ]] ; then
      
echo "LSB-Release is missing, installing..."
      
test sudo --q lsb-release
      test sudo apt
-get install lsb-release
      test sudo pacman 
---noconfirm lsb-release
    fi
sleep 5 
I'm thankfull for every idea.
__________________
Laptop: Toshiba satellite p50-a-11 CPU: Intel i7 8*2400 MHz GPU: GeForce GT 745M RAM: 8192 MB Distro: Fedora (Rawhide) DE: Awesome
Text User Interface (TUI) // Windows 8+ & Fedora 20+ Dualboot
Reply With Quote
  #2  
Old 25th October 2011, 05:59 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,865
linuxfedorafirefox
Re: BASH: testing for distro / based on?

You could always test for the existence of yum - that should identify the RH based distributions right off.

Looking for apt-get should identify the debian family.

Granted, some systems have both installed, but these would likely be debian derivatives that also access some RH repositories.

A thought - you could also look at the repository files itself - if /etc/yum.repos.d exists AND fedora-repo exists, you have Fedora, and can parse the repository file to see if [fedora] is active. This might exist for source rpms, but that would be under [fedora-source] instead. Similarly a CentOS release would also have a repository file listed.

Similar tests for the other distributions should work too. A bit long perhaps, but more reliable than a nonexistent file.

Last edited by jpollard; 25th October 2011 at 06:05 PM.
Reply With Quote
  #3  
Old 25th October 2011, 06:41 PM
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Confoederatio Helvetica (Swissh)
Age: 34
Posts: 3,483
linuxfirefox
Re: BASH: testing for distro / based on?

Yes, but again, that checks actualy only rh (or fedora for the same reason).
Would there be another way to figure out what kind of system (or its base) it is?
Something thats not too much of the current distro (to name just a few: arch, deb, rh, slack) is focused.

When i first figured out that theres a /etc/system-release i thought that would be exactly my solution, until i figured out that this is only a RH based solution

Namely i ask if there is a linux wide way to identify a distro with as basic command (therefore distro-independent) as possible, or on the other hand, a (better) way to install lsb-release.
My current try doesnt work that well
Code:
    if ! [[ tmplsb=$(lsb_release -v) ]] ; then
      echo "LSB-Release is missing, installing..."
      test sudo -y -q lsb-release
      test sudo apt-get install lsb-release
      test sudo pacman -S --noconfirm lsb-release
    fi
__________________
Laptop: Toshiba satellite p50-a-11 CPU: Intel i7 8*2400 MHz GPU: GeForce GT 745M RAM: 8192 MB Distro: Fedora (Rawhide) DE: Awesome
Text User Interface (TUI) // Windows 8+ & Fedora 20+ Dualboot
Reply With Quote
  #4  
Old 25th October 2011, 07:08 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,865
linuxfedorafirefox
Re: BASH: testing for distro / based on?

Other distributions have different repository files to check.

My first suggestion was to look at the package utility itself. If yum exists you are RH. If apt-get exists you are debian, if pacman exists you are...

Yes, sometimes two exist at the same time, but only one of them is the primary source. yum uses /etc/yum.repos.d/xxx.repo, whether that is fedora or RHEL. And CentOS uses a different file.

One possibility is the uname, though I'm not sure how widespread that is.

Slackware for instance uses /etc/slackware-version.
Reply With Quote
  #5  
Old 7th November 2011, 01:06 PM
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Confoederatio Helvetica (Swissh)
Age: 34
Posts: 3,483
linuxfirefox
Re: BASH: testing for distro / based on?

Ok so far i have the 'release' working for redhat based distros.
Slackware and Arch are also recognized, but yet no handling has been written.

PHP Code:
#!/bin/bash
# ---------------------------------------------------------------------------------------
# | Project sea:    Package of small scripts for an easy set up of linux uses    |
# | Description:    Prints out your Linux release                    |
# | Script by:        Simon A. Erat (sea)                        |
# | Contact:        http://forums.fedoraforum.org/member.php?u=183882    (sea)    |
# | Date (created):    2011.08.02 (Year/Month/Day)                    |
# | Date (changed:    2011.11.07                            |
# | FileVersion:    0.6                                |
# | Resource:        http://forums.fedoraforum.org/showthread.php?t=270313        |
# |-------------------------------------------------------------------------------------|
# | sea Release routine
# |
    
oss="amiga beos freebsd cros linux debian fedora gentoo kateos knoppix kubuntu pclinuxos mandriva linxfromsource redhat slackware suse ubuntu mint arch linux macosx macppc netbsd sunos symbian unix windows macintosh "
    
foundos=false

release
() {    
    for 
curOS in $oss ; do
        
#printf $curOS.
        
if test -"/etc/$curOS-release"
          
then    foundos=true
              releasestring
=$(cat /etc/$curOS-release)
        
elif test -"/etc/$curOS-version"
          
then    foundos=true
              releasestring
=$(cat /etc/$curOS-version )
        else
           
foundos=false
        fi
        
if test $foundos true then 
            releasestring
=$(echo $releasestring sed  s/"release "/""/g)
            
bity=$(uname -m)
            
releasestring="$releasestring $bity"
            
echo $releasestring
            
break 
        
fi
    done
    
#DONE# :: Red Hat based, Arch, Slackware
    #TODO# :: Debian, gentoo, mint
    #DO# Ubuntu
    
if test $foundos false
    then    
# CHECK FOR LSB-RELEASE EXistence
          
foundLsb=$( find /bin /usr/bin -name lsb_release )
    if 
test "" "$foundLsb"then echo FOUND$foundLsb && sleep 1 fi
    
    
if ! [[ tmplsb=$( test lsb_release -v) ]] ; then
      
echo "LSB-Release is missing, installing..."
      
test sudo --q lsb-release
      test sudo apt
-get install lsb-release
      test sudo pacman 
---noconfirm lsb-release # && echo its arch && sleep 2
    
else
      echo 
fehler?
      
seayum "LSB-Release"    "lsb_release" || seayum "RedHat-Release" "redhat-release "
    
fi
        
echo "nothing found: $curOS"
    
fi

__________________
Laptop: Toshiba satellite p50-a-11 CPU: Intel i7 8*2400 MHz GPU: GeForce GT 745M RAM: 8192 MB Distro: Fedora (Rawhide) DE: Awesome
Text User Interface (TUI) // Windows 8+ & Fedora 20+ Dualboot

Last edited by sea; 7th November 2011 at 01:08 PM.
Reply With Quote
Reply

Tags
based, bash, distro, testing

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is Fedora about to become a KDE based distro? hlfmanhlfamzng Fedora Focus 32 25th September 2011 11:57 AM
How I can testing these F8-based kernels on FC7? PashaP Installation, Upgrades and Live Media 2 25th October 2007 10:25 PM
Fedora Based Distro The_Jester Using Fedora 1 26th July 2006 04:01 PM
New Fedora Based Distro Coming Hillbilly Fedora Focus 19 24th August 2004 06:56 PM
Fedora Based Distro Hillbilly Programming & Packaging 2 7th May 2004 08:52 PM


Current GMT-time: 06:43 (Thursday, 23-10-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Tres Pontas - Pico Rivera Travel Photos on Instagram - Al Manshah Photos