[Gllug] Why does this script not work?

Nix nix at esperi.demon.co.uk
Mon Dec 10 22:06:53 UTC 2001


On Sun, 15 Jul 2001, tet at accucard.com said:
> The only way to do this is to run something in the context of the current
> process, rather than as a separate one. In Bourne related shells, you can
> use a shell function:
> 
> 	cdfunc()
> 	{
> 		pwd
> 		cd ..
> 		pwd
> 	}
> 
> If you put this in your ~/.bashrc, for example, then "cdfunc" will do
> what you want.

A common trick (`dircolors' uses it, for instance) is to have a shell
script that *emits shell script code* with `echo'; you then use `eval'
in the calling shell to suck up that shell script code and execute it.

viz

#!/bin/sh

# loads of computation goes here

echo "THIS_IS_A_COMPLEX_VARIABLE=$result_of_computation"

(or stick errors onto stderr and exit with an error code).

Then you hide the eval inside an alias, and voila, a `shell script' that
does anything you like, pretty much, but does it in the calling shell's
context.

The biggest problem with this is that you need to emit different shell
script code for different shells. I tend to say `use sh and ignore the
silly csh users', but I know I should really have it emit csh code too.

-- 
`The situation is completely under control. All of them were killed.'
     --- Alim Razim, for the Northern Alliance, demonstrating fine
         command of traditional Afghan prisoner control techniques.

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list