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"
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?
This is not close to the halting problem, it is harder than the halting problem. ;-)