help with if clause in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with if clause in bash script
# 8  
Old 11-03-2010
Or just a shorter pipe chain in bash. using egrep lets you match more complicated expressions, and using the -o option tells it to print only the matching part.

Code:
zegrep -o "DHCPDISCOVER xid=0x[a-z0-9]+" < logfile.gz | sort | uniq -c

---------- Post updated at 06:28 PM ---------- Previous update was at 06:21 PM ----------

Quote:
If the command cannot find any discover for a mac address, the output should look like ...
Are you trying to match data to some existing format? Why store a lack of data?
# 9  
Old 11-04-2010
Hi Corona,

This is how the DHCP messages look like in the logs

Nov 4 06:47:22 dhcp01 ZEMdhcp: 01/0010E7218F82: Got DHCPDISCOVER xid=0x2158b2c1 from 0.0.0.0 via 10.140.2.10, req-ip=None, 82=None.

I have some MAC addresses that I need to monitor - how many discovers they send per day. The MAC addresses look like this : 0010E7218F82.
The output of the following script is :

cat /var/log/messages.2.gz | zgrep 94.18 | grep "DHCPDIS" | grep 0010E7218F82 | cut -c7- | cut -d ' ' -f5 | sort | uniq -c

582 0010E7218F82

That's the count of the DHCP discovers sent by this MAC.
I have a list of MACs that I need to monitor, that's why I need to put 0 for the MACs that do not issue any discover. Then copy-paste the results into an excel.

So how can i adapt the script above to have the output like :

0 0010E7218F82

if it does not find anything ?

Liviu

---------- Post updated at 11:00 AM ---------- Previous update was at 10:54 AM ----------

Quote:
Originally Posted by Chubler_XL
How about using awk

Code:
zcat /var/log/messages.2.gz | awk -v MAC=0012CFCDA11A -v C=0 '{ if ($0 ~ "DHCPDIS.*94.18.*"MAC ) C++ } END { print C, MAC }'


This works! Thanks a lot!

---------- Post updated at 11:28 AM ---------- Previous update was at 11:00 AM ----------

Thanks Chuber,

How can I set this code for a pattern ?

Lets say that I have a list of MACs like this

0012CFCDA11A
0012CFCDA123
0012CFCD7123

..and I want to apply the code for each MAC addreses in the list ?
# 10  
Old 11-04-2010
What do you mean "apply the code"? Do you mean "check if it's in the list"?
# 11  
Old 11-04-2010
Two ways:

Easy but not so efficient:

Code:
for MYmac in 0012CFCDA11A 0012CFCDA123 0012CFCD7123
do
    zcat /var/log/messages.2.gz | awk -v MAC=$MYmac -v C=0 '{ if ($0 ~ "DHCPDIS.*94.18.*"MAC ) C++ } END { print C, MAC }'
done

Efficient but more complex, is to put your Mac-list into the file "macs" and process file and stdin with awk:

Code:
zcat messages.gz | awk '{ F+=(FNR==1);
       if (F==1) cnt[$1]=0;
       if (F==2 && $0 ~ "DHCPDIS.*94.18") {
            MAC=gensub(/^.*ZEMdhcp: ([^:]*):.*/, "\\1", "");
            if (MAC in cnt) cnt[MAC]++;
       } } END { for(i in cnt) print cnt[i], i } ' macs -


Last edited by Chubler_XL; 11-04-2010 at 08:58 PM.. Reason: Fixup formatting
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

If clause query

Hi, i need to add a condition in my IF clause where i need to check if the file exists in a folder and return true out of it. but in my directory i have multiple files with same name but datestamp append on it for e.g. export f1 = filename export f2=filename1 if ] then echo "No... (9 Replies)
Discussion started by: rohit_shinez
9 Replies

3. Shell Programming and Scripting

Oracle 11g script read from file in where clause (RHEL 5.7)

Hi everyone, Simple question. I have a comma-delimited file, aux.txt, with the following contents (all in one line): 'value1','value2','value3',...,'valueN' I would like to know if there's a way I can use that file inside a sql script in a where clause, like so: select myfield1 from mytable... (6 Replies)
Discussion started by: gacanepa
6 Replies

4. Shell Programming and Scripting

Generating & executing the SELECT and UPDATE clause dynamically using shell script

Hi All, I need to write one shell script. The requirement is as follows. a) I am having list the employee names in a file stored in /tmp directory location as below /tmp/emp.txt b) and the contents are as below cat emp.txt ravi raj ram arun c) I need to connect to sybase... (1 Reply)
Discussion started by: Gowtham_giri
1 Replies

5. Shell Programming and Scripting

../ in perl and if clause

Hi can anyone please explain what the below code does? i mean $fide_stopfile = ? when $FIDE_SCR = '/fs/dir1/dir2/common/scr' and also little confused with if clause too. what it check? $fide_stopfile = "$ENV{FIDE_SCR}/../tmp/STOP"; if ( -e $fide_stopfile > 0 ) { ... (3 Replies)
Discussion started by: ptappeta
3 Replies

6. Shell Programming and Scripting

If clause in perl

HI friends , I am very new to perl .please dont mind if i ask silly questions. I seee below code in one sript if ( exists $ENV{FMTWRP_TMP_DIR} and $ENV{FMTWRP_TMP_DIR} ) { $tdir = $ENV{FMTWRP_TMP_DIR}; } whats does this mean . I am very confused about the if clauses in... (1 Reply)
Discussion started by: ptappeta
1 Replies

7. Shell Programming and Scripting

Check a variable value through if clause

Hi guys, I am trying to check the values i have for two variables. if && ; then echo "Success"; fi Now Test1 can have any Alpha Variable and Count is a integer value. Even though we have given 'and' Condition, even one condition is sucess, i am getting the Success message. ... (11 Replies)
Discussion started by: mac4rfree
11 Replies

8. Shell Programming and Scripting

Bash-Shell: If-Clause to check if file is empty

Hello, I want to checkl whether my file has text in it or not. if ; then ... if ; then ... But none of these work Can someone help me? ---------- Post updated at 09:00 AM ---------- Previous update was at 08:55 AM ---------- The code-tags caused an displayerror,... (5 Replies)
Discussion started by: ABE2202
5 Replies

9. UNIX for Dummies Questions & Answers

if clause

hi, pls could you help me with one program in KSH ( i have sunOS). I need to create an If clause, that prints an error message and filenames, when in a directory are found some files of null size (find . -type f -size 0 ). thanks (3 Replies)
Discussion started by: palmer18
3 Replies

10. Shell Programming and Scripting

building a SET clause in shell script

Hi, I have a comma delimited string, e.g. empno, ename, sal. Using Korn Shell Script I want to build the SET clause for an UPDATE statement, and set clause should look like this: empno=decode(:empno, '?', empno, :empno), ename=decode(:ename, '?', empno, :ename), sal=decode(:sal, '?',... (14 Replies)
Discussion started by: shalua
14 Replies
Login or Register to Ask a Question