I wonder is there any program that can take a bash script as input and print out all bash commands it will run? A program that would unroll loops, expand environment variables and generally not perform any destructive action nor call any external binaries. It’s like a dry run of sorts.

  • bionicjoey@lemmy.ca
    link
    fedilink
    English
    arrow-up
    6
    ·
    edit-2
    1 year ago

    It would depend. Bash allows for command substitution, so it’s possible that there are commands in a script where you would only know what they would do by running other lines in the script.

    Edit: also, this is treading dangerously close to the Halting Problem. Imagine for a moment that you succeeded in creating such a program, written in Bash. Now imagine you gave this program its own source code as input. What would you expect it to tell you?

    • wolf@lemmy.zip
      link
      fedilink
      English
      arrow-up
      4
      ·
      1 year ago

      This is not close to the halting problem, it is harder than the halting problem. ;-)

      • bionicjoey@lemmy.ca
        link
        fedilink
        English
        arrow-up
        1
        ·
        1 year ago

        Hmmm, it depends a bit on what exactly you are trying to achieve. If you want to know “what is the full extent of commands that might be called by this program, not including their arguments or their environment variables” then it could be done. Bash has a solid definition for which token in any command line is the command (normally the first token which isn’t a variable assignment), but that would miss things like if a script uses a command which executes everything after it as a command (such as time or sudo), or if there are any eval calls.

        • wolf@lemmy.zip
          link
          fedilink
          English
          arrow-up
          2
          ·
          1 year ago

          Of course, if you narrow the problem scope you might be able to solve it. OP was talking about unrolling loops etc. which I interpreted as having the exact amount of times a loop is executed. AFAIK this would lead to a state explosion for nearly all non-trivial cases, and we already now that knowing if a program (incl. loops) terminates is not generally solvable.