需求

通过sed指令实现对配置文件的修改,自定义一些java的api实现对linux后台配置文件进行修改

指令

  • 添加文件内容

描述:<sed -i '$a追加内容' 文件名称>向指定文件中添加内容,支持正则表达式

例:

# 向文件中追加指定内容
sed -i '$a追加的内容' /test.txt
  • 删除文件内容

描述:< sed -i '/删除行/d' %s>删除指定文件的内容,支持正则表达式

例:

# 删除文件中的
sed -i '/xxxx/d' /test.txt
  • 修改文件内容

描述:< sed -i 's/替换字符/新字符/g' %s>修改指定文件的内容,支持正则表达式

例:

# 修改文件中的内容
sed -i 's/oldStr/newStr /g' /test.txt

实现

package net.i2safe.modules.sysconf.utils;

import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

/**
 * @author  Li Wei
 * @Time  2021.08.06
 */
@Slf4j
public class CmdIUtil {
    private static String ERROR_MSG = "执行失败";

    /**
     * 指令linux指令
     * @param cmd
     * @return
     * @throws InterruptedException
     * @throws IOException
     */
    public static String exeCmd(String cmd) throws InterruptedException, IOException {
        log.info("开始执行 " + cmd + " 命令");
        int exitVal;
        String[] b={"sh","-c",cmd};
        Process process = Runtime.getRuntime().exec(b);
        InputStreamReader ir = new InputStreamReader(process.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);
        String line;
        String res = "";
        while ((line = input.readLine()) != null) {
            res += line + "<br>";
        }
        log.info("执行结果 " + res);
        exitVal =  process.waitFor();
        if (exitVal != 0 && !cmd.contains("is-enabled") && !cmd.contains("is-active")){
            log.error("命令 " + cmd + " 执行失败");
            throw new IOException(ERROR_MSG);
        }
        process.destroy();
        ir.close();
        log.info("命令 " + cmd + " 执行成功");
        return res;
    }

    /**
     * 修改配置文件指令key和value
     * @param oldReStr
     * @param newReStr
     * @param filePath
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static void updateConfInfoCmd(String oldReStr, String newReStr, String filePath) throws IOException, InterruptedException {

        String cmdStr = String.format("sed -i 's/%s/%s/g' %s", oldReStr, newReStr, filePath);
        exeCmd(cmdStr);

    }

    /**
     * 向配置文件添加指令key和value
     * @param addStr
     * @param filePath
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static void appendConfInfoCmd(String addStr, String filePath) throws IOException, InterruptedException {

        String cmdStr = String.format("sed -i '$a%s' %s", addStr, filePath);
        exeCmd(cmdStr);

    }

    /**
     * 删除匹配的指定行
     * @param delReStr
     * @param filePath
     * @throws IOException
     * @throws InterruptedException
     */
    public static void deleteConfInfoCmd(String delReStr, String filePath) throws IOException, InterruptedException {
        String cmdStr = String.format("sed -i '/%s/d' %s", delReStr, filePath);
        exeCmd(cmdStr);
    }

    /**
     * 获取配置文件key对应的值
     * @param reKey
     * @param filePath
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static String getConfKeyOfVal(String reKey, String filePath) throws IOException, InterruptedException {
        String cmd = String.format("sed -n '/^%s/p' %s", reKey, filePath);
        String res = exeCmd(cmd);

        // 提出匹配内容
        return res.replace(reKey, "").replace("<br>", "");
    }

    /**
     * 判断文件是否存在指定字符
     * @param reStr
     * @param filePath
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static Boolean isExistStr(String reStr, String filePath) throws IOException, InterruptedException {
        String cmd = String.format("grep '%s' %s && echo \"contain\"||echo \"not contain\"", reStr, filePath);
        String res = exeCmd(cmd);
        if (res.contains("not contain")){
           return false;
        }
        return true;
    }

    /**
     * 文件备份
     * @param sourceFile
     * @param destFile
     * @throws IOException
     * @throws InterruptedException
     */
    public static void cpFile(String sourceFile, String destFile) throws IOException, InterruptedException {
        String cmd = String.format("cp %s %s",sourceFile, destFile);
        exeCmd(cmd);
    }


    /**
     * 判断文件是否存在
     * @param filePath
     * @param fileName
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static Boolean isExistFile(String filePath, String fileName) throws IOException, InterruptedException {
        String cmdStr = String.format("ls %s | grep -q '%s' && echo 'true' || echo 'false'", filePath, fileName);
        return Boolean.valueOf(cmdStr);
    }


}