LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2010, 03:14 PM   #1
sudox
LQ Newbie
 
Registered: Jul 2010
Distribution: Slackware 13.1
Posts: 26

Rep: Reputation: 0
Exclamation Scripting with BASH


I have written a query of commands that I made executable with chmod +x, but this is an old version of what I'm writing. I'm in need of a way to determine the Operating System the user is using so that the script will run yum or apt-get on the right OS's. Any ideas would be much appreciated.
 
Old 08-24-2010, 03:23 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,669

Rep: Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316
While I'm sure subsequent posters will provide better solutions, the following will work:

Code:
if [ -x /usr/sbin/yum]
then
    # do stuff
elif [ -x /usr/sbin/apt-get ]
    # do stuff
else
    # error message
fi

Last edited by dugan; 08-24-2010 at 03:34 PM.
 
Old 08-24-2010, 03:52 PM   #3
sudox
LQ Newbie
 
Registered: Jul 2010
Distribution: Slackware 13.1
Posts: 26

Original Poster
Rep: Reputation: 0
Isn't that Bourne Shell scripting?

Last edited by sudox; 08-24-2010 at 03:54 PM.
 
Old 08-24-2010, 03:59 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,669

Rep: Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316Reputation: 4316
Yes it is.
 
Old 08-24-2010, 04:08 PM   #5
sudox
LQ Newbie
 
Registered: Jul 2010
Distribution: Slackware 13.1
Posts: 26

Original Poster
Rep: Reputation: 0
I think I'm just going to revise my old code, until I get a book on BASH or SH
 
Old 08-24-2010, 07:12 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
Look at this:
http://mywiki.wooledge.org/BashGuide
 
Old 08-24-2010, 09:48 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The rkh_dat_get_os_info() function from the rkhunter source is a good model.
 
1 members found this post helpful.
Old 08-25-2010, 04:53 PM   #8
X.Cyclop
Member
 
Registered: Jun 2006
Location: Tlv
Distribution: Arch!
Posts: 120

Rep: Reputation: 21
Code:
#!/usr/bin/env bash
# Detect distro...

DETECTOS=$(grep 'DISTRIB_ID' /etc/*release | sed 's/DISTRIB_ID=//' | head -1)
if [ "$DETECTOS" == "Ubuntu" ]; then
    #apt-get
elif [ "$DETECTOS" == "Suse" ]; then
    #yum
else
    echo -n "Error"
fi

Last edited by X.Cyclop; 08-25-2010 at 04:57 PM.
 
Old 08-26-2010, 09:54 AM   #9
sudox
LQ Newbie
 
Registered: Jul 2010
Distribution: Slackware 13.1
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by X.Cyclop View Post
Code:
#!/usr/bin/env bash
# Detect distro...

DETECTOS=$(grep 'DISTRIB_ID' /etc/*release | sed 's/DISTRIB_ID=//' | head -1)
if [ "$DETECTOS" == "Ubuntu" ]; then
    #apt-get
elif [ "$DETECTOS" == "Suse" ]; then
    #yum
else
    echo -n "Error"
fi
I think I'm going to use that, great script! Thanks!
 
Old 08-26-2010, 04:04 PM   #10
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by X.Cyclop View Post
Code:
#!/usr/bin/env bash
# Detect distro...

DETECTOS=$(grep 'DISTRIB_ID' /etc/*release | sed 's/DISTRIB_ID=//' | head -1)
if [ "$DETECTOS" == "Ubuntu" ]; then
    #apt-get
elif [ "$DETECTOS" == "Suse" ]; then
    #yum
else
    echo -n "Error"
fi
You may wish to note that SuSE's use of yum has been a bit intermittent (& this may not be an issue for you, depending on how far into industrial archaeology you have to go):
  • sometime around 9.3 or so, yum became an option for package management
  • sometime around 10.1 or so, zypper became the preferred option for command-line package mangement (but yum was still supported as an option)
  • as far as I am aware, yum isn't even installed by default, these days, but maybe it is still available in repos, if you need backward compatibility
(and I can't even guess the corresponding SLES/SLED versions)

If you only have to cope with modern openSUSE versions, you can probably get away with only dealing with zypper, but if you have to deal with versions going back a bit, it might get messy.

(BTW, distrowatch has a useful guide on comparable package manager commands for different package managers, but don't look there for 'which suse version uses what; their otherwise useful data lists package manager as 'rpm' which isn't all that helpful.)
 
Old 08-26-2010, 06:08 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by catkin View Post
The rkh_dat_get_os_info() function from the rkhunter source is a good model.
@sudox: did you look at the rkh_dat_get_os_info() function? It shows that distros use several files other than /etc/release to store their ID information. If you are programming for Ubuntu and Suse alone that does not matter; if you want to program fully generically then the rkh_dat_get_os_info() function shows how much more must be scripted than the solution worked up in this thread.

If you want to program generically but only for systems using aptitude or yum then an easier alternative might be to look for aptitude and yum executables rather than identifying the distro.
 
Old 10-15-2010, 08:43 AM   #12
swirve
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
Does't work for all distros (OS X at the least is left out). Linking back to the other thread on this

http://www.linuxquestions.org/questi...339/page1.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Scripting Help? jhyland87 Programming 7 12-14-2008 11:58 AM
bash scripting ZAMO Linux - General 1 12-04-2008 02:53 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Bash scripting help dreidu Linux - Newbie 9 09-08-2008 11:07 AM
BASH If-then-else Scripting Help xianzai Programming 4 10-29-2004 04:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration