Hello all,
I have a question regarding the "getopt" command within a tcsh script (but this is a more general question, not related to tcsh).
Here is my script
energy.tcsh
Code:
#! /bin/tcsh
set temp=(`getopt -s tcsh -o g:: o:: --long gaussian::,orca:: -- $argv:q`) #the space between "g::" and "o::" was included #intentionally, so this would not be replaced by a smiley icon.
if ($? != 0) then
echo "Terminating..." >/dev/stderr
exit 1
endif
eval set argv=\($temp:q\)
while (1)
switch($1:q)
case -g:
case --gaussian:
if ($2:q == "") then
set counter="*.log"
echo "Option GAUSSIAN" $counter
else
echo "Option GAUSSIAN, argument "\`$2:q\'
endif
shift; shift
breaksw
case -o:
case --orca:
if ($2:q == "") then
set counter="*.out"
echo "Option ORCA" $counter
else
echo "Option ORCA, argument "\`$2:q\'
endif
shift; shift
breaksw
case --:
shift
break
default:
echo "Internal error!" ; exit 1
ends
end
It is based on
https://github.com/blackberry/GetOpt...opt-parse.tcsh, the only example I could find that uses getopt in tcsh. In short:
I want to extract data (
note this part is not in the script yet) from files within a directory, and these files are output produced by certain software (Gaussian or Orca). I either want the script to operate on one selected file or on all files of that type (endings are .log or .out), so I thought I try around with some options to provide this information. "-o" or "--orca" was defined for one, "-g" and "--gaussian" as the options for the other program output.
For example
./energies -o should operate on all Orca output (= .out) files present in the directory, and likewise
./energies --orca should do the same.
./energies -g or
./energies --gaussian should operate on all Gaussian output (= .log) files in the directory.
This works for any example files present.
However, when
./energies -o file.out is invoked (note the space between the option and the argument), it does not pick up the argument "file.out". When there is no space, such as in
./energies -ofile.out this works fine:
Code:
>>>./energy.tcsh -o file.out
Option ORCA one.out three.out two.out
>>>./energy.tcsh -ofile.out
Option ORCA, argument `file.out'
On a similar note, how can I pass this argument on to the long option version?
As in
./energy.tcsh --orca file.out
From the command line I currently get
Code:
>>>./energy.tcsh --orcafile.out
getopt: unrecognized option '--orcafile.out'
>>>./energy.tcsh --orca file.out
Option ORCA one.out three.out two.out
In the parent script
getopt-parse.tcsh (see link) these questions refer to the -b and -c options. Why does the parsing not work for "-c more" but for -cmore. Why does --c-long 'wow\!*\?' not pick up the argument but yields --c-long without an argument. Why is the -b option flexible in this respect?
Code:
>>>./parse.tcsh -b hallo
Option b, argument `hallo'
Remaining arguments:
>>> ./parse.tcsh -bhallo
Option b, argument `hallo'
Remaining arguments:
I don't understand how this works for the getup command I must admit. I thought this was just controlled by the ":" (mandatory argument) and "::" (optional argument) characters in the getopt line.
How are arguments (here specifically filenames) passed on to the short (-o, -g, or -b, -c) or the long options (--orca, --gaussian, --b-long, --c-long), with or without a whitespace in between them.
I cannot see the difference in the script to be honest.
Thanks so much for your valuable input.
Kind regards
Tobias