如何在命令行上编辑文件?

时间:2021-01-27 00:11:04

I am trying to create a php file that adds a user and create public_html directory in linux using exec() function(php).

我正在尝试创建一个php文件,添加用户并使用exec()函数(php)在linux中创建public_html目录。

I can add following code to the php file

我可以在php文件中添加以下代码

exec("useradd -d /home/username -m username"); exec("mkdir /home/username/public_html");

exec(“useradd -d / home / username -m username”); exec(“mkdir / home / username / public_html”);

now..I have to add public_html to smb.conf to work public_html on windows.

now ..我必须将public_html添加到smb.conf以在windows上运行public_html。

is it possible to edit smb.conf on command line?

是否可以在命令行上编辑smb.conf?

of course, I am going to use SSH as root to execute the php file.

当然,我将使用SSH作为root来执行php文件。

4 个解决方案

#1


Take a look at parse_ini_file() function.

看一下parse_ini_file()函数。

Then if you want to write inside the ini file try this:

然后,如果你想在ini文件里写,试试这个:

function writeini($file,$title,$item,$data) {
  $source = file($file);
  $t_match = "/^\[".$title."\]/i";
  $s_title = "";
  $c = "0";
  $o = "";
  foreach ($source as $temp) {
    $c++;
    if (preg_match("/^\[.+\]/i",$temp)) {
          if (preg_match($t_match,$l_title)) {
            $done = "yes";
            $f_write .= $item."=".$data."\n";
          }
          $l_title = $temp;
          $f_write .= $temp;
        } elseif (preg_match("/^".$item."=/i",$temp) && preg_match($t_match,$l_title)) {
          $done = "yes";
          $f_write .= $item."=".$data."\n";
          $l_title = "";
        } elseif ($c == count($source) && $done != "yes") {
          if (preg_match($t_match,$l_title)) {
            $f_write .= $temp.$item."=".$data."\n";
          } else {
                $f_write .= $temp."[".$title."]\n".$item."=".$data."\n";
          }
        } else {
          $f_write .= $temp;
        }
  }
  $ini_write = fopen($file,'w');
  fwrite($ini_write,$f_write);
  fclose($ini_write);
}

#2


EDIT: After reading Roy Rico's answer I believe I misunderstood the question.

编辑:在阅读了Roy Rico的回答后,我相信我误解了这个问题。

You can use either nano or vi. If you're a beginner then nano would be more straight forward. Simply use it like so:

您可以使用nano或vi。如果你是初学者,那么nano会更直接。只需像这样使用它:

nano /etc/samba/smb.conf

I believe that ctrl+o will save the file and ctrl+x will exit.

我相信ctrl + o将保存文件,ctrl + x将退出。

#3


Are you looking to edit the file using your php script executed on the command line, or are you going to use command line yourself to edit the command line (using an interactive shell)?

您是要使用在命令行上执行的php脚本编辑文件,还是要自己使用命令行编辑命令行(使用交互式shell)?

if you're trying to edit the file with your php script, you're gonna have to use the fopen, fwrite commands, plus give access to the file to your script, then you'll probably run into alot of security concerns, etc etc. Bottom line, it's more than a quick answer that can be given to you here.

如果你试图用你的PHP脚本编辑文件,你将不得不使用fopen,fwrite命令,再加上你的脚本访问文件,然后你可能会遇到很多安全问题,等等等等,这不仅仅是一个可以在这里给你的快速答案。

if you're using a interactive shell, then vi and emacs are great editors, yet very daunting if you've never used them before. pico and nano are friendlier to those who aren't familiar with CL editors.

如果你使用的是交互式shell,那么vi和emacs是很棒的编辑器,但如果你以前从未使用它们,那就太令人生畏了。 pico和nano对那些不熟悉CL编辑的人更友好。

#4


Most people use either Vim or Emacs as their command-line editor. Personally, I prefer emacs, but try both and see which you prefer.

大多数人使用Vim或Emacs作为命令行编辑器。就个人而言,我更喜欢emacs,但尝试两者并看看你喜欢哪个。

#1


Take a look at parse_ini_file() function.

看一下parse_ini_file()函数。

Then if you want to write inside the ini file try this:

然后,如果你想在ini文件里写,试试这个:

function writeini($file,$title,$item,$data) {
  $source = file($file);
  $t_match = "/^\[".$title."\]/i";
  $s_title = "";
  $c = "0";
  $o = "";
  foreach ($source as $temp) {
    $c++;
    if (preg_match("/^\[.+\]/i",$temp)) {
          if (preg_match($t_match,$l_title)) {
            $done = "yes";
            $f_write .= $item."=".$data."\n";
          }
          $l_title = $temp;
          $f_write .= $temp;
        } elseif (preg_match("/^".$item."=/i",$temp) && preg_match($t_match,$l_title)) {
          $done = "yes";
          $f_write .= $item."=".$data."\n";
          $l_title = "";
        } elseif ($c == count($source) && $done != "yes") {
          if (preg_match($t_match,$l_title)) {
            $f_write .= $temp.$item."=".$data."\n";
          } else {
                $f_write .= $temp."[".$title."]\n".$item."=".$data."\n";
          }
        } else {
          $f_write .= $temp;
        }
  }
  $ini_write = fopen($file,'w');
  fwrite($ini_write,$f_write);
  fclose($ini_write);
}

#2


EDIT: After reading Roy Rico's answer I believe I misunderstood the question.

编辑:在阅读了Roy Rico的回答后,我相信我误解了这个问题。

You can use either nano or vi. If you're a beginner then nano would be more straight forward. Simply use it like so:

您可以使用nano或vi。如果你是初学者,那么nano会更直接。只需像这样使用它:

nano /etc/samba/smb.conf

I believe that ctrl+o will save the file and ctrl+x will exit.

我相信ctrl + o将保存文件,ctrl + x将退出。

#3


Are you looking to edit the file using your php script executed on the command line, or are you going to use command line yourself to edit the command line (using an interactive shell)?

您是要使用在命令行上执行的php脚本编辑文件,还是要自己使用命令行编辑命令行(使用交互式shell)?

if you're trying to edit the file with your php script, you're gonna have to use the fopen, fwrite commands, plus give access to the file to your script, then you'll probably run into alot of security concerns, etc etc. Bottom line, it's more than a quick answer that can be given to you here.

如果你试图用你的PHP脚本编辑文件,你将不得不使用fopen,fwrite命令,再加上你的脚本访问文件,然后你可能会遇到很多安全问题,等等等等,这不仅仅是一个可以在这里给你的快速答案。

if you're using a interactive shell, then vi and emacs are great editors, yet very daunting if you've never used them before. pico and nano are friendlier to those who aren't familiar with CL editors.

如果你使用的是交互式shell,那么vi和emacs是很棒的编辑器,但如果你以前从未使用它们,那就太令人生畏了。 pico和nano对那些不熟悉CL编辑的人更友好。

#4


Most people use either Vim or Emacs as their command-line editor. Personally, I prefer emacs, but try both and see which you prefer.

大多数人使用Vim或Emacs作为命令行编辑器。就个人而言,我更喜欢emacs,但尝试两者并看看你喜欢哪个。