Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to quote any script for use as an alias UNIX
This recent hint describes how to pause and resume a process or application. The automatic version of the hint involved a long, but single-line, perl script that I said could be made into an alias. However, that's easier said than done, since the script contains so many special symbols you have to escape them properly. Therefore, this hint is actually a standalone helper hint. If you want to make an alias out of a perl command or other complex script, you can let perl do it for you like this:
perl -we '$s=;print quotemeta $s'
Run this, then type (or paste) in any command, and it will print out an escaped version of the entered code that can then be aliased.

For example, to make the aobve brief perl script into an alias itself, we'll run it at the command line. It then expects you to type a single line of text followed by a carriage return. In this case, I will cut and paste the command itself. This then returns:
perl\ \-we\ \'\$s\=\ \<\>\ \;\ print\ quotemeta\ \$s\'\
Then we can type (cut and paste) the above into an alias like this:
alias qm perl\ \-we\ \'\$s\=\ \<\>\ \;\ print\ quotemeta\ \$s\'\
And voila, we now have a command to meta-escape any future commands we want to aliias. In particular, the script in the hint I previously submitted needs to be metaquoted:

qm perl -we 'die unless @ARGV;$SIG{INT} =sub {`kill -s CONT $p`; die qq:bye now\n:};($p,$s,$c) = ( @ARGV,2.5,0.5);($sig,$w) = (qq:CONT:,30);$c1=$c2=$e1=$e2=0;$e2=30;while (1) { last unless  grep{ /^\s*$p /} `ps x`;  ($x,$y,$z) = split /\s+/,`sysctl vm.loadavg`;  ($sig,$w,$e1,$e2) =(qq:CONT:,30,0,30) if ($z<$c);($sig,$w,$e1,$e2)=(qq:STOP:,300,300,0) if ($y>$s) ; `kill -s $sig $p` ; print STDERR qq/ $p: $x $y $z   $sig  S:$c1 C:$c2 sec                /,chr(13); sleep $w;$c1+=$e1;$c2+=$e2}'

This produces:

perl\ \-we\ \'die\ unless\ \@ARGV\;\$SIG\{INT\}\ \=sub\ \{\`kill\ \-s\ CONT\ \$p\`\;\ die\ qq\:bye\ now\\n\:\}\;\(\$p\,\$s\,\$c\)\ \=\ \(\ \@ARGV\,2\.5\,0\.5\)\;\(\$sig\,\$w\)\ \=\ \(qq\:CONT\:\,30\)\;\$c1\=\$c2\=\$e1\=\$e2\=0\;\$e2\=30\;while\ \(1\)\ \{\ last\ unless\ \ grep\{\ \/\^\\s\*\$p\ \/\}\ \`ps\ x\`\;\ \ \(\$x\,\$y\,\$z\)\ \=\ split\ \/\\s\+\/\,\`sysctl\ vm\.loadavg\`\;\ \ \(\$sig\,\$w\,\$e1\,\$e2\)\ \=\(qq\:CONT\:\,30\,0\,30\)\ if\ \(\$z\<\$c\)\;\(\$sig\,\$w\,\$e1\,\$e2\)\=\(qq\:STOP\:\,300\,300\,0\)\ if\ \(\$y\>\$s\)\ \;\ \`kill\ \-s\ \$sig\ \$p\`\ \;\ print\ STDERR\ qq\/\ \$p\:\ \$x\ \$y\ \$z\ \ \ \$sig\ \ S\:\$c1\ C\:\$c2\ sec\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \/\,chr\(13\)\;\ sleep\ \$w\;\$c1\+\=\$e1\;\$c2\+\=\$e2\}\'\

Which we can now cut and paste into an alias.

[robg adds: I really hope I didn't mess up any of the quoting when editing the script; I'm pretty sure I didn't, as it seemed to work in my testing, but who knows...]
    •    
  • Currently 1.57 / 5
  You rated: 3 / 5 (7 votes cast)
 
[5,489 views]  

A script to quote any script for use as an alias | 7 comments | Create New Account
Click here to return to the 'A script to quote any script for use as an alias' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to quote any script for use as an alias
Authored by: mike3k on May 19, '06 10:20:47AM

The <> is missing from the first command.



[ Reply to This | # ]
A script to quote any script for use as an alias
Authored by: SOX on May 19, '06 11:28:00AM
the initial command is mission the \< and \> characters. So it should have read

perl -we '$s=<>;print quotemeta $s'
But it's not a big deal since you can cut and paste the later command where it is being aliased directly rather than having to type it in

[ Reply to This | # ]
A script to quote any script for use as an alias
Authored by: SOX on May 19, '06 11:32:23AM
Also there whould be a carriage return in the example of using qm. that is you want to type

qm
and hit return after the "qm". At that point you want to type (cut and paste) in any line you want to convert followed by another carriage return.

It does not work if you put the command you want to metaquote on the same line as the qm.

[ Reply to This | # ]

A script to quote any script for use as an alias
Authored by: loren_ryter on May 19, '06 06:44:46PM

hmm.. this is totally arcane but very useful. but how would you make it into a bash alias that would quote for other bash aliases?

i understand that a bash alias would look like:
alias qm='perl....'



[ Reply to This | # ]
A script to quote any script for use as an alias
Authored by: loren_ryter on May 19, '06 06:47:35PM

sorry, i should have added that I tried in my .bashrc file:

alias qm='perl\ \-we\ \'\$s\=\<\>\;print\ quotemeta\ \$s\'\'

but when i type "qm" in the bash shell I get:
-bash: syntax error near unexpected token `;'



[ Reply to This | # ]
bash style
Authored by: SOX on May 20, '06 08:41:05PM
you don't want to quote it.

alias qm=perl\ \-we\ \'\$s\=\<\>\;print\ quotemeta\ \$s\'  
[\code]


[ Reply to This | # ]
A script to quote any script for use as an alias
Authored by: n8gray on May 23, '06 02:17:50PM

Is it supposed to always produce a trailing backslash?



[ Reply to This | # ]