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
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
Check the variable $0 (the name of the file/shell in execution). Trying
echo $0
under bash or csh gives
/bin/bash
or
csh
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
.
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.
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.