[comp.unix.shell] One line If-then-else block in csh possible?

harichan@eecae.uucp (Ronald Harichandran) (02/14/91)

Is it possible to have an if-then-else-endif block on a single line?

I wish to do something like

if () then
  command
else
  command
endif

but wish to have it in a single line like

if () then, command, else, command, endif
--

Sincerely,

Ronald Harichandran

jbw@bigbird.bu.edu (Joe Wells) (02/15/91)

In article <1991Feb13.180658.16244@msuinfo.cl.msu.edu> harichan@eecae.uucp (Ronald Harichandran) writes:

  Is it possible to have an if-then-else-endif block on a single line [in csh]?

Yes, but it's a rather twisted/convoluted kludge:

  if ( [boolean condition] ) set status=1 && [else clause] || [then clause]

I always forget that I have to put the else clause first or negate the
condition.  Here's a simple example of this technique used in an alias:

    alias h \
    "  if ('\!*' != '') set status=1 && hi \\
    || if ('\!*' !~ [1-9]*) set status=1 && hi -\!* \\
    || history | grep -i '\!*' | tail"
    alias hi 'history | grep ............ | tail'

If you're just starting with the csh and you haven't already invested a
lot of time in customizing it, you're better of switching to bash or ksh.

-- 
Enjoy,

Joe Wells <jbw@bu.edu>

yeates@motcid.UUCP (Tony J Yeates) (02/15/91)

harichan@eecae.uucp (Ronald Harichandran) writes:

>Is it possible to have an if-then-else-endif block on a single line?

If () command

I found this buggy on the version of csh I used, so I don't use this form
anymore.  Also, I don't think you can use "else" with it.

>I wish to do something like
>if () then
>  command
>else
>  command
>endif
>but wish to have it in a single line like
>if () then, command, else, command, endif

The only thing I can think of is:-

if () then ; command ; else ; command ; endif

which when I try it does NOT work.

The csh man page says "The words else and endif must be the first nonwhite
characters on a line."  Oh, it also describes the if() command bug mentioned above.
Guess we're SOL!

calvin@sequent.UUCP (Calvin Goodrich) (02/16/91)

In article <5828@iron6.UUCP> yeates@motcid.UUCP (Tony J Yeates) writes:
>harichan@eecae.uucp (Ronald Harichandran) writes:
>
>>Is it possible to have an if-then-else-endif block on a single line?
>
>The only thing I can think of is:-
>
>if () then ; command ; else ; command ; endif
>
>which when I try it does NOT work.
>
>The csh man page says "The words else and endif must be the first nonwhite
>characters on a line."  Oh, it also describes the if() command bug mentioned above.
>Guess we're SOL!

for csh users, yes. for ksh users, no. in ksh you're allowed to use the form:

	if [ expr ]; then <command>; else <other command>; fi

unfortunately, it doesn't work that way for csh. believe me, i've tried
lots-o-different combinations of ;'s but it comes back and says "else: endif
not found". apparently csh requires that the endif be on it's own line.


have a day, eh.

calvin.


Calvin Goodrich   / calvin@sequent.sequent.com a\=k)4vk!%t@$+... <EOT> (beep!)

"Scotty, we need that .sig in thirty seconds or we're all dead!"
"Och, Captain! We canna do anathin'! It's still on yir other account!"

(Starfleet. It's not just an adventure, it's a job.)

tchrist@convex.COM (Tom Christiansen) (02/16/91)

From the keyboard of harichan@eecae.uucp (Ronald Harichandran):
:Is it possible to have an if-then-else-endif block on a single line?
:
:I wish to do something like
:
:if () then
:  command
:else
:  command
:endif
:
:but wish to have it in a single line like
:
:if () then, command, else, command, endif

You can often  work something out using /bin/test and the short-circuit
booleans:

    [ some test ] && success || failure

Evals might be coerced into helping -- haven't thought about that too much.

In general, your happiness with csh will be inversely proportional to how
much you try to do with it.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
 "All things are possible, but not all expedient."  (in life, UNIX, and perl)

sanders@peyote.cactus.org (Tony Sanders) (02/17/91)

In article <1991Feb16.142134.19813@convex.com> tchrist@convex.COM (Tom Christiansen) writes:
>From the keyboard of harichan@eecae.uucp (Ronald Harichandran):
>:Is it possible to have an if-then-else-endif block on a single line?
Sure, no problem:
    sh -c 'if [ 0 != 0 ]; then echo true; else echo false; fi'    :-)

>You can often  work something out using /bin/test and the short-circuit
>booleans:
>
>    [ some test ] && success || failure
so much for builtin test.

>In general, your happiness with csh will be inversely proportional to how
>much you try to do with it.
But it's so much fun trying to get it to work.

-- sanders@peyote.cactus.org
First rule of software:  Throw the first one away.
and so on...

tchrist@convex.COM (Tom Christiansen) (02/18/91)

From the keyboard of tchrist@convex.COM (Tom Christiansen):
:From the keyboard of harichan@eecae.uucp (Ronald Harichandran):
::Is it possible to have an if-then-else-endif block on a single line?
:
:You can often  work something out using /bin/test and the short-circuit
:booleans:
:
:    [ some test ] && success || failure
:

As was so kindly pointed out to me in private, that doesn't work.

If the "success" command fails, then the "failure" command will
be executed, which isn't what was asked for.  So to guarantee
that success always succeeds, add a subshell (gag) and append
a gratuitous true:

    [ some test ] && (success || true) || failure

Maybe it's time to switch shells so you can code your if's the 
way they were Meant to be. :-(

    if [ some test ] ; then success_cmd; else failure_cmd; fi

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
 "All things are possible, but not all expedient."  (in life, UNIX, and perl)