?
Solved

shell_exec not working with sed

Posted on 2011-03-16
19
Medium Priority
?
995 Views
Last Modified: 2012-05-11
Hi All,

I have the code below, that works in SSH, but not via shell_exec() in PHP.


Any idea?
$output = exec("sed -i 's/oldtext/newtext/g' /etc/postfix/virtual");

Open in new window

0
Comment
Question by:detox1978
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
19 Comments
 
LVL 2

Author Comment

by:detox1978
ID: 35152978
The sample code from php.net works.


<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35153011
Try quoting it this way:

<?php
$output = exec('sed -i \'s/oldtext/newtext/g\' /etc/postfix/virtual');
echo "<pre>$output</pre>";
?>

Open in new window

0
 
LVL 2

Author Comment

by:detox1978
ID: 35153019
no luck.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 16

Expert Comment

by:sjklein42
ID: 35153035
I meant "shell_exec".

<?php
$output = shell_exec('sed -i \'s/oldtext/newtext/g\' /etc/postfix/virtual');
echo "<pre>$output</pre>";
?>

Open in new window

0
 
LVL 2

Author Comment

by:detox1978
ID: 35153052
no luck with shell_exec
0
 
LVL 2

Author Comment

by:detox1978
ID: 35153064
I found this code on the PHP.net website for returning the exit code.

It reruns a 4.  Any idea what that means?
<?php
$command = 'sed -i s/oldtext/newtext/g /etc/postfix/virtual'; 
// Execute the shell command 
$shellOutput = shell_exec($command.' > /dev/null; echo $?'); 
    
//return execute status; 
echo trim($shellOutput);
?>

Open in new window

0
 
LVL 2

Author Comment

by:detox1978
ID: 35153089
that should have said returns not reruns.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35153100
This is actually a killer of a problem that many have run into but none have solved that I could find.

Work around it by making a tiny script file containing your sed command, and then execute that script file using shell_exec instead of the naked sed command.

Does this work for you?
0
 
LVL 2

Author Comment

by:detox1978
ID: 35153109
I'm very new to this, could you give me a sample.

I would need to be able to pass variables.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35153198
Here's what I was thinking of.

Create a shell script file (mysedscript) with these lines:

#!/bin/bash

oldtext=$1
newtext=$2
whichfile=$3

sed -i 's/$oldtext/$newtext/g' $whichfile

Open in new window



Here's the php to call it.  You will need to put the right path in, of course.  And be sure to set Execute attribute on the script file.

<?php
$output = shell_exec('/path/to/my/script/mysedscript oldtext newtext /etc/postfix/virtual');
echo "<pre>$output</pre>";
?>

Open in new window

0
 
LVL 2

Author Comment

by:detox1978
ID: 35153267
It executes locally, but not via shell_exec.  It still returns the exit status of 4.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35153334
Does this get the same error?  (just echo the sed command but don't execute it):

#!/bin/bash

oldtext=$1
newtext=$2
whichfile=$3

echo sed -i 's/$oldtext/$newtext/g' $whichfile 

Open in new window

0
 
LVL 2

Author Comment

by:detox1978
ID: 35153345
no that returns a 0, which is the same as the working sample on php.net.
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 2000 total points
ID: 35153415
Tough one.  I hope you have patience with me if we try a few things.

Some of the bloggers have given up and use PHP to do the replace operation instead of sed but we're not quite there yet.

Let's assume for a minute that it is the "sed" command that is returning the 4 status.

I haven't been able to determine what "4" means in this context, but suspect it has to do either with (1) not able to find "sed" itself, or (2) not able to open the input file (virtual) either because of a path problem or a protection problem.

We may be able to narrow it down with a test.   Put a copy of the input file (virtual) in the same folder as the mysedscript script.  Change the whichfile argument in the php accordingly to point there instead of to /etc.  Also go back to the original (non-echo-test) version of mysedscript.

If this works, we know it has to do with file protection on virtual.

If not, I'm out of ideas right now.



0
 
LVL 2

Author Comment

by:detox1978
ID: 35153424
ok, I'll give that a go.

I'd happily use PHP, but the file isn't accessible from PHP is it?
0
 
LVL 2

Author Comment

by:detox1978
ID: 35153441
Interesting results.  the script works against a copy.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35153446
The php is running on the same machine as the virtual file, isn't it?
0
 
LVL 2

Author Comment

by:detox1978
ID: 35153463
I've got it working.  The folder it sits in needed write chmod.  I can now run it without the script and using;

sed -i s/oldtext/newtext/g /etc/postfix/virtual

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35153498
I guess we should have thought of that first.

;)
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

762 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