One command to tell if it is windows, osx or linux/unix - linux

Is there a way to determine the underlying OS platform just by running one command, irrespective of whether it is run on a command prompt in windows or a terminal (bash/sh/etc.) in OSX/Linux/Unix ?
Note: there may not be Python/Perl etc. or any such interpreters already present on the platform, so we can run any of these to figure out the same.

Short answer is "No", one command is not enough (at least not the same command). That's because environments are too different. You still can do this for OS X/Linux because they share same origin - Unix and thus uname -a would work. However Windows doesn't have this concept because it's not Unix-based. In windows you need to run another command to check version: winver
If anything, you can check host OS type with the help of come cross-platform tools, for example, some simple java utility or either of these Python oneliners:
python -c "import platform; print platform.platform()"
Gives more details
python -c "import platform; print platform.system()"
Gives only OS type

Related

Developing on pycharm with a chroot environment?

I have an Arch Linux Machine inside which I have "containers" like Centos which I normally chroot and work upon from the command line. I setup Pycharm to work on a project that is inside this "container" The "container" was created using LXC, but I normally login using chroot.
There is a problem running the interactive debugger from Pycharm however.
The command that is run to start the interactive debugger from Pycharm is:
/usr/lib/lxc/centos/rootfs/home/virtual-environment/usr/bin/python /home/nishant/packages/pycharm-community/helpers/pydev/pydevconsole.py
ERROR:root:code for hash md5 was not found
I was able to reproduce this problem without Pycharm.
If I chroot to that environment and then do usr/bin/ python -v -c "import hashlib" everything works.
If I execute /usr/lib/lxc/centos/rootfs/home/virtual-environment/usr/bin/python from my main system, the same error comes.
Fundamentally I think the problem is that I am executing the command in a wrong environment. I think Pycharm should not be executing this interpreter directly but instead in a chroot context.
Is it possible to achieve that using Pycharm?
In general, is it possible to execute a chroot command by not
doing a chroot, like setting the LDD path differently or something
like that?

Run "./" bash/batch file with cygwin

