簡體   English   中英

Perl:帶引號和反引號的系統命令

[英]Perl: System command with both quotes and backticks

我試圖從Perl中發送一個OSX命令。 但是我很茫然,因為該命令包含引號和反引號,我無法弄清楚如何轉義這兩個字符並獲得一個功能系統命令。

這是終端命令:
/usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'

感謝您的任何見解!

您可以使用q quote-like運算符 ,其工作方式與普通單引號(或qq ,相當於double qoute)相同。 這使您可以使用選擇的分隔符。

my $cmd = q{/usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'};
system $cmd;

您也可以將列表傳遞給system ,將每個參數作為列表項。 這樣您就不必引用shell本可以解釋的參數:

system('/usr/bin/osascript', '-e', 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down');

你還提到了反引號。 也許你只是意味着單引號(在這種情況下el.pescado的答案是完美的),但如果你在反引號操作符之后,你可能想要使用qx 以下是等效的:

my @output = qx{/usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'};

my @output = `/usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM