Solved

Linux Shell: Pass number to command

Posted on 2013-11-21
1
238 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help With Shell Script Centos 6 6 68
reset ubuntu password for root and admin after clearing it. 7 56
awk sed 8 63
AWK: Pytthagoras bp script Part REVISITED 3 17
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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 …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

789 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