Register
It is currently Sat Nov 22, 2014 8:02 pm

IF procedure in a script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 11 posts ] 
Author Message
 PostPosted: Thu May 28, 2009 5:20 am   

Joined: Tue May 19, 2009 12:34 am
Posts: 8
Hello,

I have a file that contains an integer, e.g. 35.

I am trying to write a script that checks this file to see if the number is e.g. bigger than 50. If yes, then it should perform an action. If no, the script should end.

Please help :/


Top
 Profile  
 PostPosted: Thu May 28, 2009 6:14 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
this is actually quite simple if the file only contains a number. If there's something else in it you need to do some text formating steps.

Code:
#!/bin/bash
file=$1
if [ $(cat $file) -ge 50 ]; then
   echo "This action is done because the number is greater then or equal to 50"
else
   echo "This action is done because the number  is lesser then 50."
fi


Usage is ./script.sh filename.txt
file=$1 tells bash to store the first paramter (seperated by a whitespace character) in the variable "$file".

-ge = greater then or equal to
-le = lesser then or equal to
-gt = greater then
-lt = lesser then
-eq = equal to

These compares only integers, if you want string comparision then you need to use "==" style.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed Jun 10, 2009 6:45 am   

Joined: Tue May 19, 2009 12:34 am
Posts: 8
Hello,

Unfortunately at the first line i always get "Bad interpreter". After I removed the first line, I always get "unexpected end of file", always one line after end of the script.


Top
 Profile  
 PostPosted: Sat Jun 13, 2009 9:10 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
If you copied and pasted the code from here, it might have the Windows style ^M at the end of each line. You'll want to open the file in vim (maybe with the -b switch) and remove those.


Top
 Profile  
 PostPosted: Tue Jun 16, 2009 11:33 pm   

Joined: Tue May 19, 2009 12:34 am
Posts: 8
Thankyou. That was it.


Top
 Profile  
 PostPosted: Tue Jun 16, 2009 11:53 pm   

Joined: Tue May 19, 2009 12:34 am
Posts: 8
The numbers I am working with are in a format like "+35.0" without the " ".
The integer comparison doesn't work, and I don't think I do a greater/less than (etc) comparison with a string. What can I do? It would be enough if I could remove the + and .0


Top
 Profile  
 PostPosted: Wed Jun 17, 2009 2:44 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
sed time! :D

Code:
echo "+35.0" | sed -e "s/^+\([^\.]\+)\..*$"


Something like that might do the trick... thou it'll most likely not work unless the number actually is a floatingpoint style number :)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed Jun 17, 2009 3:19 am   

Joined: Tue May 19, 2009 12:34 am
Posts: 8
Code:
echo "+35.0" | sed -e "s/^+\([^\.]\+)\..*$"               
sed: -e expression #1, char 19: unterminated `s' command


:(

I just need to get (for example) +35.0 into a number I can do a greater/less than on.


Top
 Profile  
 PostPosted: Wed Jun 17, 2009 4:53 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
oh sry i missed abit of commands >_<

Code:
echo "+35.0" | sed -e "s/^+\([^\.]\+)\..*$/\1/"


Top
 Profile  
 PostPosted: Wed Jun 17, 2009 5:14 am   

Joined: Tue May 19, 2009 12:34 am
Posts: 8
Unfortunately now I get

Code:
sed: -e expression #1, char 23: Unmatched ( or \( 


:(


Top
 Profile  
 PostPosted: Wed Jun 17, 2009 5:26 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
doh another typo again... i really should test these things before i post them >_<

Code:
echo "+35.0" | sed -e "s/^+\([^\.]\+\)\..*$/\1/"


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP