自动运行命令

This commit is contained in:
2026-01-31 14:01:50 +08:00
parent 1c626a96d4
commit 2540529b66
3 changed files with 277 additions and 3 deletions

View File

@@ -121,6 +121,33 @@ namespace CMLeonOS
}
}
public List<string> GetFileList(string path = ".")
{
string fullPath = GetFullPath(path);
List<string> fileList = new List<string>();
try
{
if (Directory.Exists(fullPath))
{
// 获取文件列表
var files = Directory.GetFiles(fullPath);
foreach (var file in files)
{
// 使用Path.GetFileName获取文件名
string fileName = Path.GetFileName(file);
fileList.Add(fileName);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error getting file list: {ex.Message}");
}
return fileList;
}
public void CreateFile(string path, string content = "")
{
string fullPath = GetFullPath(path);

View File

@@ -35,9 +35,10 @@ namespace CMLeonOS
Sys.FileSystem.VFS.VFSManager.RegisterVFS(fs);
Console.WriteLine("VFS initialized successfully");
// 显示可用空间
// 显示可用空间(动态单位)
var available_space = fs.GetAvailableFreeSpace(@"0:\");
Console.WriteLine("Available Free Space: " + available_space + " bytes");
string spaceWithUnit = FormatBytes(available_space);
Console.WriteLine("Available Free Space: " + spaceWithUnit);
// 显示文件系统类型
var fs_type = fs.GetFileSystemType(@"0:\");
@@ -85,6 +86,12 @@ namespace CMLeonOS
// 登录成功后初始化Shell
shell = new Shell();
// 检查并执行启动脚本
ExecuteStartupScript();
// 系统启动完成,蜂鸣器响一声
Console.Beep();
}
catch (Exception ex)
{
@@ -92,6 +99,74 @@ namespace CMLeonOS
}
}
private void ExecuteStartupScript()
{
string startupFilePath = @"0:\sys\startup.cm";
try
{
// 检查启动脚本文件是否存在
if (System.IO.File.Exists(startupFilePath))
{
// 读取启动脚本内容
string[] lines = System.IO.File.ReadAllLines(startupFilePath);
// 检查文件是否为空
if (lines.Length == 0 || (lines.Length == 1 && string.IsNullOrWhiteSpace(lines[0])))
{
Console.WriteLine("Startup script is empty, skipping...");
return;
}
Console.WriteLine("Executing startup script...");
Console.WriteLine("--------------------------------");
// 逐行执行命令
foreach (string line in lines)
{
// 跳过空行和注释行
if (string.IsNullOrWhiteSpace(line) || line.Trim().StartsWith("#"))
{
continue;
}
// 执行命令
Console.WriteLine($"Executing: {line}");
shell.ExecuteCommand(line);
}
Console.WriteLine("--------------------------------");
Console.WriteLine("Startup script execution completed.");
}
else
{
// 启动脚本不存在,创建空文件
Console.WriteLine("Startup script not found, creating empty file...");
System.IO.File.WriteAllText(startupFilePath, "");
Console.WriteLine("Created empty startup script at: " + startupFilePath);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error executing startup script: {ex.Message}");
}
}
private string FormatBytes(long bytes)
{
string[] units = { "B", "KB", "MB", "GB", "TB" };
int unitIndex = 0;
double size = bytes;
while (size >= 1024 && unitIndex < units.Length - 1)
{
size /= 1024;
unitIndex++;
}
return $"{size:F2} {units[unitIndex]}";
}
protected override void Run()
{
if (shell != null)

174
Shell.cs
View File

@@ -41,6 +41,17 @@ namespace CMLeonOS
}
}
public void ExecuteCommand(string commandLine)
{
var parts = commandLine.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 0)
{
var command = parts[0].ToLower();
var args = parts.Length > 1 ? string.Join(" ", parts, 1, parts.Length - 1) : "";
ProcessCommand(command, args);
}
}
private void ProcessCommand(string command, string args)
{
switch (command)
@@ -89,9 +100,18 @@ namespace CMLeonOS
Console.WriteLine(" tail <file> - Display last lines of file");
Console.WriteLine(" Usage: tail <file> <lines>");
Console.WriteLine(" wc <file> - Count lines, words, characters");
Console.WriteLine(" cp <src> <dst> - Copy file");
Console.WriteLine(" mv <src> <dst> - Move/rename file");
Console.WriteLine(" touch <file> - Create empty file");
Console.WriteLine(" find <name> - Find file");
Console.WriteLine(" version - Show OS version");
Console.WriteLine(" about - Show about information");
Console.WriteLine(" help - Show this help message");
Console.WriteLine();
Console.WriteLine("Startup Script: sys\\startup.cm");
Console.WriteLine(" Commands in this file will be executed on startup");
Console.WriteLine(" Each line should contain one command");
Console.WriteLine(" Lines starting with # are treated as comments");
break;
case "time":
Console.WriteLine(DateTime.Now.ToString());
@@ -212,7 +232,6 @@ namespace CMLeonOS
case "about":
Console.WriteLine("CMLeonOS Test Project");
Console.WriteLine("By LeonOS 2 Developement Team");
Console.WriteLine("A simple operating system built with Cosmos");
break;
case "cpass":
userSystem.ChangePassword();
@@ -226,6 +245,18 @@ namespace CMLeonOS
case "wc":
WordCount(args);
break;
case "cp":
CopyFile(args);
break;
case "mv":
MoveFile(args);
break;
case "touch":
CreateEmptyFile(args);
break;
case "find":
FindFile(args);
break;
default:
Console.WriteLine($"Unknown command: {command}");
break;
@@ -633,5 +664,146 @@ namespace CMLeonOS
Console.WriteLine($"Error reading file: {ex.Message}");
}
}
private void CopyFile(string args)
{
if (string.IsNullOrEmpty(args))
{
Console.WriteLine("Error: Please specify source and destination files");
Console.WriteLine("Usage: cp <source> <destination>");
return;
}
try
{
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
{
Console.WriteLine("Error: Please specify both source and destination");
Console.WriteLine("Usage: cp <source> <destination>");
return;
}
string sourceFile = parts[0];
string destFile = parts[1];
// 使用FileSystem读取源文件内容
string content = fileSystem.ReadFile(sourceFile);
if (content == null)
{
Console.WriteLine($"Error: Source file '{sourceFile}' does not exist");
return;
}
// 使用FileSystem写入目标文件
fileSystem.WriteFile(destFile, content);
Console.WriteLine($"File copied successfully from '{sourceFile}' to '{destFile}'");
}
catch (Exception ex)
{
Console.WriteLine($"Error copying file: {ex.Message}");
}
}
private void MoveFile(string args)
{
if (string.IsNullOrEmpty(args))
{
Console.WriteLine("Error: Please specify source and destination files");
Console.WriteLine("Usage: mv <source> <destination>");
return;
}
try
{
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
{
Console.WriteLine("Error: Please specify both source and destination");
Console.WriteLine("Usage: mv <source> <destination>");
return;
}
string sourceFile = parts[0];
string destFile = parts[1];
// 使用FileSystem读取源文件内容
string content = fileSystem.ReadFile(sourceFile);
if (content == null)
{
Console.WriteLine($"Error: Source file '{sourceFile}' does not exist");
return;
}
// 使用FileSystem写入目标文件
fileSystem.WriteFile(destFile, content);
// 删除源文件
fileSystem.DeleteFile(sourceFile);
Console.WriteLine($"File moved/renamed successfully from '{sourceFile}' to '{destFile}'");
}
catch (Exception ex)
{
Console.WriteLine($"Error moving file: {ex.Message}");
}
}
private void CreateEmptyFile(string args)
{
if (string.IsNullOrEmpty(args))
{
Console.WriteLine("Error: Please specify a file name");
Console.WriteLine("Usage: touch <filename>");
return;
}
try
{
// 使用FileSystem创建空文件
fileSystem.WriteFile(args, "");
Console.WriteLine($"Empty file '{args}' created successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Error creating file: {ex.Message}");
}
}
private void FindFile(string args)
{
if (string.IsNullOrEmpty(args))
{
Console.WriteLine("Error: Please specify a file name to search");
Console.WriteLine("Usage: find <filename>");
return;
}
try
{
// 使用FileSystem获取当前目录的文件列表
var files = fileSystem.GetFileList(".");
bool found = false;
foreach (var file in files)
{
// 检查文件名是否包含搜索字符串
if (file.ToLower().Contains(args.ToLower()))
{
Console.WriteLine($"Found: {file}");
found = true;
}
}
if (!found)
{
Console.WriteLine($"No files found matching '{args}'");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error finding file: {ex.Message}");
}
}
}
}