0

I need to check if the script is running from bash or csh.

#!/bin/csh

if ( `echo $SHELL` != '/bin/tcsh' )
   echo 'Please run it in csh' 
   exit
endif

This code is giving

bash: g.csh: line 7: syntax error: unexpected end of file
Ashot
  • 10,807
  • 14
  • 66
  • 117
  • 1
    Uh, the hash-bang at the top should mean it always runs in csh. How are you invoking the script? – Mike Weller Mar 22 '13 at 14:50
  • No, I some csh scripts I cant run it simply, At first I need to run csh in bash, then source the script – Ashot Mar 22 '13 at 14:52
  • 1
    If someone tries to run your `csh` script with a different, incompatible shell, I'd say that's their problem, not your script's. – chepner Mar 22 '13 at 14:52
  • If `$SHELL` has the wrong value, why do you think that shell will be able to execute the `if` statement in the first place? – chepner Mar 22 '13 at 14:59
  • 3
    `$SHELL` is the user's login shell, not the current running shell. – jordanm Mar 22 '13 at 15:12
  • @jordanm Awesome. That explains an issue I was having with [pow](https://github.com/37signals/pow/issues/297#issuecomment-15207107) – mraaroncruz Mar 22 '13 at 15:21
  • I'm not sure you can assume any meaning for the value of `SHELL`; it's not mentioned in the POSIX specification. – chepner Mar 22 '13 at 15:38
  • 3
    Time to point out that if you're programming scripts for the C shell, the first problem is precisely that: you're using the wrong shell. Use a real shell and leave sea shells on the C shore. See [Csh Programming Considered Harmful](http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/) too. The shebang line (which should be consistent with the test, but isn't) ensures that if someone invokes `g.csh` as a plain command like that, they get the correct shell executed. If they run `bash g.csh`, then the syntax error is appropriate and don't fret about it; they're misusing the command. – Jonathan Leffler Mar 22 '13 at 16:16

4 Answers4

6

Check the variable $0 (the name of the file/shell in execution). Trying

echo $0

under bash or csh gives

/bin/bash

or

csh
Fritz G. Mehner
  • 16,550
  • 2
  • 34
  • 41
2

You are pretty restricted if you want to use syntax that runs in both bash and tcsh. In fact, my tcsh doesn't even seem to set SHELL, which may be why you think you're still in bash -- if I launch tcsh from bash, SHELL is still /bin/bash. That said, if you really need to check, something like this could work (caveat: linux-specific):

test `readlink /proc/$$/exe` != `which tcsh` && echo you must use tcsh && exit 1

This works in both shells. Also note, that since csh is provided by the same binary as tcsh (at least it's that way for me), this will check for either tcsh or csh.

FatalError
  • 52,695
  • 14
  • 99
  • 116
  • When I add this line to script and run it from bash, the script exits and also console window is closed :) – Ashot Mar 22 '13 at 15:54
  • @Ashol: well now that will teach them to use the wrong shell! ;) It sounds like you are sourcing this script. In that case, try changing `exit` to `return`. – FatalError Mar 22 '13 at 16:03
1

You seem to be missing a then. Try this.

#!/bin/csh

if ( "$SHELL" != '/bin/tcsh' ) then
   echo 'Please run it in csh' 
   exit
endif

Though it seems like the commenters are correct in that the hashbang will force csh.

mraaroncruz
  • 3,780
  • 2
  • 32
  • 31
0

This line should work in nearly any shell

sh -c "[ \"$SHELL\" = \"tcsh\" ] || { echo \"Please run in tcsh\"; exit 1; }" || exit

but I'm not sure that this is the best way to solve whatever problem it is you are trying to solve.

chepner
  • 497,756
  • 71
  • 530
  • 681