Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linux Shell: Pass number to command

Posted on 2013-11-21
1
Medium Priority
?
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

636 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