Sponsored Content
Top Forums Shell Programming and Scripting For loop in bash - Direct output to two files Post 302996215 by nms on Friday 21st of April 2017 01:21:04 PM
Old 04-21-2017
For loop in bash - Direct output to two files

Hello all,

i have a code in which when doing a for loop, i need to direct the output to two files, one just a single output, the other to always append (historical reasons).

So far i managed to do the following, which is working, but am still considering it as "dirty".

Code:
INTERFACE_NAME=`$SNMP -v2c -c $COMMUNITY $HOST ifDescr | awk '{print$4}'`
for int in $INTERFACE_NAME
do
inOct1=`grep "$int " $HOMEDIR/cron/tmp/$(basename $0 .sh)1 | awk '{print$4}' | tr -d "incoming="`
inOct2=`grep "$int " $HOMEDIR/cron/tmp/$(basename $0 .sh)2 | awk '{print$4}' | tr -d "incoming="`

outOct1=`grep "$int " $HOMEDIR/cron/tmp/$(basename $0 .sh)1 | awk '{print$5}' | tr -d "outgoing="`
outOct2=`grep "$int " $HOMEDIR/cron/tmp/$(basename $0 .sh)2 | awk '{print$5}' | tr -d "outgoing="`

speed=`grep "$int " $HOMEDIR/cron/tmp/$(basename $0 .sh)1 | awk '{print$3}' | tr -d "speed="`
status=`grep "$int " $HOMEDIR/cron/tmp/$(basename $0 .sh)1 | awk '{print$1}' | tr -d "state=" | tr -d "([0-9])"`

#Diff the polls, divided by the interval
if [ $outOct2 -lt 0 ] || [ $outOct1 -lt 0 ] || [ $inOct2 -lt 0 ] || [ $inOct1 -lt 0 ]; then
    echo "Negative numbers not allowed...exiting"
    exit 1
fi

inUsage=`echo $(((inOct2 - inOct1) / TIME_INTERVAL))`
outUsage=`echo $(((outOct2 - outOct1) / TIME_INTERVAL))`

#Convert to bits
inUsage=`echo $((inUsage * 8))`
outUsage=`echo $((outUsage * 8))`

#get usage in percantage
perinUsage=`echo "scale=4;((($inUsage/$speed) * 100))" | bc`
perinUsage=`printf '%.3f\n' $perinUsage`
peroutUsage=`echo $(((outUsage/speed) * 100)) | bc`
peroutUsage=`printf '%.3f\n' $peroutUsage`

echo $DATE "|" $int "|" $speed "|" $status "|" $inUsage "|" $perinUsage "|" $outUsage "|" $peroutUsage
done | tee $OUTPUTDIR/$(basename $0 .sh) | tee -a  $HISTDIR/${DATESTAMP}.$(basename $0 .sh)

#Remove LOCK
rm -f ${LOCKFILE}

As you can see this is the interesting part:
Code:
echo $DATE "|" $int "|" $speed "|" $status "|" $inUsage "|" $perinUsage "|" $outUsage "|" $peroutUsage
done | tee $OUTPUTDIR/$(basename $0 .sh) | tee -a  $HISTDIR/${DATESTAMP}.$(basename $0 .sh)

where i need to output to two files.

Is there a way to simplify this by making the for loop output to two files and making the output to not show in the screen?

Thanks in advance for your comments
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to direct awk output to expr?

