Solved

Linux Shell: Pass number to command

Posted on 2013-11-21
1
234 Views
Last Modified: 2013-11-21
In the code below, the number 99 is hardcoded.  How can I use $limit instead?
#!/bin/bash
limit=$((${1}+0))
if [ "$limit" -lt 1 ]; 
 then :
 limit=5
fi
echo `ps -eo "%c %C" | grep -v '%CPU' | grep -v '0.0' | sort | awk 'BEGIN{s="";}{if (s == $1){n=n+$2} else {if (s != ""){print s " " n};s=$1;n=$2}}END{if (s != ""){print s " " n;}}' | sort -rn -k +2 | awk 'BEGIN{p=0;} NR <= 99 {if(s==0&&$1!=""){s=1;print "\"hogs\":{"} if(s==1){if(NR != 1) { printf "," } printf " \"%s\":%.1f",$1,$2; }} END{if(s==1)print "}"}'`

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39665832
#!/bin/bash
limit=$((${1}+0))
if [ "$limit" -lt 1 ]; 
 then :
 limit=5
fi
echo `ps -eo "%c %C" | grep -v '%CPU' | grep -v '0.0' | sort | awk 'BEGIN{s="";}{if (s == $1){n=n+$2} else {if (s != ""){print s " " n};s=$1;n=$2}}END{if (s != ""){print s " " n;}}' | sort -rn -k +2 | awk -v L="$limit" 'BEGIN{p=0;} NR <= L {if(s==0&&$1!=""){s=1;print "\"hogs\":{"} if(s==1){if(NR != 1) { printf "," } printf " \"%s\":%.1f",$1,$2; }} END{if(s==1)print "}"}'`

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now