Simple if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple if statement
# 1  
Old 07-02-2008
Simple if statement

Hi there,

Im new in the scripting world and ran into trouble yesterday. I want to make a small script that runs in crontab checking folders. If the folder size is greater than 10 (or what I set the variable to) then send a mail with a warning.

#!/bin/sh
set -x
A=8
C=`du -skh /share/MD0_DATA/Qmusic |cut -c 1-3`
echo $C
echo $A

if ('$C -ge $A') ##If C which is folder size 9,6 is larger than $A

then

echo "Qmusic quota has reached maximum - size is $C"
else
echo "Qmusic is using $C storage"

fi

## When i run the script this is my output:

[/] # ./quota.sh
+ A=8
++ du -skh /share/MD0_DATA/Qmusic
++ cut -c 1-3
+ C=9.6
+ echo 9.6
9.6
+ echo 8
8
+ '$C -ge $A'
./quota.sh: line 10: $C -ge $A: command not found
+ echo 'Qmusic is using 9.6 storage'
Qmusic is using 9.6 storage


It seems that the problem is the if statement. I tried with varius " ' [ etc but without any luck. What am I missing in the if statement?
# 2  
Old 07-02-2008
The if statement should looks like:

Code:
if [ "$C" -ge "$A" ]

Regards
# 3  
Old 07-02-2008
Hi Franklin


thanks for your time

changing the if statement brought new problems Smilie

[/] # ./quota.sh
+ A=8
++ cut -c 1-3
++ du -skh /share/MD0_DATA/Qmusic
+ C=9.6
+ echo 9.6
9.6
+ echo 8
8
+ '[' 9.6 -ge 8 ']'
./quota.sh: line 11: [: 9.6: integer expression expected
+ echo 'Qmusic is using 9.6 storage'
Qmusic is using 9.6 storage
# 4  
Old 07-02-2008
Oh just realized its because of the integer 9,6

What's an integer? That means whole numbers like 1, 2, 458, -2859. It does not mean fractional numbers like 0.5, .333, or 3.1415. If you must deal with fractional numbers, there is a separate program called bc which provides an arbitrary precision calculator language. It can be used in shell scripts, but is beyond the scope of this tutorial.


Okay now I have to use another method? in the if statement
# 5  
Old 07-02-2008
Oh its because of the integer value 9,6 its not a integer

What's an integer? That means whole numbers like 1, 2, 458, -2859. It does not mean fractional numbers like 0.5, .333, or 3.1415. If you must deal with fractional numbers, there is a separate program called bc which provides an arbitrary precision calculator language. It can be used in shell scripts, but is beyond the scope of this tutorial.




Okay so now what Smilie hmmm just have to do a little reading i guess
# 6  
Old 07-02-2008
If a string comparison is sufficient you can try:

Code:
if [ "$C" \> "$A" ]

Regards
# 7  
Old 07-02-2008
Code:
#!/bin/ksh

a=1.72
b=1.71

if [ "$(echo "if (${a} > ${b}) 1" | bc)" -eq 1 ] ; then
   echo ">"
else
   echo "<"
fi;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How would I construct a (possibly simple) IF statement?

Hi all, I thought this would be simple, but I've been having a lot of trouble trying to write this IF statement, if I may ask for help pls: In BASH, how would I construct the if statement: Should ONLY be true if USEROPTscript=="yes"]] AND $mode=="INSTALL" /or/ $mode=="CHANGE" ]]... (3 Replies)
Discussion started by: jmccoughlin
3 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Shell Programming and Scripting

Need help with failing simple if statement

So I'm writing a script that takes in arguments given from the shell. The last argument I'm using as a tag to tell my script, via if statements, to do one thing or another. A sample input would be: script.scr infile.txt outfile.txt F The "F" at the end there I expect to be one of two tags... (3 Replies)
Discussion started by: Malavin
3 Replies

4. Shell Programming and Scripting

Simple if statement help needed please

if ]; then echo "successssssssssssssssssss" $filename = "<font color='red'>$i</font>" else echo "failureeeeeeeeeeeeeeeeeeeee" $filename = "$i" fi; I'm just trying to see is this - read a file name and highlight... (2 Replies)
Discussion started by: vmanda
2 Replies

5. UNIX for Dummies Questions & Answers

Simple If then statement

Hello, I have never actually created a bash script but thought I would give it a try and got hung up fast.(stupid noob). In the script I have parsed a xml file to get a bunch of file prefixs out. I am then going to check a folder to determine if a file with the correct prefix exists. All the... (5 Replies)
Discussion started by: Dallasbr
5 Replies

6. Shell Programming and Scripting

Help with simple for statement please

Hi guys, I'm typing the following command, but I'm getting a different output than expected. for file in *20* do ls -l $file | wc -l done Instead of listing a total count of the files, it seems to be counting each file and then printing the number of the file - which is 1. How... (7 Replies)
Discussion started by: bbbngowc
7 Replies

7. Shell Programming and Scripting

simple if and then statement

guys, in one of my script, I want to include and a if and then statement which basically does the the following: if a file (constant location and filename) is greater in size than 0 bytes, then it should send out an email. I have tried the following so far with no luck: 1) if # is... (1 Reply)
Discussion started by: admininmaking
1 Replies

8. Shell Programming and Scripting

Using simple if statement in script

Hello, I am trying to create a simple script that will check the disk usage of a drive, and if it is over the limit it will delete the oldest ten files. everything is working except for when i implement the if statement i get the error /usb0: zero divisor. I am pretty sure that the du -k... (1 Reply)
Discussion started by: bpage
1 Replies

9. Shell Programming and Scripting

sh -help with case statement (should be simple)

I'm having an issue running multiple commands in a case statement. If i only run one command it works fine. Am I supposed to use double semi-colons after each statement or do i not need any at all? here is a snippet of the code: case `uname` in "Linux") echo This is linux. cat... (1 Reply)
Discussion started by: kuliksco
1 Replies

10. Shell Programming and Scripting

What's wrong with this simple statement?

I know that this is a ridiculously simple statement, but I am getting an error when I execute it, and I can't figure out what it is. Can anyone point me in the right direction? #!/bin/ksh integer dateMonth=0 integer intZero=0 if then dateMonth = 1 fi echo $dateMonth (7 Replies)
Discussion started by: mharley
7 Replies
Login or Register to Ask a Question