-2

In my class assignment I'm supposed to make my own command shell. In the shell, I am supposed to have it take various command line arguments including logical operators such as && and ||. I initially assumed that I had to program the shell to behave in the logic such that if the first command is executed, only then the second will proceed if I wanted to make it all work by calling it with execvp(). However, I realized that execp() second parameter probably accounts for the && connector and I could simply just place it as an argument. If I entered execvp("./shell", "echo hello && mkdir testing") will it account for the logical operator &&?

  • Did you try it? – Kusalananda May 11 '19 at 11:03
  • Yeah I did and it worked if I do ./shell echo hello && mkdir testing in the CLI which essentially goes through shell.cpp and inputs it into execvp(). However, when I call shell implementation file from a bash script, it doesn't work. – Code4life May 11 '19 at 11:12
  • It is not clear what you have tried, or what you are trying to do. Please improve question to make it clear. – ctrl-alt-delor May 11 '19 at 11:19
  • @Code4life If you typed that command at an ordinary shell prompt, that shell would run the mkdir if your shell command exited with a zero exit status. – Kusalananda May 11 '19 at 11:23
  • so how would I know that my shell failed and it was the original shell interface that executed the command? The shell that I made has perror lines that notifies me if something went wrong with either the fork() or execvp() processes. – Code4life May 11 '19 at 11:40
2

The exec*() calls just start a program and pass the command line arguments to it. The underlying system call doesn't have any knowledge of what the arguments are supposed to mean, that's up to the process that was started.

You could use exec*() to run a POSIX-compatible shell, and pass that command line to it, e.g. with execl("/bin/sh", "/bin/sh", "-c", "echo hello && mkdir testing", (char *) NULL). The shell would then run that mkdir only if the echo succeeded.

But since your assignment was to implement a shell and the && operator yourself, doing it by calling an external, already existing shell doesn't really seem to be in the spirit of the assignment. You could just use some existing shell to begin with, and not bother writing a single line of C code.

| improve this answer | |
  • if you call execp as in this answer, it will crash or return an error, and nothing of what you say will happen. – mosvy May 11 '19 at 14:34
  • @mosvy, yep. I copied it from the question and apparently didn't think. Of course it was wrong, in several ways. Maybe better now. – ilkkachu May 11 '19 at 15:55
  • You better write (void*)0 instead of NULL. NULL can be defined to simply 0, in which case an 32bit int will be passed to the execl variadic function, which will try to fetch a 64bit pointer and will get garbage instead of a null pointer. – mosvy May 11 '19 at 16:45
  • @mosvy, yep. The Linux man pages have (char *) NULL, I think I like that more since the other args are char *'s too, and writing NULL seems clearer to me than just using 0. – ilkkachu May 11 '19 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.