Help with Bash shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Bash shell script
# 1  
Old 01-26-2018
Help with Bash shell script

Hi All,

I have a script which as below

Code:
#!/bin/bash
for i in `cat servers`
do
ssh uname@$i "df -t xfs --total | grep total";
done > out.txt

Output as below

--------------
Code:
total          140583991104 118622795524 21961195580  85% -
total          140583991104 112888595524 27695395580  81% -
total          140583991104 113083933588 27500057516  81% -
total          140583991104 113981125312 26602865792  82% -
total          140583991104 114674546432 25909444672  82% -
total          140583991104 114443785084 26140206020  82% -
total          140583991104 114541732596 26042258508  82% -
total          140583991104 114597880688 25986110416  82% -

----------------

Now I want to extend the script by sum up the columns 2,3 and 4 and average of column 5 and append the output to a file daily in csv format. I did try as below, but not the expected one as i dunno how to include today's date in the script.

Code:
#!/bin/bash
for i in `cat servers`
do
ssh uname@$i "df -t xfs --total | grep total";
done | awk '{ sum2 += $2; sum3 += $3; sum4 += $4; sum5 += $5; } END { print sum2/(1024*1024*1024), sum3/(1024*1024*1024), sum4/(1024*1024*1024) , sum5/8 }' > out1.csv

I just need the final output in below format. Please help.

Code:
24Jan2018          1047.43 843.116 204.31 81
25Jan2018          1047.43 850.612 196.82 81.9
26Jan2018          1047.43 853.452 193.98 82.12



Moderator's Comments:
Mod Comment Please use CODE tags (for data as well) as required by forum rules!

Last edited by RudiC; 01-26-2018 at 05:20 AM.. Reason: Added CODE tags.
# 2  
Old 01-26-2018
How about
Code:
awk -vDT="$(date +'%d%b%Y')" '
        {sum2 += $2
         sum3 += $3
         sum4 += $4
         sum5 += $5
        }
END     {GIGA=2^30
         print DT, sum2/GIGA, sum3/GIGA, sum4/GIGA , sum5/NR
        }
'

In your $5 summation, your take advantage of awk's behaviour to take into account leading digits only and to silently drop trailing non-digits (and followers) from a string if used in arithmetics. Please be aware of your uuoc (useless use of cat) - it's more widely accepted to read a file in a while loop.

Last edited by RudiC; 01-26-2018 at 05:35 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-13-2018
Quote:
Originally Posted by RudiC
How about
Code:
awk -vDT="$(date +'%d%b%Y')" '
        {sum2 += $2
         sum3 += $3
         sum4 += $4
         sum5 += $5
        }
END     {GIGA=2^30
         print DT, sum2/GIGA, sum3/GIGA, sum4/GIGA , sum5/NR
        }
'

In your $5 summation, your take advantage of awk's behaviour to take into account leading digits only and to silently drop trailing non-digits (and followers) from a string if used in arithmetics. Please be aware of your uuoc (useless use of cat) - it's more widely accepted to read a file in a while loop.

Thanks Rudic. This helped me. However i just can't figure out how to remove the decimals. I just need the output in numbers without decimals. Can you please help.
# 4  
Old 07-13-2018
Use printf with an appropriate format string.
# 5  
Old 07-13-2018
In case "appropriate format string" isn't obvious, note that it depends on what output you're trying to produce. If you want floating point numbers to be rounded to the nearest integer, use %.0f. If you want floating point numbers to be truncated to the largest integer that is less than or equal to the floating point number, use %d.

For example:
Code:
         printf "%s %.0f %.0f %.0f %.0f%%\n", DT, sum2/GIGA, sum3/GIGA, sum4/GIGA , sum5/NR

or:
Code:
         printf "%s %d %d %d %d%%\n", DT, sum2/GIGA, sum3/GIGA, sum4/GIGA , sum5/NR

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

3. Shell Programming and Scripting

Need help with bash shell script

I need to create digit day script that takes a single numeric argument and then it should print out the day of the week using the number modulo 7 formula e.g: 0 - Sunday 6- Saturday 131 - Friday I am fairly new to unix so I don't know how to use the number modulo 7 formula. Does the script need... (3 Replies)
Discussion started by: lukefrost96
3 Replies

4. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

5. Shell Programming and Scripting

Bash Shell Script

HELP!My program ends after entering one choice---need help making it take multiple inputs,instead of terminating after displaying just one #!/bin/bash# Crude address databaseclear # Clear the screen.echo " Contact List"echo " ------- ----"echo "Choose one of the following... (6 Replies)
Discussion started by: help123
6 Replies

6. Shell Programming and Scripting

Help with bash shell script

Hi, I have a file in which records contains non ascii characters. The records are comma delimited and quoted. The non ascii characters are found in a particular column. Example records "YY","AK000021","Ã","IO","PP" "Y1","AK000022","Ã","PO","PP" "Y2","AK000022","Ã","PO","PP" I need to... (2 Replies)
Discussion started by: akshu.agni
2 Replies

7. Shell Programming and Scripting

Bash shell script- help

I need to invoke a program on remote server using ssh in a shell script. In addition i would like to capture date/time and if there is any errors , then script should write to log file. can someone please help me out? (1 Reply)
Discussion started by: sam101
1 Replies

8. Shell Programming and Scripting

Bash Shell script--need help

Hi all, i am beginner to unix and trying out a shell script which does the following. i have to calculate a persons salary. his salary is read from the keyboard. he has two types of deductions. 40% as dearness allowance and 20% as house rent. i have to print the gross salary. here is the code... (5 Replies)
Discussion started by: Irishboy24
5 Replies

9. UNIX for Dummies Questions & Answers

Bash shell script

Hi Guys, I am trying to alter a script for my company. I need the start of it to go something like this. User is asked to input 8 numbers 8 numbers are written to a txt file ***** ***** ***** txt file is read ***** ***** The text file gets read in between other files represented by... (2 Replies)
Discussion started by: outthere_3
2 Replies

10. UNIX for Dummies Questions & Answers

need help with bash shell script

Hi guys! I have just started with shell programming!! I am having pronblem with variable subsitutuion. when i do egrep "*" marks this will give me the pattern match. but how can i catch the output of that result in a variable. if i say result = egrep "*" marks it gives me syntax... (2 Replies)
Discussion started by: vmtailor
2 Replies
Login or Register to Ask a Question