Running external programs inside an if then else


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running external programs inside an if then else
# 1  
Old 09-01-2011
Running external programs inside an if then else

Good morning,

First time poster. be gentle! Smilie

I do not know coding all that well but I can hack the hell out of example scripts! I just cant wrap my brain around this one.

Im trying to run two grep statements in the first IF statement and doing an AND between them. Something is going wrong.... Heres a sample of what I would like to do and what I have tried:

Code:
#!/bin/bash

if grep --quiet "WORD-IN-FILE" FILE-TO-CHECK && grep --quiet "WORD-IN-FILE" FILE-TO-CHECK; then

        echo both conditions are found
fi

Ultimately, im going out to a status web page, bringing down the index.html with WGET, AWKing the hell out of it then SEDing the hell out of the AWKs results and posting them to a new file. Then, I am running this file (above script) against that new file created by the previous script (which i have written already and it works well). The two GREP statements will look inside that new file created and find conditions and if those conditions are met, email me the results. I DO NOT want more than one email unless the status of the original index.html changes, then send me an email that it has changed.

Short Form: If status page shows abnormal status AND an email has not been sent yet, send an email alerting to the abnormal status and quit. Then (when the file is run again, every 15 minutes) if the status is STILL abnormal, know that you already sent an email and quit. If later checking results in NORMAL status, send an email alerting the status change and quit. Then (when the file is run every 15 minutes) is the status is still normal, know that you already sent an email and quit.

I have already written all the other programs (first prog to get status page and all the send email progs). I just need this one that figure out if an email has been sent. I have been playing around with creating a file and changing the 1 to a 0 and checking that and quitting when an email was sent but it got way too confusing!! Smilie

Thanks!!! I will be monitoring this forum all day to add any info that you would need to help me out writing this script.
# 2  
Old 09-01-2011
Is this what you are looking for:
Code:
mGrep1=$(grep -c "WORD-IN-FILE111" FILE-TO-CHECK)
mGrep2=$(grep -c "WORD-IN-FILE222" FILE-TO-CHECK)
if [[ "${mGrep1}" != "0" && "${mGrep2}" != "0" ]]; then
  echo both conditions are found
fi

# 3  
Old 09-01-2011
thank you very much for the reply. I was playing around with variables but couldnt get the if then syntax down. I imagine the "0" means that it is equalling true?

I will try that now! Smilie

I was close:

Code:

GRP1=$(grep --quiet "Outtage" newfile)
GRP2=$(grep --quiet "0" outage.txt)

if [[ $GRP1 == 0 && $GRP2 == 0 ]]; then

        echo both conditions met
fi

# 4  
Old 09-01-2011
Quote:
Originally Posted by mrplow2k69
Im trying to run two grep statements in the first IF statement and doing an AND between them. Something is going wrong.... Heres a sample of what I would like to do and what I have tried:

Code:
#!/bin/bash

if grep --quiet "WORD-IN-FILE" FILE-TO-CHECK && grep --quiet "WORD-IN-FILE" FILE-TO-CHECK; then

        echo both conditions are found
fi

There is nothing wrong with that syntax. In fact, it's preferable and simpler to directly utilize the exit status rather than complicate the code by storing it in a variable for subsequent testing (unless it's needed more than once, of course).

How is it failing? If you're using a large file, narrow it down if possible before sharing a sample of your input data. Also, copy-paste any error message and do not neglect to state what it is you're expecting (so that we can see how it deviates from your desired output).

If that code above is just a placeholder for the code that's actually failing, you should post the actual code.

Regards,
Alister
# 5  
Old 09-01-2011
Quote:
Originally Posted by alister
...In fact, it's preferable and simpler to directly utilize the exit status rather than complicating the code by storing it in a variable for subsequent testing (unless it's needed more than once, of course).
...
If you ever worked on maintenance, you would never make a statement as above.
# 6  
Old 09-01-2011
Quote:
Originally Posted by Shell_Life
If you ever worked on maintenance, you would never make a statement as above.
That response isn't in any way helpful. It would be appreciated if you'd elaborate with an example and rationale.

Regards,
Alister
# 7  
Old 09-01-2011
This is my entire code that is the latest revision before coming here for help:

Code:

#!/bin/bash
NOW=$(date)
GRP1=$(grep --quiet "Outtage" newfile)
GRP2=$(grep --quiet "0" outage.txt)

if [[ $GRP1 == 0 && $GRP2 == 0 ]]; then

        echo MyFax Outage on $NOW >> myfaxoutage.txt
        echo 1 > outage.txt
        ./senddownemail.sh
fi


if grep --quiet "Outtage" newfile; then

        echo MyFax Outage on $NOW >> myfaxoutage.txt
fi

#if grep --quiet "Normal" newfile | grep --quiet "1" outage.txt; then
#
#       echo MyFax Normal Status on $NOW >> myfaxoutage.txt
#       echo 0 > outage.txt
#       ./sendupemail.sh
#
#else

if grep --quiet "Normal" newfile; then

        echo MyFax Normal Status on $NOW >> myfaxoutage.txt
        echo 0 > outage.txt
        ./sendupemail.sh
fi

Legend:

myfaxoutage.txt - file that stores the reporting for later use
outage.txt = file that contains the 1 or 0 that tells the file if an email has been sent.
newfile - file that is created in the first step (the actual status page that WGET grabs and then gets AWKed and SEDed)

Let me know if you want me to post the code to any other files.

Thanks!! Smilie

