Solved

Running linux shell commands from PHP - How to use escaped parenthesis in exec() ?

Posted on 2006-11-14
20
655 Views
Last Modified: 2013-11-18
I am trying to execute this command via PHP exec() function:
convert rose: \( +clone  -background navy  -shadow 80x3+5+5 \) +swap  -background none   -mosaic   shadow.png

The command is working well when I execute it in shell (Linux). However, I cannot get it working in any of exec(), shell_exec() or system()...
I have tried the following, without any success:

test-1:
$command = "convert rose: \\( +clone  -background navy  -shadow 80x3+5+5 \\) +swap  -background none   -mosaic   shadow.png";
exec($command);

test-2:
$command = "convert rose: ( +clone  -background navy  -shadow 80x3+5+5 ) +swap  -background none   -mosaic   shadow.png";
exec(escapeshellcmd($command));

These are the shell command line functions for ImageMagick. I can execute other complicated commands from PHP exec() *IF* they don't have any parenthesis.
If I don't escape parenthesis, linux throws a syntax error so I have to put backslashes before parenthesis, from PHP, but how ?
0
Comment
Question by:Buraque
  • 8
  • 6
  • 4
  • +1
20 Comments
 
LVL 9

Accepted Solution

by:
blue_hunter earned 500 total points
ID: 17944580
if you trying to execute a linux command via PHP.

lets say, you using "convert"

you would need to know the full path/ location where the "convert" is. (normally, it would be in /usr/bin/)
 
the command would be like
'/usr/bin/convert rose: ( +clone  -background navy  -shadow 80x3+5+5 ) +swap  -background none   -mosaic   shadow.png'



for your parenthesis problem,
i would suggest you to include the command with single quote instead of double quote
example:

'/usr/bin/convert rose: ( +clone  -background navy  -shadow 80x3+5+5 ) +swap  -background none   -mosaic   shadow.png'


0
 
LVL 1

Author Comment

by:Buraque
ID: 17944934
Thanks for the comments blue_hunter...
As I mentioned, I can use convert command if the command line have no parenthesis.
Convert and other ImageMagick commands are accessible from anywhere in entire server.

And single quotes are also not working.
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945028
have you tried using shell_exec()
0
 
LVL 1

Author Comment

by:Buraque
ID: 17945068
Yes zanus,
As I mentioned in my question, I have tried all exec(), shell_exec() and system() functions...
Thank you.
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945119
oh...didn't see that

does the exec command just return false....any errors.....any output whatsoever

try this and see what it gives you

$command = "convert rose: \\( +clone  -background navy  -shadow 80x3+5+5 \\) +swap  -background none   -mosaic   shadow.png";
exec($command, $output);
print_r($output);
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945159
or you could also try using echo with backticks

$command = `convert rose: ( +clone  -background navy  -shadow 80x3+5+5 ) +swap  -background none   -mosaic   shadow.png`;
echo $command;
0
 
LVL 1

Author Comment

by:Buraque
ID: 17945173
Good idea!

tried it, and it returned an empty array.
Which most probably means convert command is working but cannot recognize the parenthesis. So cannot perform the task correctly and returns an empty value ??.
0
 
LVL 1

Author Comment

by:Buraque
ID: 17945180
This web site must have "Edit Post" button grrr...

I wasn't thinking... convert command actually doesn't return anything... So empty array is pretty expected value.
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945209
I haven't really used linux in a while but does the stderr automatically get sent to stdout

If not you could try redirecting it and run the same $output array method

$command = "convert rose: \( +clone  -background navy  -shadow 80x3+5+5 \) +swap  -background none   -mosaic   shadow.png 2>&1";
exec($command, $output);
print_r($output);
0
 
LVL 9

Expert Comment

by:blue_hunter
ID: 17945265
had you tried execute the command with system()?


or modified version

$command = "convert rose: \\\( +clone  -background navy  -shadow 80x3+5+5 \\\) +swap  -background none   -mosaic   shadow.png 2>&1";
exec($command, $output);
print_r($output);


0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:Buraque
ID: 17945302
Sorry not working...

I just cannot get it...
I started to suspect PHP. Is this a bug or something?
PHP cannot sent a proper backslash character to the server?
0
 
LVL 6

Expert Comment

by:aescnt
ID: 17945332
()'s dont need to be escaped in PHP, system("echo testing (parentheses)"); shows the right result for me.
Backslashes do need to be escaped in PHP strings though: system("echo testing \\backslash\\"); shows "testing \backslash\" for me.

So why is it "not working" -- what type of error did you get?
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945360
just wondering, but what happens when you use 3 backslashes......
0
 
LVL 9

Expert Comment

by:blue_hunter
ID: 17945370
(before)      ----->      \\\(    
(after)        ------>      \(

0
 
LVL 6

Expert Comment

by:aescnt
ID: 17945383
To answer zanus:

simply 3 backslashes (like "\\\") will produce a parse error.
However, if you're using 3 backslashes and a character after it "\\\(", it's like combining "\\" and "\(". the first results in a backslash, the 2nd results in (. So:
echo "\\\("; // result: \(

Also if you use something like "\\\n" that will be the same as "\\" and "\n", of course \n being the newline meta character:
echo "\\\n"; // result: a backslash followed by a new line
0
 
LVL 3

Expert Comment

by:zanus123
ID: 17945414
hehe....I knew what it did already..
I was actually asking Buraque how it affected his query..

but thanx for enlighting me though.
0
 
LVL 1

Author Comment

by:Buraque
ID: 17945448
New info:

It may not be an issue with the parenthesis at all...

I tried this simple command:
exec("convert /home/stcom/public_html/test/shadow.png  /home/stcom/public_html/test/shadow.jpg");
It is working both from linux shell and from PHP...

But this command:
exec("convert /home/stcom/public_html/test/sample1b.png +clone -background green -shadow 80x3+5+5 +swap -background none -mosaic /home/stcom/public_html/test/shadow.png");
Working from linux shell, but not produce any results from PHP...
0
 
LVL 1

Author Comment

by:Buraque
ID: 17945591
Veeerrry confusing...
this complex command also working in both PHP and Shell

exec("convert -size 320x85 xc:transparent -font /home/stcom/fonts/ariblk.ttf -pointsize 72 -draw \"text 25,60 'MiMi'\" -channel RGBA -gaussian 0x6 -fill darkred -stroke magenta -draw \"text 20,55 'MiMi'\" /home/stcom/public_html/test/fontfuzzy112.png");
0
 
LVL 1

Author Comment

by:Buraque
ID: 17948776
Finally!!

I found that I have 2 convert commands in my server. First one is in /usr/bin version 6.0, second one is in /usr/local/bin version 6.3
PHP sees the first, bugged  one, in the path, linux sees the second one...

When I entered the path to the exec command, problem is solved
exec("/usr/local/bin/convert -size 320x85 xc:transparent.........");

Blue_hunter mentioned about it first, so points goes to him/her :)
0
 
LVL 9

Expert Comment

by:blue_hunter
ID: 17952715
cool! glad to hear that you solved your problem.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now