mirror of
https://github.com/myvesta/vesta
synced 2025-08-14 18:49:21 -07:00
Create v-replace-in-file
This commit is contained in:
parent
a66d88c0a1
commit
66e003c6ab
1 changed files with 47 additions and 0 deletions
47
bin/v-replace-in-file
Normal file
47
bin/v-replace-in-file
Normal file
|
@ -0,0 +1,47 @@
|
|||
#!/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