Well the idea goes as followed,
I have a bash file for linux, there I obviously run it by making ./my_run.
The problem is I'm in windows so I downloaded and installed cygwin.
I added cygwin bin to the Enviromental Variables and check that at least "ls" works so I guessed I did it well.
When I try to run it with the cmd it displays:
'.' is not recognized as an internal or external command,
operable program or batch file.
As if the cygwin variables were not correctly installed (as I said I tried ls and works).
Then I tried it directly with cygwin and when doing the ./my_run I got it to work right.
So how is that I can use some commands like ls but when doing ./ it doesn't work on the cmd? How can I fix this?
Well, cygwin is only a shared library and a lot of stuff (the programs) using it (read Cygwin doc). cygwin.dll changes internally path resolution / chars to allow you to say ./my_script and converts it to .\my_script before doing the actual windows call, it also adds the proper extension to executables to allow it to execute windows binaries. This magic persists as long as you use it. cmd.exe is a Microsoft Windows command shell that is completely unaware of Cygwin's shared library and by that reason it doesn't use it, so it will not call it for path translation, even if you populate the environment of zetabytes of stuff. When you run in Cygwin terminal, you are running bash shell, which is a Cygwin executable, linked to cygwin.dll. It manages to use Cygwin library for all the unix system call emulations, so when you pass it e.g. to exec("./my_script", ...);, it internally converts that to try for ./my_script, then .\my_script, ./my_script.exe, ... and the same for .com and .bat extensions.
This fact often makes some people to say that Cygwin is not a good, efficient, environment. But the purpose was not to be efficient (and it is, as it caches entries and makes things best to be efficient) but to be compatible.
In your example ls is a Cygwin executable that mimics the /bin/ls executable from unix systems. It uses the Cygwin library, so all path resolution will be properly made (well, under some constraints, as you'll see after some testing) and everything will work fine. But you cannot pretend all your Windows applications to suddenly transform themselves and begin working as if they where in a different environment. This requires some try and error approach that you have to try yourself. And read Cygwin documentation, it is very good and covers everything I've said here.
If you open up Cygwin and run the command there you should be fine.

Determine OS from a single command line operation

Introduction:
I have a 'magic' tool that can perform a command line operation on a machine if I provide the IP.
The tool knows the OS that machine is using and executes the command on cmd/shell based on whether it is windows/linux and returns the output of the command back blindly.
C:> tool.exe 172.140.56.2 "ipconfig"
Assumptions:
One OS per machine. Tool has no problem executing the command (whether it fails or not is a different problem)
The OS is either windows or linux always.
I determine the OS based on the command result
Problem:
Using this power of being able to execute a command, I want to determine the OS
My Solution:
Execute ipconfig command. If result is
-bash: ipconfig: command not found
It is linux.
Else if it is like this:
Windows IP Configuration
Ethernet adapter Local Area Connection:
...
then Windows.
Question:
I wanted to know if this is a foolproof way of doing this. I want a command which would not fail under certain scenarios. (say cygwin installed on windows allowing linux commands to succeed. Or ipconfig succeeding on linux under some special scneario.)
I can process the command output with some parser, if that helps in any way.
Just to clear any confusion. It can be ANY command. I ust used ipconfig in my example.
If I understood your problem correctly, then uname is the ideal command. If it's any Unix-system (including OSX), it'll return the correct variable, and if it's Windows it'll return command not found or similar.
Safest way to determine Linux/version is
cat /etc/*release
Sample output.
DISTRIB_ID=LinuxMint
DISTRIB_RELEASE=17
DISTRIB_CODENAME=qiana
DISTRIB_DESCRIPTION="Linux Mint 17 Qiana"
NAME="Ubuntu"
VERSION="14.04.1 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.1 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
Another command that should work is set, which displays name and value of each environment variable. Any shell on Linux supports it (although output is different between csh and bash). Output from a Windows system would have PROCESSOR_ARCHITECTURE and other standard variables in it (see https://ss64.com/nt/syntax-variables.html), which are very unlikely to be set in Linux.

I want to open git bash in a linux server using putty. How will I do that?

I am following this https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git and I am stack on step 5, I want to open git bash in a linux server using putty. How will I do that? In windows, there is gitbash cmd desktop app. But in linux how will you get from normal command line to git bash?
Note: I am using putty to access the linux server. If there is other way also, please recommend me.
“Git bash” under Windows simulates a Linux environment (it uses MinGW behind the scenes, afaik).
Under a Linux distribution, you just have to have a version of Git installed. With a Debian based distribution you would for example run sudo apt-get install git (or git-core for older releases) on the command line.
Once installed, you can use it from the command line without further ado.
Actually Windows gitbash is an emulation of a bash command interpreter designed for UNIX systems. On many Linux distributives bash is a default command shell. So, when you're connecting to a Linux via putty, you're actually entering a bash shell. There's nothing more you need. You can use it the similar way as you're using windows gitbash. In a nutshell, just omit the first item in the spet 5 of your tutorial: you probably already in the bash shell.
To check out which shell you're using, run this command: ps -p $$. It will output something like this:
PID TTY TIME CMD
10967 ttys000 0:01.68 -bash
The CMD field is the shell name you're using.

How to detect if my server is running centos or other from a perl script

I want to display some text in a script only if the Operating System is Centos .
How can i do that in a perl script ?
To answer your exact question, you can identify CentOS by reading the contents of /etc/redhat-release. E.g.
$ cat /etc/redhat-release
CentOS release 5.9 (Final)
As other commenters have made clear, it is better to depend on the exact OS features you want, or write code to be portable, rather than limiting it to a particular distribution of Linux.
Try $^O. It contains the OS that was used to build your version of Perl. Here's what perlvar has to say about it.
The name of the operating system under which this copy of Perl was
built, as determined during the configuration process. For examples
see PLATFORMS in perlport. The value is identical to $Config{'osname'}
. See also Config and the -V command-line switch documented in
perlrun. In Windows platforms, $^O is not very helpful: since it is
always MSWin32 , it doesn't tell the difference between
95/98/ME/NT/2000/XP/CE/.NET. Use Win32::GetOSName() or
Win32::GetOSVersion() (see Win32 and perlport) to distinguish between
the variants. This variable was added in Perl 5.003.
Also see perlport.

Resources