LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-06-2014, 04:51 PM   #1
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Rep: Reputation: 39
Is there a better way? (BASH Script)


I am writing up an installer script for myself. I am aiming this thing at Ubuntu / Mint at the moment but would like it to be easily portable with straight Debian or quickly modified to work with something non Debian based. I am trying to get it to test the distro. If it is ubuntu or mint then it runs the ppa function. if not then it just moves on down the list in the while loop running the functions.

Code:
testdistro()
{
	if cat /proc/version | grep [U,u]buntu ; then
		addppa
	elif cat /proc/version | grep [M,m]int ; then
		addppa
	fi
}
		
addppa() ###Called from testdistro###
{
	if [ ! -s $SOURCES.d/satyajit-happy* ] ; then
		add-apt-repository ppa:satyajit-happy/themes
	fi
}
Is there a better way of doing this? Smaller and more compact maybe?

Last edited by Tadaen; 02-06-2014 at 05:09 PM.
 
Old 02-06-2014, 06:36 PM   #2
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Original Poster
Rep: Reputation: 39
Found my answer using grep. If anyone needs...

Code:
if cat /proc/version | grep -iw 'word1\|word2' ; then
Whole function for posterity

Code:
addppa()
{
	if cat /proc/version | grep -iw 'mint\|ubuntu' >/dev/null ; then 
		if ! ls $SOURCES.d/satyajit-happy* >/dev/null ; then
			add-apt-repository ppa:satyajit-happy/themes
		fi
	fi
}

Last edited by Tadaen; 02-06-2014 at 06:53 PM.
 
Old 02-06-2014, 07:54 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
This is how I would do it
Code:
#!/bin/bash
unset PPA
PPA+=("satyajit-happy/themes")
PPA+=("anotherPPA/Main")
PPA+=("yappa/other")
# http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html # bash array intro
# http://mywiki.wooledge.org/BashGuide/Arrays
 
SOURCES="/etc/apt/sources.list.d"
 
# http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html
# 8.2 Functions with parameters
addppa() {
[[ -e "${SOURCES}/${1%/*}.list" ]] || echo add-apt-repository ppa:${1}
# http://tldp.org/LDP/abs/html/string-manipulation.html
# http://tldp.org/LDP/abs/html/list-cons.html
}
 
grep -qiw 'mint\|ubuntu' /proc/version && (
    for (( i=0;i<${#PPA[@]};i++));do
        addppa ${PPA[$i]}
    done
# http://tldp.org/LDP/abs/html/loops1.html # c style loop
# since arrays start at 0 it is easier to use that style
) || (
    echo "not mint or ubuntu"
    # extra stuff here
)
 
exit
note the list construct with the grep, && ( do when true) || (do when false )


PS
don't forget useless use of cat

https://startpage.com/do/search?q=useless+use+of+cat

Last edited by Firerat; 02-06-2014 at 07:57 PM.
 
Old 02-08-2014, 12:14 AM   #4
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Original Poster
Rep: Reputation: 39
Quote:
don't forget useless use of cat

https://startpage.com/do/search?q=useless+use+of+cat
Is this what suggesting? This...

Code:
if grep -iw 'word1\|word2' < /proc/version ; then
instead of

Code:
if cat /proc/version | grep -iw 'word1\|word2' ; then
Just to make sure I understand what the link sent me to.

Last edited by Tadaen; 02-08-2014 at 12:15 AM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to end the bash script using commands in bash not manually by pressing ctrl+c Sanpreet Singh Linux - Newbie 1 07-03-2013 01:04 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:50 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