suaveant has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way, ideally in bash, but if not in another shell, to put perl between hitting enter after you have entered a command and before the shell does all its quoting and magic? Basically a way to pre-process the command before bash reads it and do things like change
s dave when you use $foo and " it interpolates #to s dave when\ you\ use\ \$foo\ and\ \"\ it\ interploates #basically s/^(s\s+\S+\s+)(.*)/$1\Q$2\E/;
so that any any s command the text after the name get shell quoted? I hope that is clear enough...

The idea is s is a system messager, and due to the shell if you misquote " or $ it can screw you up... but I know I always want them quoted for that particular command...

                - Ant
                - Some of my best work - Fish Dinner

Replies are listed 'Best First'.
Re: Command line preprocessing
by stefan k (Curate) on Aug 31, 2001 at 19:03 UTC
    Hi,
    I'm not sure if this could really work, but I'd take a look into /etc/inputrc (or ~/.inputrc) and try to fiddle around with the settings. Usually the return-key is bound to accept-line. Maybe one could set that to something like (pseudocode)
    kill-whole-line-to-register system-command-with-register-content-as-arg paste-system-command-output accept-line
    but I have never seen more than one command to be bound to a key neither the creation of own commands.
    Of course this would be readline and could thus be used in a variety of other programms (like gnuplot)
    Just my humble first guess...

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

      this looks promising... but I don't see anything with system-command in the readline docs... :(

                      - Ant
                      - Some of my best work - Fish Dinner

Re: Command line preprocessing
by jmcnamara (Monsignor) on Aug 31, 2001 at 19:13 UTC

    Perhaps you could do it in the Perl shell.

    From the docs:

    A substitution filter consists of a perl-style s/// operator instance. The Perl Shell will turn this into a line-by-line filter that performs the substitution on each line, and then prints the line. For example:
    ls | s/a/b/

    John.
    --

      Possibly... though I haven't been able to get into the perl shell... it just doesn't impress me so far...

                      - Ant
                      - Some of my best work - Fish Dinner

Re: Command line preprocessing
by clemburg (Curate) on Sep 01, 2001 at 18:45 UTC

    I do not know of any shell that has a feature that would allow one to do command preprocessing, but I have managed to persuade shell-mode in emacs to do what you want.

    Emacs shell-mode can be customized by setting some variables defined in the comint package. Put the following code in a file with ending ".el" (emacs lisp), and load it with the command "M-x load-file". After that, opening a shell buffer with "M-x shell" will give you a "shell" that does what you want. Quoting is achieved by enclosing the text to quote with "my-quote()".

    Emacs lisp code:

    (defun my-quote-function (s) "quote special chars in string" (shell-quote-argument s)) (defun my-input-sender (proc command) "specialized comint-input-sender function with extra quoting" (let ((quoting-regex "my-quote(\\(.*\\))")) (string-match quoting-regex command) (let ((string-to-quote (match-string 1 command))) (if (null string-to-quote) (comint-simple-send proc command) (let ((quoted-string (my-quote-function string-to-quote))) (string-match quoting-regex command) (comint-simple-send proc (replace-match quoted-string t t command))))))) (setq comint-input-sender 'my-input-sender)

    Works like this (excerpt from shell mode buffer):

    --> echo $foo --> echo my-quote($foo) $foo --> echo "This is not quoted but" my-quote("this") is This is not quoted but "this" is -->

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com