I’m doing the Build your own shell using javascript and stuck at #ni6 . I have managed to implement the single quote issue in echo command. But i can’t figure out the cat command.
I think the task is to make
cat ‘/tmp/quz/f 46’ ‘/tmp/quz/f 14’ ‘/tmp/quz/f 3’
into
cat ‘/tmp/quz/f46’ ‘/tmp/quz/f14’ ‘/tmp/quz/f3’ ]
and execute.
I tried to execute the cat command through the node child-process execFileSync() method. It shows No such file or directory
Here are my logs:
And here’s a snippet of my code:
Jh123x
February 7, 2025, 2:44pm
3
For the cat command, it is a binary available at
/usr/bin/cat
I don’t think you have to implement your own cat command
1 Like
How should i approach it, can you be more specific.
Hi @anoopksurendran1112 , you can reuse the logic implemented in #ip1 Run a program . Let me know if you need further clarification.
This is the code i come up with for #ip1 run a program.
function programRun(answer){
const fileName = answer.split(" ")[0];
const argument = answer.split(" ").slice(1);
const pathEnv = process.env.PATH;
let found = false;
const pathDir = pathEnv.split(":");
for (const filepath of pathDir){
const fullPath = path.join(filepath,fileName);
if(fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()){
execFileSync(fileName, argument, { encoding: 'utf-8', stdio: 'inherit' })
found = true;
break;
}
}
if(found == false){
console.log(answer+": command not found");
}
}
I tried to use the filepath given in the cat command as the fileName in the execFileSync, and it didnt work
function catCommand(answer){
let arg = answer.split("cat ")[1]
arg = arg.match(/'([^']+)'/g).map(path => path.slice(1, -1));
const filepaths = arg.map(path => path.replace(/\s+/g, ""));
filepaths.forEach(files => {
execFileSync(files, [], { encoding: 'utf-8', stdio: 'inherit' })
});
}
It shows:
[your-program] $ cat '/tmp/baz/f 64' '/tmp/baz/f 88' '/tmp/baz/f 10'
[your-program] node:internal/readline/emitKeypressEvents:74
[tester::#NI6] Output does not match expected value.
[tester::#NI6] Expected: "blueberry mango.mango blueberry.strawberry pear."
[tester::#NI6] Received: "node:internal/readline/emitKeypressEvents:74"
[your-program] throw err;
[your-program] ^
[your-program] <ref *1> Error: spawnSync /tmp/baz/f64 ENOENT