Bash if else better way


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash if else better way
# 1  
Old 04-20-2010
Bash if else better way

I have a bash script which receives form posts with some search criteria to filter through a log file and echo the results.

I use the "if else" way to pint out only what matches from the form input. It works very well, but now I need to add another sting to search on and my list will double in size. I was wondering if there was a better way.
Code:
LOGFILE=`if [ $MONTH = "all" ] && [ $uid = "all" ] && [ $ROUTER = "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{print $0;}' tmp/command_tool.log
else
         if [ $MONTH = "all" ] && [ $uid = "all" ] && [ $ROUTER != "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($0 ~ /'$ROUTER'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH = "all" ] && [ $uid != "all" ] && [ $ROUTER = "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($3 ~ /'$uid'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH = "all" ] && [ $uid != "all" ] && [ $ROUTER != "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($3 ~ /'$uid'/ && $5 ~ /'$ROUTER'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH != "all" ] && [ $uid != "all" ] && [ $ROUTER != "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($2 ~ /^'$date'/ && $3 ~ /'$uid'/ && $5 ~ /'$ROUTER'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH != "all" ] && [ $uid = "all" ] && [ $ROUTER = "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($2 ~ /^'$date'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH != "all" ] && [ $uid = "all" ] && [ $ROUTER != "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($2 ~ /^'$date'/ && $5 ~ /'$ROUTER'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH != "all" ] && [ $uid != "all" ] && [ $ROUTER = "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($2 ~ /^'$date'/ && $3 ~ /'$uid'/) print $0;}' tmp/command_tool.log
       fi
      fi
     fi
    fi
   fi
  fi
 fi
fi`

# 2  
Old 04-20-2010
nested if else can be written as..

Code:
if [..]; then
...
elif [..]; then
...
elif [..]; then
...
else
...
fi

# 3  
Old 04-20-2010
You did not provide any sample data on which to test, so the following is untested (though it should be semantically equivalent to your original post's code).

logsearch.awk:
Code:
BEGIN {FS="\n"; RS="#"}
m!="all" && index($2,d)!=1 {next}
u!="all" && index($3,u)==0 {next}
r!="all" && index($5,r)==0 {next}
{print}


Invocation:
Code:
nawk -f logsearch.awk d="$date" m="$MONTH" u="$UID" r="$ROUTER" tmp/command_tool.log

Just add new pattern-action pairs before the final print action in logsearch.awk, when it's time to add a new search field.

Regards,
Alister

Last edited by alister; 04-20-2010 at 03:14 PM..
# 4  
Old 04-20-2010
i would use binary logic to make a 3-bit variable
Code:
VAR=0
[ $MONTH = "all" ] && ((VAR+=4))
[ $uid = "all" ] && ((VAR+=2))
[ $ROUTER = "all" ] && ((VAR+=2))
case $VAR in
  7) EXPR="" ;;
  6) EXPR="0 ~ /'\$ROUTER'/" ;;
  5) EXPR="3 ~ /'\$uid'/" ;;
  4) EXPR="3 ~ /'\$uid'/ && \$5 ~ /'$ROUTER'/" ;;
  0) EXPR="2 ~ /^'\$date'/ && \$3 ~ /'\$uid'/ && \$5 ~ /'\$ROUTER'/" ;;
  3) EXPR="2 ~ /^'\$date'/" ;;
  2) EXPR="2 ~ /^'\$date'/ && \$5 ~ /'$ROUTER'/" ;;
  1) EXPR="2 ~ /^'\$date'/ && \$3 ~ /'\$uid'/" ;;
esac
# build the awk script
((VAR != 7)) && EXPR="if (\$$EXPR) "
LOGFILE=$( nawk -F "\n" "BEGIN {RS=\"#\"}{$EXPR print \$0;}" tmp/command_tool.log )

But i'm sure the awk code from alister is better...
It was just for the logic.
# 5  
Old 04-20-2010
Thanks alister, not sure how to implement your statements. Here is my entire script and a sample log.
Code:
#!/bin/sh
# Script which accepts input from logsearch.cgi and outputs Command Tool log
#
# The variables sent  in QUERY_STRING
ROUTER=`echo "$QUERY_STRING" | sed -n 's/^.*router=\([^&]*\).*$/\1/p'`
MONTH=`echo "$QUERY_STRING" | sed -n 's/^.*month=\([^&]*\).*$/\1/p'`
DAY=`echo "$QUERY_STRING" | sed -n 's/^.*day=\([^&]*\).*$/\1/p'`
TOOL=`echo "$QUERY_STRING" | sed -n 's/^.*tool=\([^&]*\).*$/\1/p'`
date="$MONTH$DAY"
uid=`echo "$QUERY_STRING" | sed -n 's/^.*uid=\([^&]*\).*$/\1/p'`
#
# Copy Command Tool log and remove blank lines and fold output to fit screen
cat /logs/command_tool.log | sed '/^$/d' | fold -s -w 120 >  tmp/command_tool.log
#
# Log file variable that searches the log depending on what is or is not matched
LOGFILE=`if [ $MONTH = "all" ] && [ $uid = "all" ] && [ $ROUTER = "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{print $0;}' tmp/command_tool.log
else
         if [ $MONTH = "all" ] && [ $uid = "all" ] && [ $ROUTER != "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($0 ~ /'$ROUTER'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH = "all" ] && [ $uid != "all" ] && [ $ROUTER = "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($3 ~ /'$uid'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH = "all" ] && [ $uid != "all" ] && [ $ROUTER != "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($3 ~ /'$uid'/ && $5 ~ /'$ROUTER'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH != "all" ] && [ $uid != "all" ] && [ $ROUTER != "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($2 ~ /^'$date'/ && $3 ~ /'$uid'/ && $5 ~ /'$ROUTER'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH != "all" ] && [ $uid = "all" ] && [ $ROUTER = "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($2 ~ /^'$date'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH != "all" ] && [ $uid = "all" ] && [ $ROUTER != "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($2 ~ /^'$date'/ && $5 ~ /'$ROUTER'/) print $0;}' tmp/command_tool.log
else
         if [ $MONTH != "all" ] && [ $uid != "all" ] && [ $ROUTER = "all" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{if ($2 ~ /^'$date'/ && $3 ~ /'$uid'/) print $0;}' tmp/command_tool.log
       fi
      fi
     fi
    fi
   fi
  fi
 fi
fi`
#
# The start of the html page
echo "Content-type: text/html"
echo ""
echo "<html>"
echo "<head>"
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'
echo "<title>Log Search Results</title>"
echo "</head>"
echo '<body>'
echo '<table cellspacing="8" cellpadding="0" width="100%" height="100%"><tr><td valign="top">'
echo '<pre>'
echo "$LOGFILE"
echo '</pre></td></tr></table>'
echo '</body></html>'

Code:
#
03-26-2010 17:13:50
op8873
SUBMITTED FROM URL: tool9.cgi
CISCO1.APPLWI.NET
sh ver
#
03-26-2010 20:05:10
mm9909
SUBMITTED FROM URL: tool6.cgi
CISCO5.DALLAS.NET
show ip int brief 
show int loopback0
#
03-27-2010 20:06:20
kb3233
SUBMITTED FROM URL: tool6.cgi
CISCO2.DALLAS.NET
show port 1/1
show port
#
03-29-2010 20:14:49
gb2887
SUBMITTED FROM URL: tool9.cgi
CISCO1.AKRNOH.NET
cont local 
show port
#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies

3. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

4. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

5. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :) ... (4 Replies)
Discussion started by: cmccabe
4 Replies

6. UNIX for Dummies Questions & Answers

Im new to bash scriping and i found this expression on a bash script what does this mean.

# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi espeacailly the top regex part? ---------- Post updated at 06:58 PM ---------- Previous update was... (1 Reply)
Discussion started by: kevin298
1 Replies

7. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

8. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

9. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

10. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies
Login or Register to Ask a Question