[shell] dry_run for your commands

Sarvsav Sharma
2 min readJul 16, 2023

--

try lets you run a command and inspect its effects before changing your live system. Source code: binpash/try

source: binpash/try

Scenario 1: Imagine you are working on a important project, and created a new file named .env.sample containing the secrets. And, after running the project everything worked well, and in excitement you commited and pushed everything to centralised version control system like GitHub.

Scenario 2: By mistake, you have run rm on a specific folder, and realised it contained some important documents.

With try , you can save yourself from such situations.

source: binpash/try

Installation

To install the try , just clone it to your $HOME/bin folder, and create soft link to the binary by running below command.

mkdir -p $HOME/bin ## Add it to your PATH variable
cd $HOME/bin
git clone https://github.com/binpash/try.git
ln -s try/try dry_run

Usage

I have an important_file1 inside the important project.

➜  cat important_project/important_file1 
───────┬─────────────────────────────────────────
│ File: important_project/important_file1
───────┼─────────────────────────────────────────
1 │ Author: Sarvsav
2 │
3 │ Some important data.
───────┴─────────────────────────────────────────
➜  dry_run mv important_project/important_file1 important_project/important_file  

Changes detected in the following files:

/tmp/tmp.5gtdcUb1W9/upperdir/home/sarvsav/Projects/important_project/important_file (modified/added)
/tmp/tmp.5gtdcUb1W9/upperdir/home/sarvsav/Projects/important_project/important_file1 (deleted)

Commit these changes? [y/N] N
Not commiting.
/tmp/tmp.5gtdcUb1W9

It showed me, what changes will happen on filesystem. On choosing N , there will be no changes made to the file.

➜  cat important_project/important_file1 
───────┬─────────────────────────────────────────
│ File: important_project/important_file1
───────┼─────────────────────────────────────────
1 │ Author: Sarvsav
2 │
3 │ Some important data.
───────┴─────────────────────────────────────────

Run the mv command again, and this time commit the changes.

➜  dry_run mv important_project/important_file1 important_project/important_file

Changes detected in the following files:

/tmp/tmp.rhPFQQZ0AU/upperdir/home/sarvsav/Projects/important_project/important_file (modified/added)
/tmp/tmp.rhPFQQZ0AU/upperdir/home/sarvsav/Projects/important_project/important_file1 (deleted)

Commit these changes? [y/N] y

And, accepted the changes this time.

➜  cat important_project/important_file1                                        
cat: 'important_project/important_file1': No such file or directory (os error 2)

Nice, the file has been successfully moved to new file and can be verified.

➜  cat important_project/important_file
───────┬─────────────────────────────────────────
│ File: important_project/important_file
───────┼─────────────────────────────────────────
1 │ Author: Sarvsav
2 │
3 │ Some important data.
───────┴─────────────────────────────────────────

Kindly shower your love ❤ and support 🙏️ if you have enjoyed the article.

--

--

Sarvsav Sharma
Sarvsav Sharma

Written by Sarvsav Sharma

Mostly writes about Go, GitHub, and DevSecOps. Interested in distributed systems. GitHub:sarvsav Maintainer:go-feature-flag/gofeatureflag-lint-action