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.

Edit: I’ve created a script that updates ufw rules. I wanted to use multiple IP addresses as a range and multiple interfaces like this:

ufw add limit in on eth0,eth1 from 172.16.0.0/12,10.0.0.0/8,192.168.0.0/16 to any port 22 comment "allow SSH on LAN"

but ufw does not support setting multiple interfaces and multiple interfaces comma separated like ports so I created a script instead.

# ...
lan_ip_range=('172.16.0.0/12' '10.0.0.0/8' '192.168.0.0/16')
for ip_lan in "${lan_ip_range[@]}"; do
	# SSH
	ufw add limit in on eth0 from "$ip_lan" to any port 22 comment "allow SSH on LAN"
	ufw add limit in on eth1 from "$ip_lan" to any port 22 comment "allow SSH on LAN"
# ...
	done

I want to make sure it does what I expect it to do. so expected output should be something like this:

ufw add limit in on eth0 from 172.16.0.0/12 to any port 22 comment "allow SSH on LAN"
ufw add limit in on eth0 from 10.0.0.0/8 to any port 22 comment "allow SSH on LAN"
ufw add limit in on eth0 from 192.168.0.0/16 to any port 22 comment "allow SSH on LAN"
  • RandomLegend [He/Him]
    link
    fedilink
    English
    10
    edit-2
    1 year ago

    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]
      link
      fedilink
      English
      7
      edit-2
      1 year ago

      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.

      • RandomLegend [He/Him]
        link
        fedilink
        English
        31 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.

      • Agility0971OP
        link
        English
        2
        edit-2
        1 year ago

        I’ve updated the question so you can see what I’m trying to do. If I would do it in a sandbox then a simple docker container should do, but it would be nice to see a “compiled” version of the script. I could imagine a program that runs the shell script in a containerized environment and if it does not find the program it just echoes it in stead of printing an error or something?