How to write If-else statement in one line in csh?
Asked Answered
M

4

5

I use Cshell and i'm writing an if else statement in oneLine.

If statement executed successfully and else block is going for infinite loop and cannot exit back to promt.

Any suggestion how it can be solved?

/home/sun_cdmasee>if ( 25 == 25 )  then ; echo "yes" ; else  echo "no" ; endif

yes

else?

else?

else?

Menarche answered 16/11, 2015 at 6:56 Comment(2)
Don't use csh. STFW for csh considered harmful then switch (using chsh) to some better shell like bash, zsh or fishSubtype
Come to learn more about tcsh and share your insights as well here: blogger.com/blog/posts/2001316915191381029?q=label%3AtcshStrabismus
L
11

Answering this one despite being old, because this is the first Google hit, and in case anyone comes looking for it, they're likely to end up here.

The manual might say it's impossible, but instead of a single if, the same functionality can be just achieved with oppositely formed ifs:

if ("a" =~ "b") echo "wrong"; if ("a" !~ "b") echo "right"

Same can be applied in aliases:

alias blabla 'if ("\!:1" =~ "bla*") echo "chat"; if ("\!:1" !~ "bla*") echo "quiet"'

And yes, after a decade+ of writing small stuff in tcsh, because it's used for just about anything, the "don't write in csh" suggestion should come with a ton of disclaimers, or better formed like "don't write serious scripts in csh". When it is your shell because other shells aren't supported in the relevant industry almost anywhere, these patronizing responses aren't doing anything useful.

Lax answered 15/2, 2021 at 19:48 Comment(1)
Amen to your final paragraph. As much as I appreciate the good intentions of the authors of such comments, its not solving my problem right now! Good chance we've heard it before now as well ... :) (bad tcsh user of over 3 decades...) P.S. Thanks for this solution. This issue with my very, very bad shell has bugged me for almost as long!Borodino
G
9

The man page says it's not possible:

(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.)

Don't try to program in csh. Don't even use it as your interactive shell.

Incidentally, you can exit back to your normal csh prompt by typing endif at the else? prompt.

Graduate answered 16/11, 2015 at 8:23 Comment(2)
My job (EE) requires (t)csh, so you should qualify your advice with "If you have a choice, ...". EE community got stuck with csh due to EDA getting start on SunOS (csh default).Kilk
Be the change you want to see in the world.Graduate
C
2

Yes, you can!
in tcsh (which is invariably available on recent Linux distributions, even MacOS)

if ( $?debug_log ) echo "verbose output for debugging purposes ${shell_variable}"
Cudweed answered 2/3, 2019 at 20:44 Comment(1)
can you give an example in tcsh which does more than check existence of a variable? as in the questioner's ( if "1" == "1" )? Or is this not possible.Shackleford
K
1

Note : you can exit infinite loop by typing "endif"

  • if several tests needed, can avoid opposite test

  • ";" "if" separator can be replaced by "&&"

  • for more than one command to execute, use eval to do it

  • for direct comparison, can use "==" :

    alias ggg 'if(\!:1 == bla)  eval "echo 'a'; echo 'x'"  ; if(\!:1 == xxx) eval "echo 'b' ; echo 'c'"'
    for wildcards, use "=~" :
       alias ggg 'if(\!:1 =~ "bla*")  eval "echo 'a'; echo 'x'"  ; if(\!:1 =~ "xxx*") eval "echo 'b' ; echo 'c'"'
    
  • for conditional operators, use "((" and "))" :

    alias ggg 'if((\!:1 =~ "bla*" || \!:1 == "tutu"))  eval "echo 'a'; echo 'x'"  ; if(\!:1 =~ "xxx*") eval "echo 'b' ; echo 'c'"'
    

beware : if not enough args, will lead to error like Bad ! arg selector.

Kibbutz answered 1/4, 2022 at 13:8 Comment(1)
I tried using eval with sed command to add a header to a txt file, but I keep getting sed command not recognized. Running the sed command outside the eval works in the terminal.Lulita

© 2022 - 2024 — McMap. All rights reserved.