variable inside a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable inside a variable
# 1  
Old 11-02-2011
variable inside a variable

I am trying manipulate variables inside a variable
Code:
root@server [~]# hour=`date | awk {'print $4'} | cut -d: -f 1`;lasthour=`expr $hour - 01`; lasthour=`printf %02d $lasthour`;grep "`date -I` $lasthour" /var/log/exim_mainlog |egrep -o 'dovecot_login[^ ]+' | sort|uniq -c|sort -nk 1
      8 dovecot_login:user1@domain.com
      4 dovecot_login:user2@domain.com

The above prints output in two lines. When I take that to a variable and analze each line, it becomes 4 lines. What I need is to check the count of each entry and output if count is more than a limit, say 5. So the below script should only display "8 dovecot_login:user1@domain.com"
Code:
root@server [~]# for i in $(hour=`date | awk {'print $4'} | cut -d: -f 1`;lasthour=`expr $hour - 01`; lasthour=`printf %02d $lasthour`;grep "`date -I` $lasthour" /var/log/exim_mainlog |egrep -o 'dovecot_login[^ ]+' | sort|uniq -c|sort -nk 1); do echo $i; done
8
dovecot_login:user1@domain.com
4
dovecot_login:user2@domain.com

# 2  
Old 11-02-2011
I believe you're over-complicating the task with the use of so many tools - there could be a more graceful way to do all of it.
Please provide a sample of your /var/log/exim_mainlog file using code tags and a desired output with the explanation.

Also what happens for the mid-night hour - hour 0?
# 3  
Old 11-02-2011
I got it corrected. If the count is more than 3, it will display it.

Code:
hour=`date | awk {'print $4'} | cut -d: -f 1`;lasthour=`expr $hour - 01`; lasthour=`printf %02d $lasthour`;grep "`date -I` $lasthour" /var/log/exim_mainlog |egrep -o 'dovecot_login[^ ]+' | sort|uniq -c|sort -nk 1|  while read login; do num=`echo $login | awk {'print $1'}`;   if [ $num  -gt 3 ]; then echo $login;fi  ; done
4 dovecot_login:user1@domain.net
4 dovecot_login:user2@domain.com
11 dovecot_login:user3@domain.com
27 dovecot_login:user4@domain.ca

---------- Post updated at 07:48 PM ---------- Previous update was at 07:36 PM ----------

I have corrected the midnight 00 issue. Here is the complete script
Code:
#!/bin/sh

hour=`date | awk {'print $4'} | cut -d: -f 1`

if [ $hour= '00' ]; then
        hour = 24
fi

lasthour=`expr $hour - 01`
lasthour=`printf %02d $lasthour`


grep "`date -I` $lasthour" /var/log/exim_mainlog |egrep -o 'dovecot_login[^ ]+' | sort|uniq -c|sort -nk 1|  while read login
do
        num=`echo $login | awk {'print $1'}`
        if [ $num  -gt 3 ]; then
                echo $login
        fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

2. Shell Programming and Scripting

Resolve variable inside another variable

Hello Everyone, I am trying to resolve a variable inside another variable.Let me go straight to the example. Input: Query=$Table_1 Join $Table_2 (Query itself is a variable here) Now for two different cases I am assigning different values to Table_1 and Table_2 Case 1:... (14 Replies)
Discussion started by: vinay4889
14 Replies

3. Shell Programming and Scripting

evaluating a variable inside a variable

Hi there, i think im getting myself a little confused and need some help :wall: I am reading in a bunch of variables to my script from an external file and need to validate that a value has been set for each so if you can imagine, the user is required to pass in 4 values... (3 Replies)
Discussion started by: rethink
3 Replies

4. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

5. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

6. UNIX for Dummies Questions & Answers

Variable inside a variable

Hi, here is the problem description... X=1 var${x}=abc echo ${var${x}} It is not working...:mad: Is there any solution.. Thanks, Swat (2 Replies)
Discussion started by: swat
2 Replies

7. Shell Programming and Scripting

passing a variable inside another variable.

Any help would be great. I know this is a dumb way of doing this, but I would like to know if there is a solution doing it this way. I'm very new at this and I'd like to learn more. Thanks! :D:D count=0 while ; do echo "enter your name" read name_$count let count=count+1 done ... (2 Replies)
Discussion started by: reconflux
2 Replies

8. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

9. Shell Programming and Scripting

How to replace variable inside the variable

hi sir, i need your help for this script inside /rnmucdr/ednms05/ken/xMNBDF045_Script.sql content variable like this select * from invoice where bill_date=$BILLDATE and startNum=$STARTPARTNNUM and total_partn=$TOTALPARTN if i just paste this replace with the $SCRIPT it works great,if... (31 Replies)
Discussion started by: mani_um
31 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question