How can I check if the Operating System is Sun Os or Unix or Solaris from by bashrc - bash

I want to know, how can we check in the bashrc file, what operating system is it?

Use uname -a in your .bashrc file.

There is no portable way to know what Operating System is running.
Depending on the OS, uname -s will tell you what kernel you are running but not necessarily what OS.
Moreover, it cannot be SunOS or Unix or Solaris. Solaris is all of them: both a Unix compliant OS and an OS based on the SunOS kernel.
One of these might give you a precise answer depending on the Unix or Linux implementation:
cat /etc/release # SVR4, Solaris
cat /etc/redhat-release
cat /etc/*elease
cat /etc/lsb-release
oslevel -r # AIX
system_profiler -detailLevel -2 # Mac OS/X

Try this:
$cat /etc/os-release
NAME="Ubuntu"
VERSION="12.04.4 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.4 LTS)"
VERSION_ID="12.04"

Related

bash --version. Where this info are stored?

In which file are stored the info about the command
bash --version
?
I've tried in some directories like /bin or /var, /etc, but without results...
I need both for Linux and MAC OS x. Thanks!
Do you have any reason to believe that it is not hard-coded in the bash-executable itself?
To find out which bash is used, run
which bash
For me, it gives /bin/bash. Now, to see whether the executable has any string-like byte sequences in it that contain the substring "version", run
strings /bin/bash | grep version
On this machine, it gives:
shell_version_string
build_version
sccs_version
rl_library_version
rl_do_lowercase_version
show_shell_version
rl_readline_version
dist_version
GNU bash, version %s-(%s)
GNU bash, version %s (%s)
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
#(#)Bash version 4.3.48(1) release GNU
display-shell-version
-l do not print tilde-prefixed versions of directories relative
HOSTTYPE The type of CPU this version of Bash is running under.
OSTYPE The version of Unix this version of Bash is running on.
version, type `enable -n test'.
do-lowercase-version
.gnu.version
.gnu.version_r
Thus, "Bash version 4.3.48" is hard-coded in the executable as an ordinary string.
strings $(which bash) | grep "Bash version"
Output (e.g.):
#(#)Bash version 4.3.48(1) release GNU

How portable is mktemp(1)?

As the title suggests — can I be reasonably sure that mktemp will exist on any unix-y operating system I'm likely to encounter?
POSIX does not seem to specify mktemp(1).
It looks like most modern systems have it, but the available functionality and the semantics of the options vary between implementations (so particular invocations may not be portable):
mktemp(1) from OpenBSD — mktemp(1) originated in OpenBSD 2.1
mktemp(1) from FreeBSD
mktemp(1) from Mac OS X — almost always the same as from FreeBSD
mktemp(1) from Todd C. Miller of sudo fame
mktemp(1) from Solaris
mktemp(1) from GNU coreutils
mktemp(1) from HP/UX — this one seems particularly divergent from most of the others listed here
So if you want a portable solution you may need to stick to functionality and options that mean the same thing on all of your platforms of interest.
A mktemp function (AKA mktemp(3)) first appeared in Unix V7 so it's likely to be everywhere. However, a mktemp command (aka mktemp(1)) first appeared, I believe, on OpenBSD 2.1, so if you have to deal with truly antediluvian Unix systems you might have to worry -- unless you can distribute the very portable mktemp.org version (to fix the potential lack of this utility on some customer's antediluvian system). How likely you are to encounter antediluvian system is nigh impossible for us to guess, of course -- e.g., in HP-UX, mktemp(1) has been around for at least 8 years (even most enterprises probably have updated their Unix OS's within that time frame), in Xenix I believe it appeared in 3.0 (in 1992), etc, etc.
FYI, mktemp appears to NOT be included with Solaris 9 (released 2002/2003) - just ran across this today:
$ uname -a
SunOS dcmnapp02 5.9 Generic_122300-47 sun4u sparc SUNW,Sun-Fire-V440
$ mktemp
bash: mktemp: command not found
$ man mktemp
bash-2.05$ man mktemp
Reformatting page. Please Wait... done
Standard C Library Functions mktemp(3C)
NAME
mktemp - make a unique file name
SYNOPSIS
#include
char *mktemp(char *template);
On Solaris 9 it's in package SMCmktemp, see http://sunfreeware.com/indexsparc9.html:
uname -s
SunOS
uname -r
5.9
/usr/sbin/pkgchk -l -p /usr/local/bin/mktemp
Pathname: /usr/local/bin/mktemp
Type: regular file
Expected mode: 0555
Expected owner: bin
Expected group: bin
Expected file size (bytes): 8884
Expected sum(1) of contents: 6493
Expected last modification: Nov 05 08:48:17 2002
Referenced by the following packages:
SMCmktemp
Current status: installed

Discovery of Dynamic library dependency on Mac OS & Linux

On Windows there is a tool Depends.exe to discover dependency of an EXE/DLL file on other DDLs. Which commandline tool is equivalent on Mac OS and Linux?
Mac OS X: otool -L file
Linux: ldd file
If those commands don't provide what you want, on Mac OS X you can dump all the load commands with otool -l file. On Linux you can dump the entire contents of the dynamic section with readelf -d file.
You can also try MacDependency (https://github.com/kwin/macdependency) which provides an UI replacement for otool on MacOS X. It shows complete dependency trees and the exported symbols as well.
try ldd in the terminal. This will provide you a list of dynamic libraries that the binary needs.
You can put something like following into your bashrc so that you can always use "ldd" as interface but it will redirect macos equivalent one if machine is mac.
# Macos equivalent of ldd
if [[ "$OSTYPE" =~ "darwin"* ]]
then
alias ldd="otool -L"
fi

Equivalent of strace -feopen < command > on mac os X

This is useful for debugging (hence programming related). On linux, we can use the command
strace -feopen python myfile.py
to figure out which python modules and shared objects are loaded. Is there an equivalent one-liner on macOS X?
I suppose you meant strace -fetrace=open?
dtruss -f -t open python myfile.py

How can a Perl script know its own memory footprint?

I have a long running Perl script and I'd like to let it know (and report) how much memory it is using. I'd like to have this information both on Linux and Windows and if possible on Mac OS X as well.
These Perl modules could help you:
Windows: Win32::Process::Memory
Linux(and maybe Mac OSX): Linux::Smaps
This will show you how:
http://perldoc.perl.org/Devel/Peek.html
Also, http://perldoc.perl.org/perlguts.html
and, man pages for perldebug and perldebguts.
This is a quick and dirty and most of all CPAN-free method. It works on any OS that provides a /proc file system, that is Linux and Unix derivates, including Mac OS X, and also on Cygwin under Windows:
perl -e 'print qx{ grep VmSize /proc/$$/status };'

Resources