The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
while loop inside while loop panknil Shell Programming and Scripting 0 01-07-2008 12:49 PM
while loop smoorthy Shell Programming and Scripting 3 09-20-2007 06:28 AM
while loop whited05 Shell Programming and Scripting 2 11-03-2005 12:27 PM
for loop? surjyap Shell Programming and Scripting 1 10-20-2005 08:52 AM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 12:09 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-03-2008
felix001 felix001 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
Do ... Loop

ill be the first to admit i am a bit of a bash scripting noob... but ive come againest a small problem...

LOG=/var/log/local2.log

for i in SEC-0 SEC-1 SEC-2 SEC-3 SEC-4 SEC-5 SEC-6 SEC-7
do
grep $i $LOG | wc -l
done

The above works and gives me 8 different values. Later on in the script I wish to echo these strings, so i would like them as varibles.
Is there anyway to assign each total against a varible so when this is finished I have... something like..
$SEC-0=some number
$SEC-1=some number ..... etc etc

I hope I have made this clear, thanks in advanced....
  #2 (permalink)  
Old 08-03-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Not really, but you can either use a more parowerful scripting language and use arrays, or create a variable containing your 'SEC-?' entries.

Code:
#!/bin/sh
LOG=/var/log/local2.log
SEC="SEC-0 SEC-1 SEC-2 SEC-3 SEC-4 SEC-5 SEC-6 SEC-7"
for i in $SEC
do
  grep $i $LOG | wc -l
done

echo "$SEC" | awk '{ print $3 }'   # prints "SEC-2" (the third 'SEC' string)
grep `echo "$SEC" | awk '{ print $5 }'` $LOG | wc -l   #searches the log for "SEC-6"

Does that help?
  #3 (permalink)  
Old 08-05-2008
felix001 felix001 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
Ive now found what I was looking for ...


Code:
LOG="/var/log/local
level=(Emergency Alert Critical Error Warning Notification Informational Debugging)
for i in 0 1 2 3 4 5 6 7
do
        printf "${level[$i]} - " ;  grep -c SEC-$i $LOG
done

But the only thing im unsure of is how to allow for the wildcard in for the $LOG variable ????

edit by bakunin: please use "code"-tags when you post program code. Thx.

Last edited by bakunin; 08-06-2008 at 12:09 AM..
  #4 (permalink)  
Old 08-05-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
If you don't use quotes the wildcards should be evaluated.


Code:
LOG=/var/log/local?.log

  #5 (permalink)  
Old 08-06-2008
felix001 felix001 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 7
it doesnt seem to like that it says ..

./sysloghtml.sh: line 14: [: /var/log/local2.log: binary operator expected
Unable to find logfile at /var/log/local?.log
  #6 (permalink)  
Old 08-06-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
Quote:
Originally Posted by felix001 View Post
it doesnt seem to like that it says ..

./sysloghtml.sh: line 14: [: /var/log/local2.log: binary operator expected
Unable to find logfile at /var/log/local?.log

What is in line 14 of your script?

If you are testing against a wildcard, you will get an error if it expands to more than one filename.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0