Find the answer to your Linux question:
Results 1 to 8 of 8
Hi I am new to shell scripting. I was wondering how I would write an if-then statement fullfilling 2 conditions. So like if this and this is true, then do ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    18

    if-then shell script question..

    Hi

    I am new to shell scripting. I was wondering how I would write an if-then statement fullfilling 2 conditions. So like if this and this is true, then do this. I want it to read a file and if it finds that in the file there is a value that is 60 or greater and then name of the file matches, then email me. Here is what I have so far.

    FREESPACELOG=/tmp/freespace.sql
    if [ awk -F' ' '{ print $5 }' "${FREESPACELOG}" -ge 60.0];
    and '{print $1} -eq "TOOLS"
    then mail -s "PROD: Tablespace Free Space" bob@bob.edu
    fi

    Thanks.

  2. #2
    Just Joined!
    Join Date
    Mar 2009
    Posts
    31
    I believe what you are looking for is && allowing you to have two conditions for your if

    example:

    if condition1 && condition2 then

    you can check bash's man page for more on it.


    - John

  3. #3
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    First problem is the -ge in bash only works with integers. If the numbers are real (with a decimal point), you'll get a 'integer expression expected' error. To compare reals you'll have to do it in awk. Also, the && is the logical AND.

    The code would look something like:

    Code:
    FREESPACELOG=/tmp/freespace.sql
    if `awk -F' ' {if ($5 >= 60.0 && $1 == "TOOLS" ) exit; else exit 1}' ${FREESPACELOG}`;
    then mail -s "PROD: Tablespace Free Space" bob@bob.edu;
    fi

  4. #4
    Just Joined!
    Join Date
    Dec 2007
    Posts
    18
    Thanks.

    I tried that and I got the following error:

    ./newtest.sh: command substitution: line 2: syntax error near unexpected token `('
    ./newtest.sh: command substitution: line 2: `awk -F' ' {if ($5 >= 60.0 && $1 == "IIQLOB01" ) exit; else exit 1}' ${FREESPACELOG}'

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539
    I've not checked it, but from a quick glance at what you've posted the closing back tick in
    Code:
    FREESPACELOG}'
    is a single quote not a back tick `

    Just a personal preference but I prefer $(<command>) to `<command>`
    RHCE #100-015-395
    Please don't PM me with questions as no reply may offend, that's what the forums are for.

  6. #6
    Just Joined!
    Join Date
    Dec 2007
    Posts
    18
    Actually, and this is weird, but it is a back tick. When I checked the script it shows me a back tick, but when the error shows up it shows it as an apostrophe. Here is the script and then the error message:


    FREESPACELOG=/home/oracle/scripts/bin/free_space/freespace.sql
    if `awk -F' ' {if ($5 >= 60.0 && $1 == "IIQLOB01" ) exit; else exit 1}' ${FREESPACELOG}`;
    then mail -s "FPRCPROD: Tablespace Free Space" alert@wisc.edu;
    fi

    Error message:

    ./newtest.sh: command substitution: line 2: syntax error near unexpected token `('
    ./newtest.sh: command substitution: line 2: `awk -F' ' {if ($5 >= 60.0 && $1 == "IIQLOB01" ) exit; else exit 1}' ${FREESPACELOG}'

  7. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by shaseeb View Post
    Hi

    I am new to shell scripting. I was wondering how I would write an if-then statement fullfilling 2 conditions. So like if this and this is true, then do this. I want it to read a file and if it finds that in the file there is a value that is 60 or greater and then name of the file matches, then email me. Here is what I have so far.

    FREESPACELOG=/tmp/freespace.sql
    if [ awk -F' ' '{ print $5 }' "${FREESPACELOG}" -ge 60.0];
    and '{print $1} -eq "TOOLS"
    then mail -s "PROD: Tablespace Free Space" bob@bob.edu
    fi

    Thanks.


    Code:
    email="bob@bob.edu"
    subject="PROD: Tablespace Free Space"
    cmd="mailx -s $subject $email"
    awk  -v c="$cmd" '$5>=60 && $1 == "TOOLS"{ system(c) }'  "$FREESPACELOG"

  8. #8
    Just Joined!
    Join Date
    Dec 2007
    Posts
    18
    Thanks for the reply.

    I tried your script, but it doesn't seem to email me anything even when the condition is met. The $FREESPACELOG variable is actually a sql script that needs to be run first and then this script should check the contents of that script to match the condition. Does the command you gave me actually tell the sql script to run or does it just check the contents of the file?

    Here is how the script looks:

    FREESPACELOG=/home/oracle/scripts/bin/free_space/freespace.sql
    email="bob@bob.edu"
    subject="PROD: Tablespace Free Space"
    cmd="mailx -s $subject $email"
    awk -v c="$cmd" '$5>=60 && $1 == "IIQLOB01"{ system(c) }' "$FREESPACELOG"



    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...