简体   繁体   中英

how do I edit a file on command line?

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

I can add following code to the php file

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.

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

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

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

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 /etc/samba/smb.conf

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

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)?

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.

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.

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

Take a look at parse_ini_file() function.

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM