LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How The Unix OS translate the shell command (https://www.linuxquestions.org/questions/programming-9/how-the-unix-os-translate-the-shell-command-376631/)

flavor 10-25-2005 04:53 AM

How The Unix OS translate the shell command
 
for example: ls -a

how the unix read "ls" and "-a"

My firend think unix read the command with regex

I don't think so.

murugesan 10-25-2005 05:53 AM

http://<br /> http://xxki.com/note/...ms#content_1_2

Yes the shell takes the parameters as regular expressions if they are specified in correct format.

For example

Code:

int main(int argc,char*argv[])
{
        int i;
        for(i=0;i<argc;i++)
                printf("%s\n",argv[i]);
}

after compiling this program if you run

$ a.out *
the output will show the files in the current directory. This is because shell interprets *
as an operator which means

"Matches zero or more of the preceding characters or expressions. Example: ho*p matches hp, hop and hoop."

bigearsbilly 10-25-2005 09:58 AM

depends which shell I should imagine.

Bash i think reads with readline

jonaskoelker 10-25-2005 12:51 PM

bigearsbilly: I think OP meant how it's parsed, not read.

I don't know, since I haven't hacked on bash, but you could always try #bash on freenode (assuming you use bash). It is, of course, an implementation detail of the individual shell.

My best guess would be that they use flex and yacc, but I don't know.

hth --Jonas

lowpro2k3 10-25-2005 01:02 PM

A shell is simple enough in theory, but remember over time they got the simple stuff working and added more complex features (like shell expansion).

Basically a shell runs in a loop, writing a prompt to stdout, reading a line of any length to stdin, breaking apart the command, then forks + exec's the correct process.

eg:

- step 1 (print prompt)
[user@bash ~]$

- step 2 (read a line until <ENTER> is pressed - '\n')
[user@bash ~]$ ls -l /usr/lib

- step 3 (shell determines if this is a 'built-in' or external command)
- step 4 (realizes 'ls' is an external program)
- step 5 (forks new process, exec's ls and passes all arguments to ls)
- step 6 (ls runs, processes its own arguments, prints output to stdout)
[user@bash ~]$ ls -l /usr/lib
output of all files in /usr/lib

- step 7 (bash takes over control when ls finishes executing, prints prompt)
[user@bash ~]$

- step 8 (bash waits for next command to be entered until '\n' is pressed)


Its more complex than that, but thats the jist of it. Remember bash is filled with features, so it has to determine aliases, execute programs inline, set up pipes (I think), perform wildcard expansion and more. There is an order to this, if you're really interested I learned alot of this from a shell programming book. I forget most of it because it was for school and I was never as interested in bash as I was in other languages, but a good shell book or tutorial might help you understand the process.


All times are GMT -5. The time now is 04:03 AM.