-1

I'm on 1st year of IT Studies and I've got an exercise. I have to write my own shell for Linux using C. I have a few points to do, and one of them is my shell has to use 2-3 of shell builtin commands from man bash. How can I implement this functionality to my code? By using functions like system() or execl() or isn't it correct in this case and there are any other options?

6
  • Which builtins? Do you get to pick? cd, echo, exit, kill, pwd Commented Jan 25, 2018 at 12:07
  • Good question. I received email with guidelines and one of them was "Supports two other built-in bash shell commands (section "SHELL BUILTIN COMMANDS" in man bash)". That's all. I wanted to use smth like pwd or ls. Commented Jan 25, 2018 at 12:09
  • 1
    The five there are probably good ones for you to look into, then. Commented Jan 25, 2018 at 12:11
  • Are they bash builtin commands? I'm looking at this page at now and they are 2 sections: Bash builtins and Bourne shell builtins. Is that the same? gnu.org/software/bash/manual/html_node/… Commented Jan 25, 2018 at 12:14
  • 3
    I would imagine you're meant to write your own version of those builtin functions rather than spawning bash to call them Commented Jan 25, 2018 at 12:14

2 Answers 2

1

(I am understanding you are asked to implement your own tiny unix shell; this is a very good but common exercise)

Most of the time, you cannot use bash builtin commands (e.g. cd, ulimit, etc...) in your own shell. And you should not use bash or /bin/sh thru system(3) but use directly fork(2), execve(2) etc..., and implement your own globbing (see glob(7)).

As an example, you have to explicitly implement some cd command (calling the chdir(2) system call) yourself in your shell. Because the current working directory is a specific property of every process (so each process has its own working directory). See also credentials(7), fork(2), execve(2).

That is why you cannot implement your cd foo by doing system("cd foo"), since system(3) starts a separate, new, /bin/sh process (and the cd foo would only chdir in that new sh process, not in yours).

Most Unix shells are free software. Feel free to study their source code. Consider also using strace(1) to understand what system calls they are doing.

Read also some book on Linux programming (e.g. ALP or something newer). See also intro(2), syscalls(2) and the man pages referenced from them.

Sign up to request clarification or add additional context in comments.

3 Comments

I would study some books with pleasure, but I receive this task 3 days ago and I have to send it until monday. At the university from October we were learning only simple commands like copying files, chmod, chdir, etc. And a few things about bash scripting. So this task is quite difficult for me and I'm searching Internet now to understand that what I need to do this.
Then study the source code of some existing shell, notably sash (whose code is really small). And do spend a few hours reading relevant chapters of ALP (or any other Linux programming book). You need to learn, and to increase your skills
Yup, I know. I'm looking at projects on github to understand something. I think reading code which someone else wrote is a good idea. I did most of guidelines like setting environment variables or changing directory but this one is a problem for me.
-1

If I understood your question correctly, you want to code a shell able to execute bash command in C. In that case:

You can use Execve (man execve), it is able to run bash command

You can find the path in your env. (main(int ac, char** argv, char** env))

You have to use fork() when you run a command, because execve() close your prog.

1 Comment

No; wrong! the OP is coding his own (tiny) shell, bash-like but a lot simpler (e.g. similar to sash)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.