this post was submitted on 28 Jun 2023
15 points (100.0% liked)

Linux

46777 readers
1945 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
15
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]
 

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.

top 8 comments
sorted by: hot top controversial new old
[–] [email protected] 9 points 1 year ago* (last edited 1 year ago) (2 children)

Found this over on Stackoverflow

You could try running the script under Kornshell. When you execute a script with ksh -D, it reads the commands and checks them for syntax, but doesn't execute them. Combine that with set -xv, and you'll print out the commands that will be executed.

You can also use set -n for the same effect. Kornshell and BASH are fairly compatible with each other. If it's a pure Bourne shell script, both Kornshell and BASH will execute it pretty much the same.

You can also run ksh -u which will cause unset shell variables to cause the script to fail. However, that wouldn't have caught the catless cat of a nonexistent file. In that case, the shell variable was set. It was set to null.

Of course, you could run the script under a restricted shell too, but that's probably not going to uninstall the package.

That's the best you can probably do.

[–] [email protected] 6 points 1 year ago* (last edited 1 year ago) (1 children)

I agree that's probably the best you can do, but if it just printing the statements it sees and not actually running them, the behavior when it is run could be very different. For example:

touch a_file
if test -f a_file; then 
  rm -rf / 
fi

To do what OP is asking for would require running inside a sandbox.

[–] [email protected] 2 points 1 year ago

yeah i think a sandbox would be the best solution.

Depending on what script OP is trying to run it would be best to just "rebuild" the potentially affected part of your system inside a VM and see what happens.

[–] [email protected] 1 points 1 year ago

This is great—I've somehow never noticed set -n before. Very helpful.

[–] [email protected] 5 points 1 year ago* (last edited 1 year ago) (1 children)

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 4 points 1 year ago (1 children)

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

[–] [email protected] 1 points 1 year ago (1 children)

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 1 points 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.

load more comments
view more: next ›