{"title":"Java MiniShell: A Simple Command Line Interpreter with Configuration","description":"This Java code implements a basic command-line interpreter called MiniShell. It allows users to execute commands such as ls, cd, mkdir, rmdir, rm, touch, chmod, cat, tar, history, and pwd. The application also supports configuration through a miniShell.conf file.","keywords":"Java, MiniShell, command line interpreter, CLI, configuration, ls, cd, mkdir, rmdir, rm, touch, chmod, cat, tar, history, pwd, file system, command execution","content":"import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class MiniShell {private static final String CONFIG_FILE = "miniShell.conf";private static final String DEFAULT_PROMPT = "# ";private static final String DEFAULT_BACKGROUND = "black";private static final String DEFAULT_FONT = "宋体";private static final int DEFAULT_FONTSIZE = 30;private static final String DEFAULT_FONTCOLOR = "white";private String prompt;private String background;private String font;private int fontSize;private String fontColor;private List commandHistory;public MiniShell() {this.prompt = DEFAULT_PROMPT;this.background = DEFAULT_BACKGROUND;this.font = DEFAULT_FONT;this.fontSize = DEFAULT_FONTSIZE;this.fontColor = DEFAULT_FONTCOLOR;this.commandHistory = new ArrayList<>();}public void loadConfig() {try (BufferedReader reader = new BufferedReader(new FileReader(CONFIG_FILE))) {String line;while ((line = reader.readLine()) != null) {line = line.trim();if (!line.isEmpty() && !line.startsWith("#")) {String[] parts = line.split("=");if (parts.length == 2) {String key = parts[0].trim();String value = parts[1].trim();switch (key) {case "promote":this.prompt = value;break;case "background":this.background = value;break;case "font":this.font = value;break;case "fontsize":this.fontSize = Integer.parseInt(value);break;case "fontcolor":this.fontColor = value;break;}}}}catch (IOException e) {// Ignore if config file is not found or cannot be read}}public void start() {Scanner scanner = new Scanner(System.in);while (true) {System.out.print(prompt);String command = scanner.nextLine();commandHistory.add(command);if (command.equals("exit")) {break;}executeCommand(command);}scanner.close();}private void executeCommand(String command) {String[] parts = command.split(" ");String cmd = parts[0];switch (cmd) {case "ls":{// Handle ls command}break;case "cd":{// Handle cd command}break;case "mkdir":{// Handle mkdir command}break;case "rmdir":{// Handle rmdir command}break;case "rm":{// Handle rm command}break;case "touch":{// Handle touch command}break;case "chmod":{// Handle chmod command}break;case "cat":{// Handle cat command}break;case "tar":{// Handle tar command}break;case "history":{printCommandHistory();}break;case "pwd":{// Handle pwd command}break;default:System.out.println("Unknown command: " + cmd);}}private void printCommandHistory() {for (int i = 0; i < commandHistory.size(); i++) {System.out.println(i + ": " + commandHistory.get(i));}}public static void main(String[] args) {MiniShell miniShell = new MiniShell();miniShell.loadConfig();miniShell.start();}}To start MiniShell, simply run the main method. The main method creates a MiniShell object, loads the configuration file using loadConfig, and then starts the command-line interaction using the start method.


原文地址: https://www.cveoy.top/t/topic/pBWo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录