Help with Shell Script to View Logs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Shell Script to View Logs
# 1  
Old 08-01-2017
Help with Shell Script to View Logs

Hi

I'm very new to unix shell scripting. Im also new here in this forum. I'm a SQL Server DBA but I'm slowly learning Oracle and Sybase DB. Our Oracle and Sybase are on Unix platforms. Im slowly learning Linux Admin and Shell Scripting to automate tasks.

I'm writing a script to view DB error logs. I'd like my script to ask me how many lines from the logs do I want to read and if I want to grep a word.

I use the if..else construct. the command after the "if" is working but after the "else", if blank, I just want my script to ignore the "grep"

Code:
## script
echo "Enter number of lines to view:"
read lines
echo "grep a word?"
read word
if [ word=$word ]
then
   tail -$lines /soft/prd/logs/sybase/SGPRD1_SQL.log | grep $word
else
   tail -$lines /soft/prd/logs/sybase/SGPRD1_SQL.log
fi


--Error:
Code:
grep a word?

Usage: grep -hblcnsviw pattern file . . .

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments.

Last edited by Don Cragun; 08-01-2017 at 03:52 AM..
# 2  
Old 08-01-2017
Quote:
Originally Posted by Ricky777
Hi

I'm very new to unix shell scripting. Im also new here in this forum. I'm a SQL Server DBA but I'm slowly learning Oracle and Sybase DB. Our Oracle and Sybase are on Unix platforms. Im slowly learning Linux Admin and Shell Scripting to automate tasks.

I'm writing a script to view DB error logs. I'd like my script to ask me how many lines from the logs do I want to read and if I want to grep a word.

I use the if..else construct. the command after the "if" is working but after the "else", if blank, I just want my script to ignore the "grep"

Code:
## script
echo "Enter number of lines to view:"
read lines
echo "grep a word?"
read word
if [ word=$word ]
then
   tail -$lines /soft/prd/logs/sybase/SGPRD1_SQL.log | grep $word
else
   tail -$lines /soft/prd/logs/sybase/SGPRD1_SQL.log
fi


--Error:
Code:
grep a word?

Usage: grep -hblcnsviw pattern file . . .

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments.
Hi Ricky777,
Please get into the habit of telling us what operating system and shell you're using when asking questions about shell scripts. Different shells (and, for some features, different versions of shells) behave differently.

Assuming that you're using a shell based on Bourne shell syntax, the = operator in the test command (as in [ string1 = string2 ]) has to be separated from the two strings being compared by space or tab characters. When all three operands are presented as one word, the test command just tests whether that single word in an empty string (which yields a false result) or a non-empty string (which yields a true result). Furthermore, what you want to know is whether the expansion of the variable $word is not an empty string; not whether the expansion is the string word. And, when expanding a variable that might include spaces or special characters (i.e., any user supplied input) you need to quote the expansion so the command invoked with that expansion will see the expansion as a single word.

Try this instead:
Code:
## script
echo "Enter number of lines to view:"
read lines
echo "grep a word?"
read word
if [ "$word" != "" ]
then
   tail -"$lines" /soft/prd/logs/sybase/SGPRD1_SQL.log | grep "$word"
else
   tail -"$lines" /soft/prd/logs/sybase/SGPRD1_SQL.log
fi

# 3  
Old 08-01-2017
Hi Don,

Thanks for your reply. Below is my operating system info

Code:
bash-3.2$ uname -a
SunOS ITS168 5.10 Generic_150400-49 sun4v sparc sun4v

I'm using Bash shell.

Okay. Im going to try out the script that you suggested.

Thank you

Best Regards,
Ricky777

Last edited by rbatte1; 08-01-2017 at 06:48 AM.. Reason: Added CODE tags
# 4  
Old 08-01-2017
Hi Don,

Thanks for your reply. Below is my operating system info

Code:
bash-3.2$ uname -a
SunOS ITS168 5.10 Generic_150400-49 sun4v sparc sun4v

