Why does the output of df -m $PWD|awk {'print $4'} differ in different versions of RHEL - linux

I am writing a script to check for a few things before installing a package. I need to check how much free space the filesystem containing the $PWD has. I wrote and tested the script on RHEL 7.0. I used df -m $PWD|awk {'print $4'} to get the available free space. This works fine for RHEL 7.0. But in RHEL 6.4 and 7.1, this does not return the free space in MB, but the free % available space on the filesystem. Visually they look the same. I see that the version of the df command is different from df --version. This script is to be used across a large variety of RHEL systems. What can be a workaorund to this?

Use -P with df command
-P, --portability
use the POSIX output format
df -m -P $PWD| awk {'print $4'}

Related

Why am I not able to use -o or --format with ps command to control the output format?

I want to print certain columns only from ps output that is PID, PPID, command, memory utilization and CPU utilization columns.
when I run ps command I get the following output.
Now I only want some columns from this output so I use -o flag as mentioned in this tutorial.
But I am getting this error.
I don't understand where is the problem. I have also tried usin --help and it is not showing -o flag. So I am confused here.
I am using the windows operating system. And using Git Bash terminal to run all these Linux commands.
Git Bash is a terminal for Windows that emulates the Linux bash (shell) functionality. It is not 100% compatible to a "real" bash shell. As you've empirically seen, its ps executable doesn't support all the flags you're used to from Linux. The --help option will show you what flags are supported.
Hello
Maybe put 2 things together, ps and grep? Then try this...
ps | grep -o -E "^[ 0-9]{1,9}"
...and is this working on your system?
( The Space in [ ] is important )

How can I split a file by pattern on GNU linux? (split command does not have a -p flag on GNU as it does on BSD)

Background: I have a very large .sql file, which causes a timeout when I import it into my MySQL server. To fix this in Mac OS X, i run: split -p 'DROP TABLE IF EXISTS' my-backup-file.sql which causes a series of smaller files which then don't cause the timeout.
My issue is:
split -p 'pattern' my-backup-file.sql works fine on Mac OS but not on GNU linux such as Ubuntu as far as I understand.
I cannot run something like `docker run -v $(pwd):/workspace some/freebsdimage /bin/bash -c 'cd /workspace && split -p pattern my-backup-file.sql' because I can't run a freebsd docker image on a non-freebsd docker host.
What alternative is there in Ubuntu to split a file into smaller files every time a pattern occurs?
The answer which works for me, as #Mark Plotnick states above, is
csplit -k inputfile '/pattern/' '{99999}'

Difference between GNU grep and AIX Unix grep command options

What is the difference between grep command available on Linux (GNU grep ) and the one available on IBM AIX ?
For eg: -m and -A options are not supported in IBM AIX.
This will be helpful in writing portable shell scripts. Also it would be helpful is someone can list unsupported options on Solaris too.
N means option NOT available. It include Solaris version too.

Equivalent lsof -i in Solaris

I have a fast question. I want to know what is the losf -i equivalent command in a Solaris system.
I only want to show the files with network connection.
Thank you!!
Here is a shell script that list all processes having open TCP or UDP ports on Solaris, you can limit it to a given port number by passing it as an argument:
pfiles /proc/* 2>/dev/null | nawk -v port=$1 '
/^[0-9]/ { cmd=$2; type="unknown"; continue }
$1 == "SOCK_STREAM" { type="tcp" }
$1 == "SOCK_DGRAM" { type="udp" }
$2 ~ "AF_INET" { if((port!="")&&($5!=port)) continue;
if(cmd!="") { printf("%s\n",cmd); cmd="" }
printf(" %s:%s/%s\n",$3,$5,type); }'
Note: As documented in the warning section of the pfiles manual page, it is not recommended to run this command on a heavily loaded production system with a time sensitive process running as deadlocks or crashes might happen.
Note #2: The previous warning doesn't apply to the last update of Solaris (Oracle Solaris 11.4) because pfiles no more suspend the monitored process(es). It now just uses ad hoc /proc pseudo files.
As of Solaris 11.2 this type of information is now available directly in the netstat command (-u option) so you don't have to use the pfiles hack for the purpose or use the lsof tool. Personally I've always wondered why this information could not be part of the netstat output so glad to see that'll finally be the case.
There's a nice blog from Oracle on the topic.
(caveat: at the time of writing v11.2 is in beta but fully disclosed as to the contents / new features)
you can try pfiles,fuser. you can install lsof on solaris.
http://andriigrytsenko.net/2010/08/lsof-installation-on-solaris-10/

ps command output on AIX, HPUX and Solaris

I am writing a portable shell script to get system process information, I need process id, command, pwdx (linux). On linux I am able to get this information as follows.. but it fails on all other unix flavours.
$ ps -awwwwwww -u <userid> -o pid,cmd|grep -i <filter_term> | egrep -v grep
$ pwdx <pid>
what I should use on AIX, HPUX and Solaris to get the similar information, or there any cross platform command
On Solaris I have tried /usr/ucb/ps but that support formatted output and lsof for pwdx equivalent but that also doesn't show what I need
On Solaris I have tried /usr/ucb/ps but that support formatted output:
What is wrong with formatted output ?
and lsof for pwdx equivalent but that also doesn't show what I need.
That doesn't make sense. pwdx is a Solaris native command and was even originally implemented on that OS.
Linux != Unix. And in the same hand, the commands are not always going to be the same, for instance GNU ps is not like Solaris ps or HP-UX ps etc. In some cases the Vendor Unix flavors offer a "compatibility binary" like those stashed in /usr/ucb on solaris. But ultimately you need to look at the man page for each version and review the output format options.
Edit. That is for in general all commands. Including grep, egrep etc.
To show the full command name, use this
ps -eo comm
This will show the command that was run. (ps is from /usr/bin on my Solaris system 5.11)

Resources