including options for commands

including options for commands

Post by Charles A. Li » Thu, 11 Feb 1993 22:28:27



Hi,
        I have the following csh which allows me to compile a code
just by typing fort (the name of the shell) and the file (filename.f)

#!/bin/csh
# csh to compile fortran code: f77 -o file file.f
#

if ($#argv > 1) then
 echo " Usage: fort [ filename.f ]"
        exit 2
endif

set code=$1
set ocode = $code:r
echo " "
echo " Compiling $code, executable will be $ocode "
echo " "
/usr/lang/f77 -o $ocode $code
echo " "
# end csh

What I would now like to do is incorporate other options,
say -g -u -v.  I could set a variable to each possible option
like  
        set var1 = $2
        set var2 = $3
        etc

Then say /usr/lang/f77 -o $var1 $var2

But I do not wish to have a var for every possible option.

But I wish to do this an easier way, if possible.  

Also, how do I change my usage line (lines 1-4) to accomodate
this.

Thanks

Charles

--
------------------------------------------------------

          Department of Aerospace Engineering
       University of MD, College Park, MD 20742        

 
 
 

including options for commands

Post by Tom Christianse » Fri, 12 Feb 1993 05:59:39



:       I have the following csh which allows me to compile a code
:just by typing fort (the name of the shell) and the file (filename.f)

:
:#!/bin/csh
:# csh to compile fortran code: f77 -o file file.f
    ^
    |
    |
This is not a csh.  *This* is a csh:

    -rwxr-xr-x  2 root       481050 Feb  2 15:06 /bin/csh

:if ($#argv > 1) then
: echo " Usage: fort [ filename.f ]"
:        exit 2
:endif
:
:set code=$1
:set ocode = $code:r
:echo " "
:echo " Compiling $code, executable will be $ocode "
:echo " "
:/usr/lang/f77 -o $ocode $code
:echo " "
:# end csh

Ceci n'est pas un csh.

:What I would now like to do is incorporate other options,
:say -g -u -v.  I could set a variable to each possible option
:like  
:       set var1 = $2
:       set var2 = $3
:       etc
:
:Then say /usr/lang/f77 -o $var1 $var2
:
:But I do not wish to have a var for every possible option.

:
:
:But I wish to do this an easier way, if possible.  
:
:Also, how do I change my usage line (lines 1-4) to accomodate
:this.

I think what you need to do is buy a book on shell programming.
Meanwhile, here's a solution

    #!/bin/sh
    f77=/usr/lang/f77
    eval code=$"$#"
    ocode=`echo $code | sed s/\.f$//`
    echo
    echo Compiling $code, executable will be $ocode
    echo
    $f77 $* -o $ocode
    echo

--tom
--

"The number of UNIX installations has grown to 10, with more expected."
    - _The UNIX Programmer's Manual_, Second Edition, June, 1972.

 
 
 

including options for commands

Post by Ola Resell,T » Fri, 12 Feb 1993 20:21:28




Quote:>Hi,
>    I have the following csh which allows me to compile a code
>just by typing fort (the name of the shell) and the file (filename.f)

>#!/bin/csh
># csh to compile fortran code: f77 -o file file.f
>#

>if ($#argv > 1) then
> echo " Usage: fort [ filename.f ]"
>        exit 2
>endif

[stuff deleted]

Quote:>What I would now like to do is incorporate other options,
>say -g -u -v.  

[more stuff deleted]

In UNIX V.3.2, the command GETOPTS(1) parses command line options.
It supports rules of the command syntax standard.  I use this feature
in many scripts.
Briefly, getopts parses the options, and when done, it shifts the
command line arguments, and you end up with your "filename.f" in $1.

Check the man pages for details!

Quote:>Also, how do I change my usage line (lines 1-4) to accomodate
>this.

echo " Usage: fort [-guv] [ filename.f ]"
or
echo " Usage: fort [-gu] [-v opt_arg] [ filename.f ]"
   if the -v option requires an option-argument
etc.

>------------------------------------------------------

>          Department of Aerospace Engineering
>       University of MD, College Park, MD 20742        

Ola

--
Ola Resell,Trondheim Ingeniorhogskole,EDB-avdelingen,7005 TRONDHEIM, NORWAY

Privat: Per Sivlesv. 19 c, N-7024 TRONDHEIM     Tlf: +47-7-561440

 
 
 

including options for commands

Post by Bret Giddin » Tue, 16 Feb 1993 19:42:14



Quote:

>Hi,
>    I have the following csh which allows me to compile a code
>just by typing fort (the name of the shell) and the file (filename.f)

>#!/bin/csh
># csh to compile fortran code: f77 -o file file.f
>#

>if ($#argv > 1) then
> echo " Usage: fort [ filename.f ]"
>        exit 2
>endif

[stuff deleted ]

Quote:>/usr/lang/f77 -o $ocode $code
>echo " "
># end csh

[ stuff deleted ]

>Thanks

>Charles


Have you never heard of make?

Most versions of make support built-in rules to compile (say) a .f program to an
executable. It looks like you are running on a sun, so you should be able to type

funny-prompt% make fred

this will compile fred.f into fred. If you want to change the arguments that it uses
by default, then use the environment variables flags FFLAGS, LDLIBS and LDFLAGS to
alter the way make behaves. In a decent shell, you can use the construct:

funny-prompt% FFLAGS='-g -v -u' make fred

to compile just this time using debugging info etc.

This does not of course generalise to more than one file, but then neither does your
original script, and at this point you must/should use make, so get used to it now
and save yourself other headaches.

Bret

 
 
 

1. Is it possible to parse a command option-string into option words?

Hello, please help me.

In sh or ksh, I know getopt function may help to parse a command
option string into individual characters. Is there a way to parse a
command option string into option words?

For example I got to run like this:

do_sth.sh -NORMAL abc

I like do_sth.sh to recognize "NORMAL" as an option (and take abc as
some value), against other potential words, such as "URGENT" --
instead of only "N" against "U" that getopt may help do.

Please also let me know if I can do it in either sh, csh or ksh.

Thanks.

2. fix strange stack calculation for secondary cpus

3. SSI includes including relative includes?

4. Ktalk

5. Setting exec option for includes

6. KDE background damaged when window iconified.

7. 2.5.40 compile errors with my CONFIG options (included)

8. Microsoft Releases Windows CE 3.0 Source

9. XLF -g option struggles on include files

10. release with DHCP as included option

11. isofs: cruft option with volume_seq_no? (patch included)

12. Sun compiler and -I- option to include files

13. Options Includes, right?