Run tcsh with arbitrary startup script

2

2

I want to create a new instance of tcsh and source an arbitrary script, all in one step. The problem is that when I use the -c option, the shell instance closes as soon as the script is complete. So, in the following trivial example the pushd command completes successfully but then the shell exits:

tcsh -c "pushd ~/some/directory/of/interest"

How can I source a script which modifies the environment and then work interactively within that environment? This is most useful when used in conjunction with programs like ssh or screen, as in the following:

ssh -t user@host 'tcsh -c "source ~/test_environment.csh"'

nispio

Posted 2015-08-22T01:36:19.150

Reputation: 337

Answers

4

Our solution will involve two steps.

  1. Pass Environment variable containing path to script you want to source to new tcsh instance.
  2. Have tcsh source the script pointed at by this environment variable

For step 1 ssh will run the command you specify in your default shell (the one in the destination server's /etc/passwd) so I'll give you several solutions for this.

  • If destination shell is sh/bash: Connect to ssh server with command: ssh -t user@host 'export SOURCESCRIPT=/tmp/tmp.sh; exec /usr/bin/tcsh'
  • If destination shell is csh/tcsh: Connect to ssh server with command: ssh -t user@host 'setenv SOURCESCRIPT /tmp/tmp.sh; exec /usr/bin/tcsh'
  • If you can modify the destination's ssh server config, add/modify AcceptEnv option in /etc/ssh/sshd_config to allow SOURCESCRIPT environment variable to be passed (e.g. AcceptEnv SOURCESCRIPT), set SOURCESCRIPT in local environment and connect with command: ssh -t -o SendEnv=SOURCESCRIPT user@host 'exec /usr/bin/tcsh'

For step 2, we modify ~/.tcshrc to add the following:

if $?SOURCESCRIPT then
    source $SOURCESCRIPT
endif

ssnobody

Posted 2015-08-22T01:36:19.150

Reputation: 2 510

This is a very nice, straight-forward workaround! It requires modifying the user's ~/.tcshrc beforehand, but that is not a problem in my scenario. – nispio – 2015-08-26T19:37:49.510

Glad to hear it was successful! Thanks for catching the error, fixed that equal sign. – ssnobody – 2015-08-26T21:07:07.103

-1

Correct me if I'm wrong, but can't you use the '-m' option for tcsh to load a startup script? I just read the man page and thought that you might be able to make the '~/.tcshrc' file a symlink to another script. In case there's something screwey going on with the commands, maybe you could try the '-e' or '-m' options.

Hope this helps!

EDIT: Or maybe you could use the '-i' option for interactive in conjunction with the '-c' option :)

Mr. Minty Fresh

Posted 2015-08-22T01:36:19.150

Reputation: 489

~/.tcshrc is my startup script, so I can't just replace it with a symlink. I am looking for a way to optionally run an alternate startup script. – nispio – 2015-08-25T18:49:19.590