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 ...
- 04-02-2009 #1Just 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.
- 04-02-2009 #2Just 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
- 04-02-2009 #3Linux 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
- 04-02-2009 #4Just 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}'
- 04-03-2009 #5Linux 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
is a single quote not a back tick `Code:FREESPACELOG}'
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.
- 04-03-2009 #6Just 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}'
- 04-04-2009 #7Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 04-06-2009 #8Just 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.


Reply With Quote
