Westrock
Westrock

Reputation: 169

Perl: System command with both quotes and backticks

I am trying to send an OSX command from within Perl. However I am at a loss because the command contains both quotes and backticks, and I have not been able to figure out how to escape both characters and get a functional system command.

Here is the terminal command:
/usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'

Thanks for any insight!

Upvotes: 3

Views: 7034

Answers (2)

sferencik
sferencik

Reputation: 3249

You also mention backticks. Maybe you just meant single quotes (in which case el.pescado's answer is perfect), but in case you were after the backtick operator, you may want to use qx. The following are equivalent:

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'`

Upvotes: 2

You can use q quote-like operator, which works like ordinary single quote (or qq, equivalent to double qoute). This gives you advantage of using delimieters of choice.

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

You can also pass list to system, witch each argument to external program as list item. This way you do not have to quote arguments that would be otherwise interpreted by shell:

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

Upvotes: 10

Related Questions