shell - is there a way to check if a bash script is complete or not? -


i'm trying implement repl (read-eval-print loop) in bash. if such thing exists, please ignore following , answer question pointer it.

let's use script example (name test.sh):

if true   echo else   echo b fi echo c 

what want read script line line, check if have read far complete bash expression; if complete, eval it; otherwise keep on reading next line. script below illustrates idea (it not quite work, though).

x="" while read -r line   x=$x$'\n'$line  # concatenate \n   # line below bad way go   if eval $x 2>/dev/null;     eval $x  # code seems working, eval     x=""  # empty x, , start collecting code again   else     echo 'incomplete expression'   fi done < test.sh 

motivation

for bash script, want parse syntactically complete expressions, evaluate each expression, capture output, , mark source code , output (say, using markdown/html/latex/...). example, script

echo echo b 

what want achieve output this:

```bash echo ```  ``` ```  ```bash echo b ```  ``` b ``` 

instead of evaluating whole script , capture output:

```bash echo echo b ```  ``` b ``` 

bash -n -c "$command_text" 

...will determine whether $command_text syntactically valid script without executing it.


note there's huge breadth of space between "syntactically valid" , "correct". consider adopting http://shellcheck.net/ if want parse language.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -