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
# 1  
Old 11-03-2010
help with if clause in bash script

hey guys,

I am trying to get some statistics from a DHCP server, like counting the number of DHCP Discovers from a specific MAC address. The script should count the number of DHCP Discovers and output it, otherwise if it cannot find any , it should output the MAC address and "0".

The first clause I am pretty sure it works, I did test it. The problems when I add the if clause.

I did the script like this :

cat /var/log/messages.2.gz | zgrep 192.160 | zgrep "DHCPDIS" | zgrep 0012DFCFC492 | cut -c7- | cut -d ' ' -f5 | sort | uniq -c | sort -rn >> /my_scripts/LOGS/dhcp_test
if [ " cat /var/log/messages.2.gz | zgrep 192.160 | zgrep "DHCPDIS" | zgrep 0012DFCFC492 | cut -c7- | cut -d ' ' -f5 | sort | uniq -c | sort -rn " -lt "1"];
then echo "0012DFCFC492 0" >> /my_scripts/LOGS/dhcp_test
fi

I am getting this error :

./test.bash: line 2: [: missing `]'

What am I doing wrong ?

Thanks in advance!
# 2  
Old 11-03-2010
I'm not sure what you're even trying to do by putting that giant chunk of stuff in an if-statement, but I'm pretty sure there's a better way to get what you want done than an eight-long pipe chain. You've also got several useless uses of cat thrown in for good measure. And I don't think you're supposed to use several zgrep's in a row like that either, just one is enough to unzip the stream.

What is your input, and what output do you want?
# 3  
Old 11-03-2010
Have you got a space at the end of your test brackets? Yours seems to end with -lt "1"] Without the space bash thinks that the bracket is part of the rest of it.
# 4  
Old 11-03-2010
Corona,

What I try to do is simple : I try to count the number of DHCP discovers for that specific MAC address (0012DFCFC492) and the IP address starting with 192.168.

The initial string looks like this :

Code:
Nov  3 17:01:04 dhcp01 ZEMdhcp: 0012DFCFC492: Got DHCPDISCOVER xid=0x545e01bc from 0.0.0.0 via 192.168.124.1, req-ip=None, 82=(0012DFCFC492, 576F6F1E1201, '192.168.124.1').

I have a lot of this kind of messages in my network, so I decided to count the devices which issue these messages more than 10 times.

This is what actually this line should do :

Code:
cat/var/log/messages.2.gz | zgrep 192.168 | grep "DHCPDIS" |grep 0012DFCFC492 | cut -c7- | cut -d ' ' -f5  | sort | uniq -c | sort -rn >> /my_scripts/LOGS/dhcp_test

The output of this looks like :
Code:
1599 0012DFCFC492

Note that I also use the sort to arrange this count descending.

What can I eliminate from this script till now ?

Next, if no such line is found in the messages file, I want that the script output this :
Code:
0 0012DFCFC492

Did you get my point ?

Last edited by Scott; 11-03-2010 at 08:12 PM.. Reason: Code tags
# 5  
Old 11-03-2010
Code:
cat /var/log/messages.2.gz | zgrep 192.160 | zgrep "DHCPDIS" |
  zgrep 0012DFCFC492 | cut -c7- | cut -d ' ' -f5 | sort | uniq -c |
  sort -rn >> /my_scripts/LOGS/dhcp_test

if [ " cat /var/log/messages.2.gz | zgrep 192.160 | zgrep "DHCPDIS" |
  zgrep 0012DFCFC492 | cut -c7- | cut -d ' ' -f5 | sort | uniq -c |
  sort -rn " -lt "1" ];
then
   echo "0012DFCFC492 0" >> /my_scripts/LOGS/dhcp_test
fi

See the red? I added a space between ] and everything else.

---------- Post updated at 17:19 ---------- Previous update was at 17:10 ----------

Your code has many errors.

zgrep -c [pattern] file.gz will give you a count. You should consider creating a pattern to match several of the criteria all at once. OR. You can use awk to "group them" if you wanted different IP's, for example.

So show us EXACTLY what you want for output, a small sample. You kind of hinted above at what you want.
# 6  
Old 11-03-2010
Hi ,

Thanks, I did that change but still I am getting this error :

./test.bash: line 2: [: too many arguments


What could be wrong ?

---------- Post updated at 01:35 AM ---------- Previous update was at 01:19 AM ----------

Hi,

As I said, I need to count the numbers of DHCP Discovers from the logfile.

Here it is the output :

DHCP01 Test # cat /var/log/messages.2.gz | zgrep 94.18 | grep "DHCPDIS" | grep 0012CFCDA11A | cut -c7- | cut -d ' ' -f5 | sort | uniq -c


3 010012CFCDA11A:

Here is one sample of a line in messages.2.gz :

Nov 3 17:01:04 dhcp01 ZEMdhcp: 010012CFCDA11A: Got DHCPDISCOVER xid=0x545e01bc from 0.0.0.0 via 94.18.124.1, req-ip=None, 82=(0012CFCDA11A ,576F6F1E1201, '94.18.124.6').



So, basically, it counts the number of DHCP discovers for this mac address.

Next, I want to create a script to put the output results in a file. If the command cannot find any discover for a mac address, the output should look like

0 010012CFCDA11A:



The script that I created looks like this :


cat /var/log/messages.2.gz | zgrep 94.18 | zgrep "DHCPDIS" | grep 10012CFCDA11A | cut -c7- | cut -d ' ' -f5 | sort | uniq -c >> /my_scripts/LOGS/dhcp_test

if [ " cat /var/log/messages.2.gz | zgrep 94.18 | grep "DHCPDIS" | grep 10012CFCDA11A | cut -c7- | cut -d ' ' -f5 | sort | uniq -c " -lt "1" ];
then
echo "0 10012CFCDA11A" >> /my_scripts/LOGS/dhcp_test
fi


Thanks,

Last edited by liviusbr; 11-03-2010 at 08:50 PM..
# 7  
Old 11-03-2010
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 }'

These 2 Users Gave Thanks to Chubler_XL For This Post:
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