How to let the .bashrc know the current operation system

1

I have several linux computers sharing one disk and one home folder.

That is on the disk, there is the home folder: \home\wuciawe

And for computer A, whose os is Centos, it will load \home\wuciawe\.bashrc

For computer B, whose os is Ubuntu, it will load \home\wuciawe\.bashrc, same with computer A.

Because I am not the Admin, I build and install some software locally on Ubuntu, and add something to the Path in \home\wuciawe\.bashrc.

Due to some reason, something added to the Path draw a contradiction with Centos.

I wonder if there is a way to let the .bashrc know the current system, do something like follows:

if os is Ubuntu:
  Path = xxx:$PATH
  export PATH
endif

宇宙人

Posted 2015-01-08T06:39:55.993

Reputation: 147

You can use the information here: http://unix.stackexchange.com/questions/35183/how-do-i-identify-which-linux-distro-is-running I wouldn't actually bother trying to parse these though. You should be able to simply check for the existence of files like /etc/redhat-release to determine if it is Red Hat, for example.

– krowe – 2015-01-08T06:55:24.380

1I normally have scripts which are hostname specific. e.g :

`case $HOSTNAME in pc1|pc1)`

as I cant find reliable way to find OS across all linux distros, mac + cygwin, – Neil Wightman – 2015-01-09T09:18:59.353

1@NeilWightman This method looks greater as it can specify different computers with same os. – 宇宙人 – 2015-01-09T09:48:54.170

1I normally use a combination of case $HOSTNAME and case $TERM for all the combinations I need. – Neil Wightman – 2015-01-09T09:52:52.067

Answers

0

Ubuntu

if grep -i Ubuntu /etc/lsb-release &>/dev/null
then
  export PATH=xxx:$PATH
fi

Notes

  • In shell, there is no colon at the end of an if statement.

  • In shell, you do not want spaces on either side of an equal sign in an assigment statement.

  • In shell, if statements are concluded with an fi statement

CentOS

There is an analogous solution for CentOS:

if grep -i CentOS /etc/centos-release &>/dev/null
then
  export PATH=xxx:$PATH
fi

John1024

Posted 2015-01-08T06:39:55.993

Reputation: 13 893

The release file may not reside at /etc/lsb-release. On RH-like systems it's /etc/redhat-release. – None – 2015-01-08T07:15:53.047

@CongMa Very true. If you are interested in more general solutions that handle a variety of distributions, see, for example, this post. The simple code above was intended to answer the OP's specific question about a test for Ubuntu.

– John1024 – 2015-01-08T07:44:06.880

The OP seems to want something for both CentOS and Ubuntu. In that case, it would be fine just checking both paths. – None – 2015-01-08T07:49:53.750

1

You could try grepping the /etc/issue file for OS names. The file's content is used by getty to display a pre-login message. From the man page of issue(5):

The file /etc/issue is a text file which contains a message or system identification to be printed before the login prompt. It may contain various @char and \char sequences, if supported by the getty-type program employed on the system.

Please see @John2014's post about how to grep it in a shell script.

user319088

Posted 2015-01-08T06:39:55.993

Reputation:

since John's answer is complete, I accept his. And as well, I vote for your answer because it's more general. – 宇宙人 – 2015-01-08T08:12:55.340

1

It is common advice to sysadmins, see here for example, to remove any identifying OS information from /etc/issue. So, use this approach with caution.

– John1024 – 2015-01-08T08:16:56.873