mirror of
https://github.com/myvesta/vesta
synced 2025-08-20 13:24:25 -07:00
Update and rename v-replace-in-file to v-file-replace
This commit is contained in:
parent
756a2b0d27
commit
2a71edda37
2 changed files with 17 additions and 47 deletions
17
bin/v-file-replace
Normal file
17
bin/v-file-replace
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#!/usr/bin/php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
include ("/usr/local/vesta/func/main.php");
|
||||||
|
include ("/usr/local/vesta/func/string.php");
|
||||||
|
|
||||||
|
myvesta_check_args (3, 'find replace file');
|
||||||
|
|
||||||
|
echo "Find: ".$find."\n";
|
||||||
|
echo "Replace: ".$replace."\n";
|
||||||
|
echo "File: ".$file."\n";
|
||||||
|
|
||||||
|
$r=myvesta_replace_in_file($file, $find, $replace);
|
||||||
|
|
||||||
|
if ($r) echo "Replacing done!\n";
|
||||||
|
|
||||||
|
exit(0);
|
|
@ -1,47 +0,0 @@
|
||||||
#!/usr/bin/php
|
|
||||||
<?php
|
|
||||||
|
|
||||||
$current_user=exec('whoami', $output, $return_var);
|
|
||||||
if ($current_user != 'root') {echo "You must be root to execute this script\n"; exit(1);}
|
|
||||||
|
|
||||||
if (isset($argv)) {
|
|
||||||
if (count($argv)==4) {
|
|
||||||
$find=$argv[1];
|
|
||||||
$replace=$argv[2];
|
|
||||||
$file=$argv[3];
|
|
||||||
} else {
|
|
||||||
echo "Usage: v-replace-in-file 'findString' 'replaceString' file\n";
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function fix_backslashes($s) {
|
|
||||||
$s=str_replace("\\n", "\n", $s);
|
|
||||||
$s=str_replace("\\r", "\r", $s);
|
|
||||||
$s=str_replace("\\t", "\t", $s);
|
|
||||||
return $s;
|
|
||||||
}
|
|
||||||
|
|
||||||
$find=fix_backslashes($find);
|
|
||||||
$replace=fix_backslashes($replace);
|
|
||||||
|
|
||||||
echo "Find: ".$find."\n";
|
|
||||||
echo "Replace: ".$replace."\n";
|
|
||||||
echo "File: ".$file."\n";
|
|
||||||
|
|
||||||
if (!file_exists($file)) {
|
|
||||||
echo "ERROR: File not found!\n";
|
|
||||||
exit(3);
|
|
||||||
}
|
|
||||||
|
|
||||||
$buf=file_get_contents($file);
|
|
||||||
|
|
||||||
if (strpos($buf, $find)===false) {
|
|
||||||
echo "ERROR: String not found!\n";
|
|
||||||
exit(4);
|
|
||||||
}
|
|
||||||
|
|
||||||
$buf=str_replace($find, $replace, $buf);
|
|
||||||
$r=file_put_contents($file, $buf);
|
|
||||||
if ($r) echo "Done.\n";
|
|
||||||
exit(0);
|
|
Loading…
Add table
Add a link
Reference in a new issue