Search code examples
perlshquotestcsh

three levels of parenthesis/quoting in shell snippets in tcsh?


I'm using tcsh; I want to run some snippet from sh on the command line, which itself contains a perl snippet, which contains some strings that are to be printed.

This results in three levels of parentheses, but there are only two available — " and '.

Is there a way around?

tcsh# sh -c 'while (true); do mtr --order "SRL BGAWV M" …; hping --icmp-ts --count 12 … | perl -ne '... if (/tsrtt=(\d+)/) {print $0,"\t"…}' ; done'


Solution

  • To include a single quote inside of single quotes, use '\''. e.g.

    perl -ne'... print $0, "\t" ...'
    

    becomes

    sh -c '... | perl -ne'\''... print $0, "\t" ...'\'''
    

    In this particular case, an alternative is to replace

    perl -ne'... print $0, "\t" ...'
    

    with

    perl -ne"... print \$0, qq{\t} ..."
    

    so you'd get

    sh -c '... | perl -ne"... print \$0, qq{\t} ..."'
    

    I'd just write the whole thing in Perl

    perl -e'
        while (1) {
           system("mtr", "--order", "SRL BGAWV M");
    
           open(my $pipe, "-|", "hping", "--icmp-ts", "--count", "12");
           while (<$pipe>) {
              ...
           }
        }
    '