Is there a way to organize bash_profile across different platforms

Discussion in 'Programming/Scripts' started by John_Peter, Mar 14, 2014.

  1. John_Peter

    John_Peter New Member

    I want to have one .bash_profile works on multiple platform, ubuntu, debian, redhat, cygwin, osx. So how should I organize .bash_profile?
    It can be multiple files in some subdir

    Let me brief you:
    what i want is a way to organize bash_profile across platforms so I can use one set of profiles and use everywhere, e.g., how to identify different platforms.

    Please help me.
     
  2. LGM

    LGM New Member

    bash if, else and case statements

    Hi,

    You could simply use a mix of if, else and case statements in order to group the commands or environment variables for each of your system distro & version types. There is probably a better way to do this, this just happens to be the approach I thought of off the cuff without RTFM.

    Each distribution has a file in /etc that identifies the distribution and the version. I'm currently running Mint 16, which is built on Ubuntu 13, which in turn is built upon Debian 7.

    The Debian version can found in the file /etc/debian-version, which in my case contains, "wheezy/sid"

    The Ubuntu version can be found in /etc/os-release, the first 2 lines of mine contain:
    "NAME="Ubuntu"
    VERSION="13.10, Saucy Salamander"

    Linux Mint appears in a number of files in /etc (I'm not sure which ones are re-used by different distributions, so in this case I'll stick to the one I know is Mint only), /etc/linuxmint/info, the first 2 lines are:
    "RELEASE=16
    CODENAME=petra"

    So if we pretend we had 3 different types of systems, Debian, Ubuntu & Mint. As Mint is built on Ubuntu and Ubuntu is built on Debian, we'd need to check for Mint first, then Ubuntu, lastly Debian.

    So the distro specific section would look be like (but of course doing this for Mint, Ubuntu and Debian doesn't make a lot of sense as the file placement and setup should be basically the same):

    Code:
    # example non-working code segment for bash_profile so a single file can be setup to service multiple distros
    # LGM March 2014
    
    # Mint
    if [[ -r /etc/linuxmint/info ]]; then
    
    	# Mint specific, version independent bash_profile commands
    	# <insert dist specfic environment vars and commands here>
    	
    	# Get Mint version
    	ver=`grep -o "RELEASE=.*" /etc/linuxmint/info | sed 's/RELEASE=\(.*\)/\1/'`
    	
    	# Mint version
    	case $ver in
    	16)
    		# Mint version 16
    		
    		# <insert dist & version specfic environment vars and commands here>
    		;;
    	15)
    		# Mint version 15
    		
    		# <insert dist & version specfic environment vars and commands here>
    		;;
    	*)
    		# Mint any other version
    		
    		;;
    	esac
    	
    # Ubuntu
    elif [[ -r /etc/os-release ]]; then
    	
    	# Ubuntu specific bash_profile
    	# <insert dist specfic environment vars and commands here>
    	
    	# <alt. add commands to extract version from file & case statement as above>
    	
    # Debian
    elif [[ -r /etc/debian-version ]]; then
    	
    	# Debian specific bash profile
    	# <insert dist specfic environment vars and commands here>
    	
    	# <alt. add commands to extract version from file & case statement as above>
    		
    fi
    Note:
    - This is not a complete bash_profile, simply an example code block that could be used to setup up different environment variables based on distribution and version.

    - This is a non-working example only (and not necessarily a good one) and will need to be modified to suit your distribution and version/s
     

Share This Page