How Command Line Parameters Are Parsed
@sourabhtk37
Question:
How does arguments written in command line reaches the program magically in its form?
SHELL*
EXECVE
MyProg
* - Not always shell.
We were here
EXECVE
#include <unistd.h>
int execve ( const char *filename, char *const argv[], char *const envp[]);
execve -> executes program
filename -> file to be executed/run
argv -> argument vector to be provided
envp -> list of name=value pair in environment
Alternative cousins: execvp, execl, execlp, execle, and execvp
OBSERVATIONS
-
Shell provided parsed argv to "run_execve"
-
Shell -> execve( "run_execve") -> execve("my_prog")
SHELL*
EXECVE
MyProg
* - Not always shell.
We were here
FACT
Cmd-line parsing in Linux is done by the calling process/program before the new process is created.
In Windows it's the opposite.
BREAKING DOWN SHELL
Breaking Down Shell
- Read from file(shell script)
$ sh haxxor.sh
- or from terminal
$ chsh -s `which fish`
Breaking Down Shell
- Convert to tokens Using the "Quoting" rules.[0]
- Alias Expansion
Breaking Down Shell
- Parsing tokens into simple and compound commands
• Simple Commands: | The most common type of command. | |
• Pipelines: | Connecting the input and output of several commands. | |
• Lists: | How to execute commands sequentially. | |
• Compound Commands: | Shell commands for control flow. | |
• Coprocesses: | Two-way communication between commands. | |
• GNU Parallel: | Running commands in parallel. |
Compound commands
- ( ) -> ( list-of-commands )
- A sub-shell environment is created for execution.
- { } -> { list-of-commands; }
- No sub-shell environment is created.
Breaking Down Shell
- Perform Shell Expansion
-
Brace expansion
-
Tilde expansion
-
Parameter and variable expansion
-
Command substitution
-
Arithmetic expansion
-
Word splitting
- Filename expansion
Brace
Tilde
$ mkdir /dir/repo/{stage,develop,master}
# is expanded to
$ mkdir /dir/repo/stage
$ mkdir /dir/repo/develop
$ mkdir /dir/repo/master
$ ~
The value of $HOME
$ ~/foo
$HOME/foo
$ ~+/foo
$PWD/foo
Shell Parameter
$ array[0]=FishIsNice
$ echo ${array:0:4}
Fish
Breaking Down Shell
- Filename Expansion
$ rm *.py *.c
# expands to
$ rm xyz.py doge.py exce.py prog.c more.c
Breaking Down Shell
- Redirection of input or output
$ yes "USE i3 and fish" > /dev/null
Breaking Down Shell
- Executing the command. Finally!
- Checks for shell functions
- Checks in list of builtin
- Checks in Hash
- Searches in $PATH for executable
- Command executed in separate execution environment. From here, arguments are supplied.
- Waits for command execution and exit status
SHELL*
EXECVE
MyProg
* - Not always shell.
We were here
Questions?
Thank You.
How command line parameters are parsed
By tk sourabh
How command line parameters are parsed
- 2,332