---------- Post updated at 01:19 PM ---------- Previous update was at 12:34 PM ----------

This is my current file with the changes said above:

Code:
#!/bin/bash
NOW=$(date)
mGrep1=$(grep -c "Outtage" newfile)
mGrep2=$(grep -c "0" outage.txt)
mGrep3=$(grep -c "1" outage.txt)
mGrep4=$(grep -c "Normal" newfile)

if [[ "${mGrep1}" != "0" && "${mGrep2}" != "0" ]]; then

        echo MyFax Outage on $NOW >> myfaxoutage.txt
        echo 1 > outage.txt
        ./senddownemail.sh
else
        echo MyFax Outage on $NOW >> myfaxoutage.txt
fi

#if grep --quiet "Normal" newfile | grep --quiet "1" outage.txt; then
#
#       echo MyFax Normal Status on $NOW >> myfaxoutage.txt
#       echo 0 > outage.txt
#       ./sendupemail.sh
#
#else

if [[ "${mGrep3}" != "0" && "${mGrep4}" != "0" ]]; then

        echo MyFax Normal Status on $NOW >> myfaxoutage.txt
        echo 0 > outage.txt
        ./sendupemail.sh
else
        echo MyFax Normal Status on $NOW >> myfaxoutage.txt
fi

Now, the file runs BUT it puts that the status is normal AND there is an outage in the reporting file (myfaxoutage.txt). the outage.txt has a ZERO in it to start.

---------- Post updated at 01:26 PM ---------- Previous update was at 01:19 PM ----------

Asi look more into the file, its putting both echo statements in because neither the AND statements are true so it defaults to both the ELSEs..... hmmm. I could put 4 if statements in and that would be the end of it but i know there is a more efficient way. Ill put those 4 in to get it working (need it working) and hopefully we can figure this out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Interpretation of $variables inside programs run from a script

Hi, I am running a shell script that executes a program. Inside this program, variables are also referenced using a dollar symbol, eg. $a, $thething, and the shell script is misinterpreting them as variables relevant to the shell script rather than relevant to the program run from inside the... (2 Replies)
Discussion started by: TheBigH
2 Replies

2. Programming

Running programs from a Python script

Any idea how I can run a program from a phyton script? For example, in csh I will do $tpath/tsimplex base=$fbase data=$fdata restore=$frestore \ nxp=$nx nzp=$nz param=$param intp=$intp nlay=$nlay \ varp=$varp sigma0=$sigma maxiter=$maxiter tol=$tol \ ... (2 Replies)
Discussion started by: kristinu
2 Replies

3. Shell Programming and Scripting

Running programs

I have installed a software called GMT, then writing a script that call the new programs ./example02.sh but I am getting ./example02.sh: line 20: gmtset: command not found I have done ./configure make make install (0 Replies)
Discussion started by: kristinu
0 Replies

4. Shell Programming and Scripting

Running programs in perl

I am trying to run a program called GMT using perl. Cannot make to run. I have tried using exec("date"); as a test but when I use exec($try); nothing happens. #!/usr/bin/perl print "$#ARGV\n"; if ($#ARGV != 3) { print "usage: jcdplot.perl\n"; exit; } $h = $ARGV;... (1 Reply)
Discussion started by: kristinu
1 Replies

5. Shell Programming and Scripting

Running external script

I do have one script which needs to be run hourly and should generates a report using an "appadmin" user. But script locates in my home directory. Whenerver i executes this script from my home directory /home/jg55555 it prompts me for the password, which is not required since im assinging the... (6 Replies)
Discussion started by: raghunsi
6 Replies

6. UNIX for Advanced & Expert Users

calling external values inside awk command

I have a code as follows awk ' BEGIN{FS=OFS=","} { n=split($3,a1,"~") split($4,a2,"~") split($5,a3,"~") for(i=1;i<=n;i++) { print $1,$2,a1,a2,a3,date } }' file In the above code I need to add current date(date). How is this possible to call an date or a exported value... (13 Replies)
Discussion started by: tkbharani
13 Replies

7. UNIX for Advanced & Expert Users

running X-11 programs as root

Hello, I would like to run gedit as root while logged into my regular user account. When I try to launch gedit from the command line as super user, I get this message: Gtk-WARNING **: cannot open display: Any suggestions or word arounds? It would make my life a lot simpler to edit files... (8 Replies)
Discussion started by: Allasso
8 Replies

8. Shell Programming and Scripting

Finding The Number Of Programs That A Given User Running On A TERMINAL

How To Find The Number Of Programs That A User Running ON A GIVEN TERMINAL (4 Replies)
Discussion started by: venkata.ganesh
4 Replies

9. UNIX for Dummies Questions & Answers

installing/running programs.

Hey folks, i'm a total newbie at linux (only installed it yesterday) so don't be mad at me for querying this. I downloaded firefox, and unpacked the library files and binaries into /usr/lib/firefox. Now, am I correct in thinking that to run it, I need to enter the 'sh /usr/lib/firefox' command into... (4 Replies)
Discussion started by: shep
4 Replies

10. UNIX for Dummies Questions & Answers

running preinstalled programs

Im using the knoppix version of linux, which is booting straight off the cd, and it shows my c drive (partiton) that has all my programs i have installed in my xp operating system. Im wondering if theres a way i can play my games like lotr return of the king, or jedi academy, in knoppox. it shows... (2 Replies)
Discussion started by: jestra
2 Replies
Login or Register to Ask a Question