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"

## 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:

grep a word?

Usage: grep -hblcnsviw pattern file . . .

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:

## 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

Hi Don,

Thanks for your reply. Below is my operating system info

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

Hi Don,

Thanks for your reply. Below is my operating system info

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

You could exploit grep 's behaviour to display the entire file if the search pattern is empty:

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"
1 Like