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

后端 未结 4 824
囚心锁ツ
囚心锁ツ 2021-01-03 21:08

I\'m trying to implement a REPL (read-eval-print loop) in bash. If such a thing already exists, please ignore the following and answer this question with a pointer to it.

4条回答
  •  情话喂你
    2021-01-03 21:29

    Assume your test commands are stored in a file called "example". That is, using same commands than in previous answer:

    $ cat example
    x=3
    echo "$x"
    y=$(($x+1))
    echo "$y"
    
    
    while [ "$y" -gt "0" ]
    do
        echo $y
        y=$(($y-1))
    done
    

    the command:

    $ (echo 'PS1=; PROMPT_COMMAND="echo -n =====; echo"'; cat example2 ) | bash -i
    

    produces:

    =====
    x=3
    =====
    echo "$x"
    3
    =====
    y=$(($x+1))
    =====
    echo "$y"
    4
    =====
    
    =====
    
    =====
    while [ "$y" -gt "0" ]
    > do
    >     echo $y
    >     y=$(($y-1))
    > done
    4
    3
    2
    1
    =====
    exit
    

    if you are interested also in the intermediate results of a loop, the command:

    $ ( echo 'trap '"'"'echo; echo command: $BASH_COMMAND; echo answer:'"'"' DEBUG'; cat example ) | bash
    

    results in:

    command: x=3
    answer:
    
    command: echo "$x"
    answer:
    3
    
    command: y=$(($x+1))
    answer:
    
    command: echo "$y"
    answer:
    4
    
    command: [ "$y" -gt "0" ]
    answer:
    
    command: echo $y
    answer:
    4
    
    command: y=$(($y-1))
    answer:
    
    command: [ "$y" -gt "0" ]
    answer:
    
    command: echo $y
    answer:
    3
    
    command: y=$(($y-1))
    answer:
    
    command: [ "$y" -gt "0" ]
    answer:
    
    command: echo $y
    answer:
    2
    
    command: y=$(($y-1))
    answer:
    
    command: [ "$y" -gt "0" ]
    answer:
    
    command: echo $y
    answer:
    1
    
    command: y=$(($y-1))
    answer:
    
    command: [ "$y" -gt "0" ]
    answer:
    

    Addendum 1

    It is not difficult to change the previous results to some other format. By example, this small perl script:

    $ cat formatter.pl 
    #!/usr/bin/perl
    #
    
    $state=4; # 0: answer, 1: first line command, 2: more command, 4: unknown
    
    while(<>) {
    #  print $state;
    
      if( /^===COMMAND===/ ) {
        print "===\n";
        $state=1;
        next;
      }
    
      if( $state == 1 ) {
        print;
        $state=2;
        next;
      }
    
      if( $state == 2 && /^>+ (.*)/ ) {
        print "$1\n";
        next;
      }
    
      if( $state == 2 ) {
        print "---\n";
        $state=0;
        redo;
      }
    
      if( $state == 0 ) {
        print;
        next;
      }
    }
    

    when used in command:

    ( echo 'PS1="===COMMAND===\n"'; cat example ) | bash -i 2>&1 | ./formatter.pl

    gives this result:

    ===
    x=3
    ===
    echo "$x"
    ---
    3
    ===
    y=$(($x+1))
    ===
    echo "$y"
    ---
    4
    ===
    
    ===
    
    ===
    while [ "$y" -gt "0" ]
    do
        echo $y
        y=$(($y-1))
    done
    ---
    4
    3
    2
    1
    ===
    exit
    

提交回复
热议问题