import java.io.File;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**运行shell脚本
*
*
*/
public class RunShellUtil {
private static final Logger logger=LoggerFactory.getLogger(RunShellUtil.class);
/**运行链接文件
* @param sourceFile
* @param distFile
* @return
* @throws Exception
*/
public static int runLinkFile(String sourceFilePath,String distFilePath) throws Exception{
logger.info(StringUtil.format("开始执行命令将 {} 硬连接 到{}", sourceFilePath,distFilePath));
String[] command=getLinkFileCommand(sourceFilePath,distFilePath);
int code = executeShellCommand(command);
return code;
}
/**链接文件夹
* @param sourceFilePath
* @param disFilePath
* @return
* @throws Exception
*/
public static int runLinkDirectory(String sourceFilePath,String disFilePath) throws Exception{
logger.info(StringUtil.format("开始执行命令将文件夹 {} 硬链接到 {}", sourceFilePath,disFilePath));
File sourceDirectory=new File(sourceFilePath);
File disDirectory=new File(disFilePath);
disDirectory.mkdirs();
File[] fileList=sourceDirectory.listFiles();
if(null == fileList || fileList.length == 0){
return 0;
}
for(File file:fileList){
String currentName=file.getName();
String absolutePath=file.getAbsolutePath();
String disAbsolutePath=joinFilePath(disFilePath,currentName);
if(file.isDirectory()){
runLinkDirectory(absolutePath,disAbsolutePath);
}else{
runLinkFile(absolutePath,disAbsolutePath);
}
}
return 0;
}
/**获取文件链接命令
* @param sourceFilePath
* @param distFilePath
* @return
* @throws Exception
*/
private static String[] getLinkFileCommand(String sourceFilePath,String distFilePath) throws Exception{
EPlatform platform=OSinfoUtil.getOsName();
String command[]=null;
if(platform.equals(EPlatform.Linux)){
sourceFilePath=FilePathUtil.processUniqueC(sourceFilePath);
distFilePath=FilePathUtil.processUniqueC(distFilePath);
//
// command="ln "+sourceFilePath+" "+distFilePath;
// command=StringUtil.format("/bin/sh -c ln {} {}",sourceFilePath,distFilePath);
command=new String[]{"/bin/sh","-c",StringUtil.format("ln {} {}", sourceFilePath,distFilePath)};
}else {
throw new FileOperateException("操作系统 {} 不支持文件链接",platform.toString());
}
return command;
}
/**移动文件
* @param sourceFilePath
* @param distFilePath
* @return
* @throws Exception
*/
public static int runMoveFile(String sourceFilePath,String distFilePath) throws Exception{
logger.info(StringUtil.format("开始执行命令将 {} 移动到 {}", sourceFilePath,distFilePath));
String command=getMoveFileCommand(sourceFilePath,distFilePath);
int code=executeShellCommand(command);
return code;
}
/**运行移动文件夹命令
* @param sourceFilePath
* @param disFilePath
* @return
* @throws Exception
*/
public static int runMoveDirectory(String sourceFilePath,String disFilePath) throws Exception{
logger.info(StringUtil.format("开始执行命令将文件夹 {} 移动到 {}", sourceFilePath,disFilePath));
String command=getMoveDirectoryCommand(sourceFilePath,disFilePath);
int code=executeShellCommand(command);
return code;
}
/**获取移动文件命令
* @param sourceFilePath
* @param fistFilePath
* @return
*/
private static String getMoveFileCommand(String sourceFilePath,String distFilePath) throws Exception{
EPlatform platform=OSinfoUtil.getOsName();
String command=null;
if(platform.equals(EPlatform.Windows)){
//windows
command="cmd /c move "+sourceFilePath+" "+distFilePath;
}else if(platform.equals(EPlatform.Linux)){
//linux
command="mv "+sourceFilePath+" "+distFilePath;
}else{
throw new FileOperateException(StringUtil.format("该系统 {} 不支持文件移动命令", platform));
}
return command;
}
/**获取移动文件夹命令
* @param sourceFilePath
* @param distFilePath
* @return
* @throws Exception
*/
private static String getMoveDirectoryCommand(String sourceFilePath,String distFilePath) throws Exception{
EPlatform platform=OSinfoUtil.getOsName();
String command=null;
if(platform.equals(EPlatform.Windows)){
//windows
command="cmd /c move "+sourceFilePath+" "+distFilePath;
}else if(platform.equals(EPlatform.Linux)){
//linux
command="mv -fr "+sourceFilePath+" "+distFilePath;
}else{
throw new FileOperateException(StringUtil.format("该系统 {} 不支持文件夹移动命令", platform));
}
return command;
}
/**运行复制文件命令
* @param sourceFilePath
* @param distFilePath
* @return
* @throws Exception
*/
public static int runCopyFile(String sourceFilePath,String distFilePath) throws Exception{
logger.info(StringUtil.format("开始执行命令将 {} 复制到 {}", sourceFilePath,distFilePath));
String command=getCopyFileCommand(sourceFilePath,distFilePath);
int code=executeShellCommand(command);
return code;
}
/**运行复制文件夹命令
* @param sourceFilePath
* @param distFilePath
* @return
* @throws Excetion
*/
public static int runCopyDirectory(String sourceFilePath,String distFilePath) throws Exception{
logger.info(StringUtil.format("开始执行命令将文件夹 {} 复制到 {}", sourceFilePath,distFilePath));
String command=getCopyDirectoryCommand(sourceFilePath,distFilePath);
int code=executeShellCommand(command);
return code;
}
/**获取文件复制命令
* @param sourceFilePath
* @param distFilePath
* @return
* @throws Exception
*/
private static String getCopyFileCommand(String sourceFilePath,String distFilePath) throws Exception{
EPlatform platform=OSinfoUtil.getOsName();
String command=null;
if(platform.equals(EPlatform.Windows)){
command="cmd /c Copy "+sourceFilePath+" "+distFilePath;
}else if(platform.equals(EPlatform.Linux)){
command="cp "+sourceFilePath+" "+distFilePath;
}else{
throw new FileOperateException("操作系统 {} 不支持文件复制",platform.toString());
}
return command;
}
/**获取复制文件夹命令
* @param sourceFilePath
* @param distFilePath
* @return
* @throws Exception
*/
private static String getCopyDirectoryCommand(String sourceFilePath,String distFilePath) throws Exception{
EPlatform platform=OSinfoUtil.getOsName();
String command=null;
if(platform.equals(EPlatform.Windows)){
//windows
command="cmd /c Copy "+sourceFilePath+" "+distFilePath;
}else if(platform.equals(EPlatform.Linux)){
//linux
command="cp -r "+sourceFilePath+" "+distFilePath;
}else{
throw new FileOperateException("操作系统 {} 不支持文件夹复制",platform.toString());
}
return command;
}
/**执行shell命令
* @param sourceFilePath
* @param distFilePath
* @return
* @throws Exception
*/
public static int executeShellCommand(String... command) throws Exception{
Process process = Runtime.getRuntime().exec(command);
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
StringBuffer b=new StringBuffer();
while ((line = input.readLine()) != null) {
b.append(b);
}
int code = process.waitFor();
logger.info(StringUtil.format("执行命令 {} 返回code {} 返回内容 {}", NormalUtil.join(command," "),code,b.toString()));
if(code != 0){
throw new Exception(StringUtil.format("执行命令 {} 出错,{}", command.toString(),b.toString()));
}
return code;
}
public static List<String> runShell(String shStr) throws Exception {
List<String> strList = new ArrayList<String>();
Process process;
process = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c",shStr},null,null);
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
process.waitFor();
while ((line = input.readLine()) != null) {
strList.add(line);
}
return strList;
}
/**文件路径连接
* @param paths
* @return
*/
public static String joinFilePath(String...paths){
if(null == paths || paths.length == 0){
return null;
}
return NormalUtil.join(paths, "/");
}
}