Register
It is currently Tue Sep 23, 2014 10:23 pm

About arguments (yeah, another one)


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon Nov 10, 2008 12:31 pm   

Joined: Mon Nov 10, 2008 12:20 pm
Posts: 2
Location: Nice, France
Hi everyone,

I'm trying to get my very first script working...
Actually it's really simple. Just compile and run a source file, with the possibility to time it with the (optionnal) argument -t.

Here it is :

Code:
#!/bin/bash
# used with -t, it times the execution
file=bob.cpp
time=

while [ "$1" != "" ]; do
case $1 in
        -f | --file )   shift
         filename=$1
         ;;
    "-t" )    time=1
         ;;
   * )      ;;
esac
shift
done

if [ "$time" = "1" ];
then if [ g++ $file -O2 -Wall -o $file.out && time ./$file.out ];
     then rm $file.out fi
else if [ g++ $file -O2 -Wall -o $file.out && ./$file.out ];
     then rm $file.out fi
fi


I don't see exactly what's wrong... But I also have some questions :
In my case condition, why do I have to shift before assigning file to $1 (by the way it does not work...) ? I followed a tutorial, but I don't get this.
Is it the right way to do such a thing ?

Thanks et please forgive my poor english :)
cpa


Top
 Profile  
 PostPosted: Tue Nov 18, 2008 10:12 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Okey, lets start with the case.

Yes, you have to shift first, $1 still contains "--file" or "-f" if not shifted.
What shift does is basically just moving $1 to nothing, $2 to $1 and so on. (where $2 and so on is the next incoming parameter seperated by whitespace)

Secondly, i wouldn't use while [ "$1" != "" ]... use either this:
Code:
until [ -z $1 ]; do
   case .....
done

(-z says true if variable is null or zero)
or this
Code:
while [ $# -gt 0 ]; do
   case ....
done

$# is number of parameters (every time you shift it becomes -1)

in your first IF-statement you should use
Code:
if [ $time -eq 1 ]; then
   commands...
fi

Also, the string comparision sign is "==" and not "=". The differance between using sign operators and the ones like -eq is that sign operators are meant to use for strings and the others are used for numbers.

Hope this clarified some things. Still not sure what wouldn't work in your script but the && time ./$file.out thingie looks suspicious to me :P

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Nov 18, 2008 10:46 am   

Joined: Mon Nov 10, 2008 12:20 pm
Posts: 2
Location: Nice, France
Okay, it now works perfectly !
Thanks !


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 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