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

Linux

48372 readers
1441 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
16
submitted 1 year ago* (last edited 1 year ago) by Agility0971 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.

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"
you are viewing a single comment's thread
view the rest of the comments
[–] saucyloggins 1 points 1 year ago (1 children)

This is way over complicating the issue. Just replace the actual commands you’re worried about with printf. You can even format it so it prints the exact command you WOULD run.

If you want to get fancy with add a flag to you script like —test or —execute or something where it either prints what it’s going to run or actually executes them.

[–] Agility0971 2 points 1 year ago

yes, I could do that but If I ever would check some other script this way, a script I didn't write my self, then I would need to edit that as well. I'm now thinking of something like a container that runs a script and replaces command not found errors with an echo of the command in question.