LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2011, 09:49 AM   #16
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0

Quote:
Originally Posted by grail View Post
This will include 2000 which you implied was not to be included in post #9
Yes 2000 is nwhat I needed.
 
Old 08-17-2011, 10:28 AM   #17
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Hello, just one more question and I'm hoping someone can help me as I know it's simple for someone more experienced. How do I add these two variables to make it equal "0". For example, I need the sum of $CNT1 and $CNT2. I kinda giot screwed up towards the bottom of the script.

#!/bin/bash


PID1=$(ps -ef | grep -i "wxs_catalog_coherence" | grep -v grep)
PID2=$(ps -ef | grep -i "wxs_catalog_profile" | grep -v grep)

CNT1=$(ps -p $PID1 -m -o THREAD | wc -l)
CNT2=$(ps -p $PID2 -m -o THREAD | wc -l)



(( $CNT1 > 2000 )) && echo $CNT1 || echo "0"
(( $CNT2 > 2000 )) && echo $CNT2 || echo "0"

The result is prints out this output:

$ ./threadcount
0
0

I just need it to print out the sum of the two variables $CNT1 and $CNT2. Into one numer. I would most likely expect a "0" in most scenarios. Thanks.
 
Old 08-17-2011, 10:47 AM   #18
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Q_Linux View Post
PID1=$(ps -ef | grep -i "wxs_catalog_coherence" | grep -v grep)
PID2=$(ps -ef | grep -i "wxs_catalog_profile" | grep -v grep)
Print $PID1 and $PID2; I doubt they're integers.
Kevin Barry
 
Old 08-17-2011, 10:57 AM   #19
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ta0kira View Post
Print $PID1 and $PID2; I doubt they're integers.
Kevin Barry
That just printed out the existing PID id's. What I am looking for is adding both $CNT1 and $CNT2 together. I have to have just one value for output.
 
Old 08-17-2011, 11:13 AM   #20
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
It seems lots of people keep directing you so sites but it does not appear you are reading many / any of them. Bash arithmetic can be performed with expr, let or (()).
I will leave it up to you to work out which you prefer.
 
Old 08-17-2011, 11:35 AM   #21
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
It seems lots of people keep directing you so sites but it does not appear you are reading many / any of them. Bash arithmetic can be performed with expr, let or (()).
I will leave it up to you to work out which you prefer.
I'm not sure which sites you are referring to and I have been going to many of them to save myself from asking people for help, but I will try and do this on my own from now on.
 
Old 08-17-2011, 11:35 AM   #22
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Q_Linux View Post
That just printed out the existing PID id's. What I am looking for is adding both $CNT1 and $CNT2 together. I have to have just one value for output.
If you're getting integers for $PID1 and $PID2 then your ps -ef shows something completely different than mine. My ps doesn't support the command-line format you use when setting CNT1 and CNT2, either. At this point I think you need to go through man ps and assemble what you need a step at a time in the terminal.
Kevin Barry
 
Old 08-17-2011, 02:05 PM   #23
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
To answer your basic question:

You can add the values together using commands like expr or bc (expr doesn't work for very large numbers or do floating point so bc is sometimes used when expr doesn't work.

Code:
TTLCNT=$(expr $CNT1 + $CNT2)
echo $TTLCNT
--OR--

Code:
TTLTCNT=$(echo $CNT1 + $CNT2 | bc)
echo $TTLCNT
Note that for floating point you have to use "bc -l" instead of just bc.

Whenever someone mentions a command you can find out more about the command by typing "man <command>" and/or "info <command>".

The shells have a lot built into them so you can do math without requiring external commands but being an old UNIX Admin I tend to continue using the commands above.
 
Old 08-17-2011, 02:45 PM   #24
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MensaWater View Post
To answer your basic question:

You can add the values together using commands like expr or bc (expr doesn't work for very large numbers or do floating point so bc is sometimes used when expr doesn't work.

Code:
TTLCNT=$(expr $CNT1 + $CNT2)
echo $TTLCNT
--OR--

Code:
TTLTCNT=$(echo $CNT1 + $CNT2 | bc)
echo $TTLCNT
Note that for floating point you have to use "bc -l" instead of just bc.

Whenever someone mentions a command you can find out more about the command by typing "man <command>" and/or "info <command>".

The shells have a lot built into them so you can do math without requiring external commands but being an old UNIX Admin I tend to continue using the commands above.

Thanks, that was quite helpful Mensa.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 03:05 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM
ssh - using variables in call to start remote script from local script babag Linux - Networking 2 06-03-2008 04:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration