Created
February 18, 2025 00:35
-
-
Save akishore15/2ecc34f5bae61182fccd21eb9ff94812 to your computer and use it in GitHub Desktop.
Shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require('readline'); | |
const { execSync } = require('child_process'); | |
// Create an interface for reading input from the command line | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
prompt: 'shell> ' | |
}); | |
// Function to execute commands | |
const executeCommand = (command) => { | |
try { | |
const output = execSync(command, { stdio: 'pipe' }).toString(); | |
console.log(output); | |
} catch (error) { | |
console.error(`Error: ${error.message}`); | |
} | |
}; | |
// Display the prompt and wait for user input | |
rl.prompt(); | |
rl.on('line', (line) => { | |
const command = line.trim(); | |
if (command.toLowerCase() === 'exit') { | |
rl.close(); | |
} else { | |
executeCommand(command); | |
rl.prompt(); | |
} | |
}).on('close', () => { | |
console.log('Exiting shell...'); | |
process.exit(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment