Walid-Shouman
10/23/2014 - 6:59 AM

my display sorted processes script

my display sorted processes script

#!/bin/bash
# ---
# @author newlido
# @version 0.1
#
# HOWTO:
# ./myps.sh
# No options yet
# 
# Notes:
# sorting for the second time happens only for the highest 10 .. to make the preview better (highest show up first)
# for text colouring check: http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
# for switch cases check: http://www.thegeekstuff.com/2010/07/bash-case-statement/
#
# TODO:
# [x] play with colors
# [] pass a single value to a single ps command (instead of multiple ones)
# [] accept user input of tailing processes 
#
# -------------------------------------------------------


echo -n "Sort first 10 processes according to [mem or cpu or return]?: "
read memcpu
case $memcpu in

        [m] | [mM][Ee][mM] )
                echo -e "\e[1;31mSorting according to mem\e[0m";
                ps ax --no-headings -o user,pid,%cpu,%mem,vsz,sgi_rss,tname,stat,start_time,time,ucmd |sort -nk 3|tail -10 | sort -rnk 3;
                ;;

        [cC] | [c|C][P|p][U|u] )
                echo "$(tput setaf 1)Sorting according to CPU$(tput sgr0)";
                ps ax --no-headings -o user,pid,%cpu,%mem,vsz,sgi_rss,tname,stat,start_time,time,ucmd |sort -nk 4|tail -10 | sort -rnk 4;
                ;;

        [rR] | [r|R][e|E][T|t][U|u][R|r][N|n] )
		red='\e[0;31m';
		NC='\e[0m' # No Color;
                echo -e "${red}Return${NC}";
                exit 1
                ;;
        *) echo "Invalid input"
            ;;
esac