I'm using Bash shell.

Okay. Im going to try out the script that you suggested.

Thank you

Best Regards,
Ricky777


---------- Post updated at 04:23 AM ---------- Previous update was at 04:20 AM ----------

Hi Don,

Thanks!! The script worked like magic!!!!

Best Regards,

Ricky777

Last edited by rbatte1; 08-01-2017 at 06:47 AM.. Reason: Removed multiple repeated replies and added CODE tags for output.
# 5  
Old 08-01-2017
You could exploit grep's behaviour to display the entire file if the search pattern is empty:
Code:
echo "Enter number of lines to view:"
read lines
echo "grep a word?"
read word
tail -"$lines" /soft/prd/logs/sybase/SGPRD1_SQL.log | grep "$word"

This User Gave Thanks to RudiC 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

Shell script for capturing FTP logs

I have a script #!/bin/bash HOST=ftp.example.com USER=ftpuser PASSWORD=P@ssw0rd ftp -inv $HOST <<EOF user $USER $PASSWORD cd /path/to/file mput *.html bye EOF the script executes sucessfully I need to capture the FTP logs to a logfile should contain FTP Login successful ... (1 Reply)
Discussion started by: rajeshas83
1 Replies

2. Shell Programming and Scripting

Managing logs in shell script

Hi, I need write a shell script which should be executed from the crontab every day. This shell script is running several other shell scripts , and each one of them is writing to its log file. Few of the the shell script are also connecting using ssh to some other users on remote machine , do... (1 Reply)
Discussion started by: Yoav
1 Replies

3. Solaris

How to view audit logs in Solaris?

Does anyone know if there is software written to view the audit logs generated by Solaris? I am referring the the logs created by auditd. It produces an unreadable log. I am familiar with auditreduce and praudit, but I am looking for something that produces a report, much like logwatch looks at the... (4 Replies)
Discussion started by: brownwrap
4 Replies

4. Shell Programming and Scripting

How to view a running Shell Script?

I ran a script, and then I closed SSH. I want to go back and view it as if I just ran it. Is this possible? (2 Replies)
Discussion started by: slayr288
2 Replies

5. Shell Programming and Scripting

Shell Script for GC Logs

Hi, I have a strange situation here, I want to archive gc.logs file, generated by a java application, the strange thing about gc.log file is is doesn't have any time/date stamp appended to it unlike other logs (catalina/access/error) and one more strange thing is when ever the application is... (6 Replies)
Discussion started by: Neeryan
6 Replies

6. Shell Programming and Scripting

Shell script for to view all users & groups history in root

Dear All, I want to know all users & group history in one file, for root terminal through shell or any other option (5 Replies)
Discussion started by: kpoobathi
5 Replies

7. UNIX for Dummies Questions & Answers

how to view dynamically updated managed server logs from the same putty window

Hi, I want to know how we can view two or three logs which are dynamically getting updated from a single/same putty window with tail commnad. Thanks. (7 Replies)
Discussion started by: reachsudha
7 Replies

8. UNIX for Advanced & Expert Users

How to view the unix logs in windows ??

Hi ! I have a FTP site, where I softlinked my server log file. Now I want to view the logs in IE as I do in unix Some kind of free tool should be there, Can somebody provide me a pointer. Thanks. (0 Replies)
Discussion started by: dashok.83
0 Replies

9. Shell Programming and Scripting

Shell script to view logs of a server

Please share a shell script to collect logs of a server (like cpu utilization, memory etc) for a perticular time interval by giving date, time and server name as input. (1 Reply)
Discussion started by: abhishek27
1 Replies

10. UNIX for Dummies Questions & Answers

view access logs telenet

Hello one of my sites i host is doing somewhat well in the search engines, and i would like to watch the raw log files while looged in with telenet is there a way to do this. thanx Mike (2 Replies)
Discussion started by: whothought1
2 Replies
Login or Register to Ask a Question