Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get csh to source a file and then go interactive?

Tags:

csh

BRIEF

how do I (1) start a new csh, (2) force it to execute a few commands that are NOT in any .cshrc (although I could arrange for them to be in a non-standard location to be source'ed) and (3) then go interactive?

E.g. is there any way to get csh or tcsh to sue alternate startup files other than those described at http://www.manpagez.com/man/1/tcsh/, which says

I already know about

Startup and shutdown A login shell begins by executing commands from the system files /etc/csh.cshrc and /etc/csh.login. It then executes commands from files in the user's home directory: first ~/.tcshrc (+) or, if ~/.tcshrc is not found, ~/.cshrc, then ~/.history (or the value of the histfile shell variable), then ~/.login, and finally ~/.cshdirs (or the value of the dirsfile shell variable) (+). The shell may read /etc/csh.login before instead of after /etc/csh.cshrc, and ~/.login before instead of after ~/.tcshrc or ~/.cshrc and ~/.history, if so compiled; see the version shell variable. (+)

   Non-login  shells read only /etc/csh.cshrc and ~/.tcshrc or ~/.cshrc

DETAIL

I'm a bash user. But I work at a hardware company, where (1) many people are csh users, and (2) many of the CAD tools depend on stuff in the environment, such as the modules system.

Many recipes - e.g. internal wiki pages where people explain how to do things - start off something like

start a new shell or xterm
source /proj/Foo/setup.proj
source ~some_engineer/env/setup-for-this-tool
now run the tool

Oftentimes the environment variables conflict; sometimes I have to delete all of my ~/.* files, ssh in afresh, etc.

I would like to automate many of these. Indeed, I have automated many of these. However, I had to automate them by using expect (actually, Perl CPAN Expect), to fake out being an interactive user.

Along the way, I have my own script clear-env that I often use to start a shell with almost no environment variables. Used thusly:

clear-env -keep HOME -- csh
and then either pipe commands in through expect
or run interactively

This works for automation. But sometimes I really do need to be interactive - but only after I have loaded several lines of long source script names.

I would like to be able to load thes source files, and then fall back to interactive.

E.g. I have tried

clear-env -keep HOME -- csh -c 'source filename' -i
...

or, without my clear-env

csh -c 'source filename' -i
...

hoping that it would execute the -c command and then become interactive. But it only executes the -c command.

I have been able to feed commands to csh via expect in a perl script of my own, that reads my tty, and then pipes the command into the csh via expect. However, then I lose the interactive features of csh.

Is there any better way? Some way of saying "From this point on, I want you to reconnect youir controlling terminal to this other terminal?"

Thanks.

By the way, this would be useful for other shells as well. However, it has been my experience that csh users are the most prone to writing recipes that must be manually executed.

--

Perhaps I am not being clear:

I know about

 exec tcsh -c "source stuff ; exec bash"

and exec csh -c "source stuff ; exec csh"

The situation is that I have already driven the tool, the script, a long way using "interactive" commands via Expect.

Now, after I have done that pseudo-interactive setup, finally I want to return to being truly interactive. Basically, changing the controlling terminal for the shell from the pty that Expect was using, to a real pty.

I have been able to do this by creating a forwarding process.

But I was hoping that I could switch. Or do something like

... long
... sequence
... of expect commands
exec csh -i < /dev/my-pty ...
like image 767
Krazy Glew Avatar asked Jan 24 '12 05:01

Krazy Glew


People also ask

How do I use Cshrc?

cshrc file can be read again by typing source Pathname , where the Pathname parameter is the path to the . cshrc file. To avoid problems with remote operations, the . cshrc file should not contain any functions that echo output unless they test for the $prompt variable, which signifies that the shell is interactive.

What is csh Cshrc?

The /etc/csh. cshrc file contains system-wide settings that are common to all shell users. It is used for setting shell variables and defining command aliases. Usually, it will set environment variables such as PATH.


2 Answers

You were on the right track. You can use expect to do this. The key is the statement interact.

#!/usr/bin/env expect

spawn tcsh

expect {
  > {
    send "cd /some/path/foobar\n"
  }
}
interact

... if > is in your prompt, of course. This is the main problem with using expect, but sometimes you're trapped in a situation with no other way out.

Also, you might want to take steps to not pollute your .history file with scripted commands, but you're probably already aware of that.

like image 69
clacke Avatar answered Oct 19 '22 18:10

clacke


I had the same problem. I'm using bash, and have a software package that only works if you are at a csh prompt and source a file called nemo_start. This is the solution:

me@machine:~/nemo$ csh -c "source nemo_start; csh" # This is bash.
FALCON set to /home/me/nemo/usr/dehnen/falcON
machine:~/nemo% tsf                                # This is csh already.
Insufficient parameters, try 'help=', 'help=?' or 'help=h' or 'man tsf',
Usage: tsf in=??? ...
type contents of a (binary) structured file

This tsf command would not have been available unless the file was sourced properly and environmental variables and aliases kept.

like image 34
fheshwfq Avatar answered Oct 19 '22 18:10

fheshwfq