A Perl system call must send exactly both characte

2019-06-08 12:03发布

A Perl system call must send the following string to the UnixShell:

'"XYZ"'

In my Perl script I have used the following command:

system("cleartool mkattr -replace ATTRIBUTE '"$attribute"' lbtype:$label");

Everything is well passed to the Shell Unix, except both uses of the quote character:

'

Indeed,

cleartool mkattr -replace ATTRIBUTE

The above command is passed as it is exactly what I want. The Perl variables $attribute and $label are well interpreted. But I don't know what to do to obtain exactly:

'"XYZ"'

Here XYZ is the value of the Perl variable $attribute OS is AIX (Unix) and Shell is ksh. cleartool is the command line interface of Clearcase but no Clearcase skill is necessary to fix my problem.

2条回答
来,给爷笑一个
2楼-- · 2019-06-08 12:14

If you want to execute a system command and don't have to use any shell syntax like redirects, it's usually better and safer to use the list form of system:

system(
    'cleartool',  'mkattr', '-replace', 'ATTRIBUTE',
    qq{"$attribute"}, qq{lbtype:$label}
);
# or, if you really want to pass both types of quotes:
system(
    'cleartool',  'mkattr', '-replace', 'ATTRIBUTE',
    qq{'"$attribute"'}, qq{lbtype:$label}
);

See perldoc -f system

It's not clear from your question if you want to pass '"XYZ"' or "XYZ".

查看更多
Viruses.
3楼-- · 2019-06-08 12:22

See "Quote and Quote like Operators" and use qq{...}:

system(qq{cleartool mkattr -replace ATTRIBUTE '"$attribute"' lbtype:$label});

qq{...} is exactly like "..." except you can then use double quotes " in your string without escaping them.

You can use any character directly after the qq and must then use the same character to denote the end-of-string, i.e. qqX...X would work the same way. You would run into problems if your string contains Xes, so don't do that.

You can also use paired characters as delimiter ({}, (), <>) which is what you usually will see.

查看更多
登录 后发表回答