M
M
Maxim Pavlyuchuk2017-05-25 17:41:11
linux
Maxim Pavlyuchuk, 2017-05-25 17:41:11

How to properly parse a bash command?

There is a command:
ls -l -a && less build.sh
Command - comes first
The value of the command comes immediately after the command (if any)
Options - comes with a '-' or '--' prefix, follows the command
Need to build a tree calls like:
ls
- - l
- - a
less
- build.sh
Now there is an algorithm that builds a similar tree in splits and cycles.
The crux of the matter, what pitfalls can be encountered?
Is there a document showing the hierarchy (priority) of command execution, like in java for example ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2017-05-25
@jcmvbkbc

There is a command:
ls -l -a && less build.sh
Command - comes first
The value of the command comes immediately after the command (if any)
Options - comes with a '-' or '--' prefix, follows the command

It doesn't matter to bash whether they are options or where they start. bash treats them simply as words that it, after some transformations (such as brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, and pathname expansion), passes as parameters to the command being run.
Depends on what this call tree should be and how much possible syntax you want to support. There are a lot of pitfalls, you can start with quotes in the parameters.
man 1 bash

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question