Results 1 to 10 of 28
Hello, i've spent many days already searching and searching on the topic, and i couldnt find enough info to get me (and my team) started. We have (for our college) ...
- 04-24-2008 #1Just Joined!
- Join Date
- Apr 2008
- Posts
- 28
writtign my own linux shell using BASH, guidelines? examples? thx
Hello, i've spent many days already searching and searching on the topic, and i couldnt find enough info to get me (and my team) started. We have (for our college) to write a shell that will be mounted (or whatever the correct translation for "montar" applies here) on bash (debian etch). Its a basic shell (or so we have been told), but its something "extra", not explained on the clases or anything, so we are on our own here. (we only got a brieff explanation with examples on bash scripting, but google was more usefull on that matter than the class :P, but i wasnt able to find example shells or something that pointed us in the right direction, only a LOT of general info about shell scripting), but we know the basics of programing from other assingments/classes we had in the past.
What we must do:
-write a core for the shell, that will:
1) check if there are no modules loaded for the user that logs in
2) periodically check if the config files for the logged user has changed
3) some more stuff
4) always wait for input commands
* the modules are what will do stuff with the comands, there are some, the security one is for checking if the command entered is available to the user or not, for example
Now, the thing is, that we have not been able to find a simple (or complex, or whatever) shell, written in bash, to see how it works...
what we think for now, is that the main script will have a loop (while), and will keep waiting for commands to be entered... now.. how do we make it recieve the commands, and send em to the modules (if there is any) or to bash (if the command passed all the modules without errors, or if there are no modules loaded), or execute it by itself if it was a built in...
all this, while from time to time (set intervals), check for configuration changes :P
NOW, we still have many doubts about how to manage the commands entered... some options are:
read $variable, and work with the variable, but some commands (cd for example) seems not to work properly (we are still experimenting).
cat "something" (its not my idea and i didnt understand it well yet)
other ?
this is my first post in a programming forum, so i might not be doing it ok, so, my appologies, and any help will be appreciated.
- 04-24-2008 #2Just Joined!
- Join Date
- Apr 2008
- Posts
- 28
here is a summary of the "homework":
The proyect is to develop a shell (from now on myshell) built only by shell scripting. Such script will be executed by (or over, dunno how to correctly translate) bash, wich is the default shell asociated to every user in linux.
Myshell primary caracteristics will be the posibility of adding/removing modules in a dinamic way, establish configurations for each user, and, by being "mounted over bash", use all the functionality that it offers. In this way, we can think of myshell as a wrapper of bash, to wich it adds functionalities.
maybe with that rough translation of the "introduction/summary"of the proyect, i make things clearer.
- 04-24-2008 #3
You'll have to do all the design work yourselves, of course. This is homework, after all!
And I'll bet there are no sample shells written in bash out there. Write your own. The biggest part will be designing it.
Start with something simple which is a loop that reads a command and says something like "I just read this command: cat a b".
Then add one feature after another, testing each as you go.
If you have a question about how to do a particular task in bash, review the many bash tutorials that are out there. If you are really stumped, maybe you can come back here with a specific, narrow question.
Your project sounds like fun! Good luck!--
Bill
Old age and treachery will overcome youth and skill.
- 04-24-2008 #4Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
This is something really mad. Fun, no doubt, but still insane hehe. However it can be very ilustrative, and for sure that you will have to learn a lot about bash to do this.
As wje_lf said, you should start with something really basic: the main loop. After that, you can go on from there. Ask if you have a doubt on how to implement something in particular.
- 04-24-2008 #5
The basic piece of this is something called a read-eval loop. This is a loop that just constantly reads in an instruction, executes it, and then waits for the next instruction.
If you're really stuck, you might be able to try implementing a basic shell in C (for which many more tutorials are available), so that you have a basic idea of what goes into a shell design.
Good luck!DISTRO=Arch
Registered Linux User #388732
- 04-24-2008 #6Just Joined!
- Join Date
- Apr 2008
- Posts
- 28
the basic idea for the loop would be:
thats mostly pseudo code or worse :PCode:while( comand != exit) { Read User input send it to loaded modules, one at a time if none returns an error msg execute it/send it to bash for execution (whichever aplies) Else Spit out an error }
now, the thing is, if i write a script, lets say... like this (here there is no sending the comand to modules, its just executed, i thought that woul be an inteligent way to start :P):
i get no promt between comands, nor can i "browse" to a previous command i may have used by pressing the up/down arrows.Code:#!/bin/bash while [ COMANDO != exit ] do read COMANDO $COMANDO done
ill KEEP investigating, but i already saw (forgot the past tense of read) many bash tutorials, and none informed me about how to start with this :P
an user in another forum (who i think, understood what we must do from my not so good explanation/translation) said:
will try to google about that too.Essentially, I see cocchiararo's task as writing a Bash superset shell - on top of the very thing it's superseding (Bash).
edit:
will look into that too, thx.
- 04-24-2008 #7Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
The prompt is only drawn on interactive shells. And as long as your script is running, you are not into an interactive shell, so, if you want a prompt, you will have to print it yourself from the script.
In which regards the keystrokes, bash will not be reading them either. When the loop stops, it's because the read command is runnings, so, all the input goes to read, and not bash.
You can't use interactive features like those when you are running a script. You will have to implement your own way to read those keystrokes if they are really relevant for you, and then a way to use the bash history from your script.
The input problem is one of the many things that turn this task into an insane one, as I said in my other post. Bash is not really suited for this task, and any other language would probably be better.
It's probably trivial to solve a few problems and do some basic wrapping, but if you need ALL the bash features like keystrokes, tab completion and variable substitution, you are going to have to replicate everything that bash can do into a bash script, which, if you ask me, is not only an overkill, but also represents an insane amount of work (I'd never think on doing this seriously, so I can't really be sure if it's even possible).
It's read, read, read (base, past, participle). They are all the same :Pill KEEP investigating, but i already saw (forgot the past tense of read) many bash tutorials, and none informed me about how to start with this :P
- 04-24-2008 #8Just Joined!
- Join Date
- Apr 2008
- Posts
- 28
well, i need to ask our "team leader" (missing now
) to ask if we need to replicate this much... the true objective behind this is for us to learn shell scripting, or so they say 
maybe its simplier than i thought, ill come back when i have more info i think :P
pd: maybe i was confused cause the base is pronounced in a way, and the other 2 something like "red"? :P thx there too.
- 04-25-2008 #9Just Joined!
- Join Date
- Apr 2008
- Posts
- 28
ok, so read -e allows for history (using the up/down arrows to use previously used commands).
read -p "message/variable" works for writing a "prompt", but since we didnt want to hardcode it, we tried $PS1 as the -p message, and it shows a BLANK line if we ran it from a script (i guess we needed to export/import the variable or something before using it htere), and if i run echo $PS1 in the terminal, i get: ${debian_chroot:+($debian_chroot)}\u@\h:\w\$
and an echo of that gives me u@h:w$
now, i know (or so i think :P) that the value of PS1 is what is displayed as prompt, but i dont get why what we are doing isnt working :P
for the rest (using read to store the commands, and then $comando or echo $comando | bash not working, we were pointed towards history and eval, and are looking into it).
edit: oh, just noticed... eval $comando was what we needed, so until we decide how to make the prompt, well start "coding" the rest of the "core" and then the modules.
ill be back if i get stuck again XD
- 04-25-2008 #10Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
As I said above, that variable is not imported into non-interactive sessions. And that means that you can't access it on any easy way using a script. In fact, a common way to detect if you are running on interactive mode or not, is to check if PS1 is set.
You should really read the bash man page if you are going to use obscure features:
Originally Posted by bash man page And if you use echo, then you also ought to read "man echo". It's a very short page.and an echo of that gives me u@h:w$
About the prompt, since you are not in bash, I suggest that you design a new prompt for your shell. Making it the same than in bash can be confusing. You can also define your custom format for it, and store it into any other variable, like MYSH_PROMPT or whatever.
If you truly want to use PS1 the only way that you can do so is by sourcing your script. Like this:
If my_shell.sh contains an "echo $PS1", then now it will work, because the script is being dumped into the current (interactive) shell, rather than being executed in a newly forked non-interactive session of bash.Code:source my_shell.sh
Another way would be to invoke the script regularly, but previously exporting the variable into another container:
Then you can access the contents of PS1 into $BLAH on your script.Code:BLAH="$PS1" ./my_shell.sh
Surely, none of these solutions is satisfactory, so, you will need to design a new prompt system or something.



