I am trying to use sed to overwrite my index.php file, but I am getting an error:
$ sed -i \'s@@
If the use of sed is not a hard requirement, you can probably use the highly ignored command ed
.
ed -s index.php <<< $'%s@<head>@<head><script type="text/javascript" src="new.js"></script>@\nwq'
<command> <<< '<my_input_string>'
construct will pipe '<my_input_string>' into the <command>
as standard input$'...'
construct will be parsed for special characters. In this case the \n
splits the string into two linesStandard Input String:
%s@<head>@<head><script type="text/javascript" src="new.js"></script>@
wq
The first line is your regex search and replace as command for ed
. The second line tells ed
to write the changes and quit. You may recognize these commands from vi.
source: mrbluecoat.blogspot.com/2014/03/in-place-editing-right-way-hint-dont.html
Really no great answers here. Sorry if thread is dead, it came up in google searches and nothing really answered the question. Although altendky's answer is sort of the same as mine... There IS a workaround...find a world writable directory, say /tmp
cp /etc/file_to_mod /tmp
sed -i -e 's/foo/bar/g' /tmp/file_to_mod
cat /tmp/file_to_mod >/etc_file_to_mod
Hope that helps!
Seems like you have a permission issue on the /tmp
dir. (as discussed bellow, you run command in phpshell
, so TMP dir
can be setted elsewhere than /tmp
)
It should looks like :
$ ls -ld /tmp
drwxrwxrwx 333 root root 32768 12 oct. 03:13 /tmp/
explanations
When invoking sed
with -i
flag, sed
create a temporary file in /tmp
dir.
Proof with strace
:
$ strace -f -e trace=file sed -i 's/a/z/' /tmp/a
execve("/bin/sed", ["sed", "-i", "s/a/z/", "/tmp/a"], [/* 94 vars */]) = 0
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
(...)
open("/tmp/sedPSBTPG", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
rename("/tmp/sedPSBTPG", "/tmp/a") = 0
+++ exited with 0 +++
check for the /tmp
folder permission
It should have the following permission
drwxrwxrwt 7 root root 4.0K Nov 16 15:06 tmp
If it is not ok for you then run the following commands
sudo chown root:root /tmp
sudo chmod 1777 /tmp
once this is done, then put a sudo
infront of your sed
command to execute the command as a root user.
This will solve the issue
Short answer :
$ chmod +w .
And re-run sed
.
Long answer :
As already said, the problem come from the fact that you don't have write permission on .
(the current directory from which you run sed
). If you are on your own machine/drive then you surely can give yourself the permission to write on this directory, that's what the chmod
do here, unless you want protect it for some reason. However, if your on a network drive and can't change your permission on it then you should ask the one that own the directory to do it or use a workaround solution like copy-modify-past the file somewhere else.
I think you can you "sudo" in front of your command. Like sudo sed -i 's/geteuid/getppid/g' /usr/bin/vlc It worked for me.