Shell Scripting Help/Ideas?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Scripting Help/Ideas?
# 1  
Old 05-21-2007
Shell Scripting Help/Ideas?

I understand the code in the following may not be perfect, but I'll work that out. I'm more looking for ideas on how to do what I need more efficiently.

I have log files that are archived each day so today's log is called nmslog, yesterday's is nmslog.1.gz, 2 days ago nmslog.2.gz, etc. I want to make a script that allows me to specify how many days back I want to check including everyday between now and the specified day.

The only way I can think to do this is with if statements like:

Code:
#!/bin/bash

echo -n "How many days back would you like to check?"; read days
echo -n "What would you like to search on?"; read search

if [ "days" = "1" ]; then
grep nmslog $search; zgrep nmslog.1.gz $search
elif [ "days" = "2" ]; then
grep nmslog $search; zgrep nmslog.1.gz $search; zgrep nmslog.2.gz
fi

The problem is there are 30 days of log files so that would be a very long if i wanted to search everyday between today and 25 days ago.

Is there a better way to do this?

Thanks for any help you can provide.
# 2  
Old 05-21-2007
Consider something like this:
Code:
echo -n " number of previous days " 
read days
let i=1
grep  "$search" nmslog 
if [[ $days -eq 0 ]] then
   exit
fi
while [[ $i -le  $days ]]
do
    zgrep "$search"  nmslog.$i.gz
    let i=$i+1
done

# 3  
Old 05-21-2007
Code:
#!/bin/ksh

# to consider:
# validating the days variable (make sure it is a number, etc)
# validating the search variable (make sure it not null, etc)

echo "how many days      \c"
read days
echo "enter search string \c"
read search

while [ ${days} -ne 1 ]; do
        let days=days-1
        zgrep ${search} nmslog.${days}.gz
done
grep ${search} nmslog

# 4  
Old 05-22-2007
You can try something like that (not tested) :

Code:
#!/bin/bash

echo -n "How many days back would you like to check?"; read days
echo -n "What would you like to search on?"; read search

logs=
for ((d=1; d<days; d++))
do
   logs="${logs} nmslog.${d}.gz"
done

grep  ${search} nmslog
[ -n "${logs}" ] && zgrep ${search} ${logs}

Jean-Pierre.

Last edited by aigles; 05-22-2007 at 05:10 AM..
# 5  
Old 06-08-2007
Hey, never got a chance to say thanks for the replies all. I'm not able to post from work..

Anyways, ended up using TinWalrus' solution as it was the first one i tried that listed the log information in chronological order.

Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need ideas in shell script

Hi Gurus, I need a simple logic idea what can be done in the below shell script. I written a script to do a automated maintenance work on every month of 15th and I have scheduled it through the crontab. I need to send an alert email to the user before 24 hrs of that maintenance script run.... (5 Replies)
Discussion started by: ramkumar15
5 Replies

2. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

3. Shell Programming and Scripting

Learning project ideas - shell, python, UNIX tools, system administration

Hi guys, I am currently working as a system administration engineer, administering telecom applications on linux/unix platforms. I want to learn new things and improve the ones that i have and for this i though to really work on some project or something but i lack of ideas. I want to be... (2 Replies)
Discussion started by: capitanui
2 Replies

4. UNIX and Linux Applications

Need ideas for graduation project based on unix or linux Need ideas for graduation project based on

Dear all, i am in last year of electronics department in engineering faculty i need suggestions for a graduation project based on unix or free bsd or linux and electronics "embedded linux " i think about embedded unix for example or device drivers please i need helps (1 Reply)
Discussion started by: MOHA-1
1 Replies

5. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

6. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

7. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

8. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

9. Shell Programming and Scripting

Scripting ideas?

Hi All, How can I script the following logic? Step 1: Check if the file xyz.txt exists under direcotry test and if the size of the file xyz.txt is greater than 32MB. Step 2: If the above conditions are true(file exists and size >32 MB), then step 3, otherwise step 4 (file does not exist or... (2 Replies)
Discussion started by: Sueyoung88
2 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question