Is there any way to combine the following two statements into one? I can't figure out how to get expr to take input from the output of the awk call - I've tried piping the output of the awk call into the expr call, and tried using a 'Here' document, nothing seems to work. export CNT=`wc -l... (4 Replies)
Discussion started by: jvander
4 Replies

2. UNIX for Dummies Questions & Answers

direct output to a file then email it

Ok so i have this script and I dont know how to have the output go to a file and then email that file to someone. #!/bin/ksh print "AL" print "AM" print "AN" print "RL\n" nawk '/PROD/ {print $3, $2}' /home/user/switch_listtest | sort -k1,2 print "End of Report" Thank you in... (2 Replies)
Discussion started by: llsmr777
2 Replies

3. Shell Programming and Scripting

Direct the output of a script to a log file

Hi, I have a script to compare 2 files. file1=$1 file2=$2 num_of_records_file1=`awk ' END { print NR } ' $file1` num_of_records_file2=`awk ' END { print NR } ' $file2` i=1 while do sed -n "$i"p $file1 > file1_temp sed -n "$i"p $file2 > file2_temp diff file1_temp... (5 Replies)
Discussion started by: autosys_nm
5 Replies

4. Shell Programming and Scripting

how to direct scp output to a file in bash shell or script

I can run this from the command line: scp -i identfile /path/file_to_send remotelogin@remotebox:/path_to_put_it/file_to_send and I get: file_to_send 100% |***************************************************************************| 0 00:00 but if I do: scp -i identfile... (6 Replies)
Discussion started by: NewSolarisAdmin
6 Replies

5. Shell Programming and Scripting

Bash: Help with output from a for loop

Hi, Sorry I'm new to shell scripting.. my loop is as follows: let i=0 for item in ${APPSARRAY} do #..some code to get a unique value called $result let i=i+1 done What I want to do is within the for loop, create a comma seperated list: ... (3 Replies)
Discussion started by: mjwoodford
3 Replies

6. Shell Programming and Scripting

Been working since 25+ hrs: Bash Script to rename files supposedly direct but difficult to execute

:wall::wall::wall: Hi I have horrible script below, need help in renaming ls -l output into new filename format: Desired output: cp -pv original_path/.* newDirectory/owner_of_file.%dd%mm%y.file_extension.first_8_characters_of_original_filename localuser@localuser:~ vi... (3 Replies)
Discussion started by: wolf@=NK
3 Replies

7. Shell Programming and Scripting

Manipulating sed Direct Input to Direct Output

Hi guys, been scratching round the forums and my mountain of resources. Maybe I havn't read deep enough My question is not how sed edits a stream and outputs it to a file, rather something like this below: I have a .txt with some text in it :rolleyes: abc:123:xyz 123:abc:987... (7 Replies)
Discussion started by: the0nion
7 Replies

8. Shell Programming and Scripting

BASH - Need to echo for loop output to one line

I'm trying to echo the release version of some of our Linux servers. Typically I do these types of things by "catting" a text file with the host names, "ssh-ing" to the host and running my string. This is what I've written for i in `cat versions.txt` ; do echo $i ; ssh $i cat /etc/issue |... (5 Replies)
Discussion started by: lombardi4851
5 Replies

9. Shell Programming and Scripting

Disk Space Script to direct output

Hi, I am working on Sun Solaris 5.10 and want to direct the output from a disk space check script to an output file; #!/bin/bash CURRENT=$(df -k /log/logs | grep /log/logs | awk '{ print $5}' | sed 's/%//g') THRESHOLD=30 if ; then echo "Remaining free space is low" > output.txt else... (10 Replies)
Discussion started by: SSKAAB
10 Replies

10. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies
continue(3tcl)						       Tcl Built-In Commands						    continue(3tcl)

__________________________________________________________________________________________________________________________________________________

NAME
continue - Skip to the next iteration of a loop SYNOPSIS
continue _________________________________________________________________ DESCRIPTION
This command is typically invoked inside the body of a looping command such as for or foreach or while. It returns a TCL_CONTINUE code, which causes a continue exception to occur. The exception causes the current script to be aborted out to the innermost containing loop command, which then continues with the next iteration of the loop. Catch exceptions are also handled in a few other situations, such as the catch command and the outermost scripts of procedure bodies. EXAMPLE
Print a line for each of the integers from 0 to 10 except 5: for {set x 0} {$x<10} {incr x} { if {$x == 5} { continue } puts "x is $x" } SEE ALSO
break(3tcl), for(3tcl), foreach(3tcl), return(3tcl), while(3tcl) KEYWORDS
continue, iteration, loop Tcl continue(3tcl)
All times are GMT -4. The time now is 05:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy