On Mon, 22 Dec 2008, Gabe Turner wrote:

> On Fri, Dec 19, 2008 at 06:09:59PM -0600, Mike Miller wrote:
> [snip]
>> Have any of you done this kind of thing before?  Let me know if you know
>> of code for converting scripts or have ideas on how to make the thing
>> below do more or work better.
>
> I don't know about your scripts, but most of the ones I write have a 
> bunch of if statements and cases in them.  Such statements can be a bit 
> tricky in bash at first due to all of the implicit typing.  A few 
> pointers:
>
> if [[ ... ]]; # this is a conditional expression
>
>    Example:
>
> 	if [[ $foo == "bar" ]]; then echo $foo; fi
>
> if (( ... )); # this is an arithmetic expression
>
>    Example:
>
> 	if (( $num < 10 )); then echo "Oops!"; exit; fi
>
> (( )) is also a nice way to do boolean conditions in bash, I've found.
> let's say you want to exit if a command has failed:
>
>    mv foo bar;
>
>    if (($?)); then "Couldn't rename foo!"; exit 1; fi
>
> $? is the exit status of the last command run.  If it's 0, i.e. the last 
> command exited without error, the if will be false.


Thanks, Gabe.

I didn't use many "if" statements in my tcsh scripts though I have used 
them more often in bash scripts, so my need to translate "if" from tcsh to 
bash is not strong, but I might add a few lines to my little translator. 
See man page sections below.  I could at least change "else if" to "elif", 
put semicolons in the right places, change "endif" to "fi" and a few other 
things to make it easier to convert.

Thanks for the idea.  I suppose there are also "while" loops to deal with.

P.S. I don't suppose "tcsh" stands for "Twin Cities SHell"?!  OK, I know 
it doesn't.  The "c" stands for "C" and the "t" stands for "TENEX."

Best,
Mike


TCSH:

      if (expr) command
              If expr (an expression, as described  under  Expres-
              sions)  evaluates  true,  then  command is executed.
              Variable substitution on command happens  early,  at
              the  same  time  it  does  for  the  rest  of the if
              command.  command must be a simple command,  not  an
              alias, a pipeline, a command list or a parenthesized
              command   list,   but   it   may   have   arguments.
              Input/output  redirection  occurs  even  if  expr is
              false and command is thus not executed;  this  is  a
              bug.

      if (expr) then
      ...
      else if (expr2) then
      ...
      else
      ...
      endif   If the specified expr is true then the  commands  to
              the  first  else are executed; otherwise if expr2 is
              true then the commands to the second else  are  exe-
              cuted,  etc.  Any number of else-if pairs are possi-
              ble; only one endif is needed.   The  else  part  is
              likewise  optional.   (The words else and endif must
              appear at the beginning of input lines; the if  must
              appear alone on its input line or after an else.)


BASH:

        if list; then list; [ elif list; then list; ] ... [ else list; ] fi
               The if list is executed.  If its exit status is  zero,  the
               then  list  is executed.  Otherwise, each elif list is exe-
               cuted in turn, and if its exit status is zero,  the  corre-
               sponding  then  list is executed and the command completes.
               Otherwise, the else list is executed, if present.  The exit
               status  is the exit status of the last command executed, or
               zero if no condition tested true.