Home  >  Article  >  Backend Development  >  Use php to call shell script to synchronize files

Use php to call shell script to synchronize files

WBOY
WBOYOriginal
2016-07-28 08:27:491231browse

当你在上传文件的时候需要同步到另外机器的时候,可以执行shell脚本实现文件同步

<code><span>#! /bin/bash</span>
a=<span>$1</span>
b=<span>$2</span>
ip=<span>$3</span>
port=<span>$4</span>
passwd=<span>$5</span>
/bin/rpm -qa|/bin/grep -q expect
<span>if</span> [ $? <span>-ne</span><span>0</span> ];<span>then</span><span>echo</span><span>"please install expect"</span><span>exit</span><span>fi</span><span>if</span> [ <span>$#</span><span>-ne</span><span>5</span> ];<span>then</span><span>echo</span><span>"must 5 parameter,1:source file,2:object file,3:object ip 4:object port,5:passwd"</span><span>exit</span><span>fi</span>
expect -c <span>"
  spawn scp -P <span>$port</span><span>$a</span> root@<span>$ip</span>:<span>$b</span>
  expect {
    \"*assword\" {set timeout 300; send \"<span>$passwd</span>\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
  }
  expect eof"</span></code>

运行脚本需要安装expect,直接yum安装即可
expect脚本是一种建立在tcl基础上的脚本语言,曝光率不高,却堪称shell脚本的好基友。expect脚本为交互而生,被设计为专门针对交互式程序的工具,常与对telnet、ftp、fsck、rlogin、tip、scp等配合使用
如果是从win上编辑过的脚本在linux下出现no such file or directory,可在linux下vi脚本执行

<code><span>:sef ff</span>=<span>unix</span></code>

在php执行shell脚本时,记得要写绝对路径

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 使用php调用shell脚本同步文件,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn