Turn null value into 0 with a Bash Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Turn null value into 0 with a Bash Script
# 1  
Old 06-12-2014
Turn null value into 0 with a Bash Script

Hi,

I'm trying to make a bash script that if a null value is returned then to output the value 0.

I would like a script to search the 'top' tree and return the CPU value of a process, however if the process is not running for it to return 0 or another identifier.
Code:
top -b -n1 | awk '/ PROCESS /{print $9}'

Any help appreciated.


Moderator's Comments:
Mod Comment Please use code tags for your code and data, thanks

Last edited by vbe; 06-12-2014 at 04:27 PM..
# 2  
Old 06-12-2014
Try this:

Code:
top -b -n1 | awk '/ PROCESS /{CPU+=$9} END { printf "%03.1f\n",CPU }'

# 3  
Old 06-12-2014
Perfect thank you.

I'm not sure how, but it worked.

Many thanks.
# 4  
Old 06-12-2014
I'll try and explain CPU+=$9 is equivalent to CPU=CPU+$9, so you can see we are adding up all the CPU for all lines that match your process.

END { printf "%03.1f\n", CPU } when the end of input is reached the total CPU is printed, the format of printf is a little tricky (man printf may be of help here).
In this case we are forcing 1 decimal place and require a leading zero if value is less than 1 (eg 0.5).
# 5  
Old 06-12-2014
Thanks for your explanation. That makes sense. I have not used printf before, seems like it come in handy.

Cheers, for all your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

How to turn off ora errors in shell script?

I have a shell script which select total count from a table and use its value in a if condition like below connect_string="username/password@tnsname" tot=`sqlplus -s $connect_string << EOF set echo off set feedback off set head off select count(*) from test_table; EOF ` if then echo... (2 Replies)
Discussion started by: vel4ever
2 Replies

3. Shell Programming and Scripting

pass null value to sql script from korn shell script

There are 4 parameters that I have to pass from korn shell to sql script. 1) I have to check if $1 , $2 , $3 and $4 are null values or not . How can I do that ? 2) Once its determined that these values are null (in the sense they are empty) how can I pass null values to sql script... (11 Replies)
Discussion started by: megha2525
11 Replies

4. Shell Programming and Scripting

bash script argument not outputing line -NULL

Hi everybody! I am writing a script which accepts two arguments one of them being a message. All is working except for the message part. That is, to accept text as an argument. I have trouble storing and echoing a line of text. It seems that writing a few words and store them in an... (6 Replies)
Discussion started by: bluetxxth
6 Replies

5. Shell Programming and Scripting

Turn an awk one liner into a script?

Hi, I was just helped with an excellent one liner. Now I need to know how to turn it into a script that I can call at the command line. So, this works with file script: ------------ for file in DATA.txt; do awk 'NR==1;NR>1{for (i=1;i<=NF;i++){a+=$i}}END{for (i=1;i<=NF;i++){printf... (3 Replies)
Discussion started by: mikey11415
3 Replies

6. UNIX for Dummies Questions & Answers

Turn python script into an installed UNIX command (Mac OS X)

Okay so i have a script i wrote in python. I want to turn this script into an INSTALLED COMMAND to run in terminal. Right now i run it like this in terminal... $ python myscriptname.py or $ ./myscriptname.py but i want to be able to run it like this in terminal... $ myscriptname ... (3 Replies)
Discussion started by: cbreiny
3 Replies

7. UNIX for Advanced & Expert Users

How would I create a NULL terminated file in Unix under Bash?

I am testing some file routines against potential "nasty name" Unix files, such as those with a CR, LF, in the middle or NULL terminated, utf multi-byte character. So, under Bash, I want some way of: mv "name" "name\0" with the \0 a real NULL. Against all my efforts, I have not been... (2 Replies)
Discussion started by: drewk
2 Replies

8. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

9. Shell Programming and Scripting

Shell script to turn on Autofilter in Excel.

Hi, I have a shellscript that produces a csv that can be opened in Microsoft Excel. It has two columns and about 10 rows in each column, so only twenty cells (at the moment). When the user opened the csv, I wanted it so autofilter was already turned on in columns A1 and B1 (and potentially C1,... (1 Reply)
Discussion started by: rainemaida
1 Replies

10. Shell Programming and Scripting

redirect stderr to dev/null in bash env.

Working in a bash environment, in the following example, how do I direct the error message that putting in an invalid flag (-j for example) would normally produce to dev/null? while getopts "abcd" opt do case "$opt" in i) a etc ;; r) b etc ;; f) c etc ;; v) d... (7 Replies)
Discussion started by: Sniper Pixie
7 Replies
Login or Register to Ask a Question