Editing an encrypted script.

weirdmayo profile image Daniel Mayovsky ・2 min read

So, according to reactions on my previous post about encrypted TMUX scripts, that it was useful, I decided to write a script that would allow on editing this file and encrypting it back again. Essentially its just a little function in bash with some filename manipulation and a sequence of scripts, which I will explain one by one

    filename=$(basename $1 .gpg)
    gpg -d --quiet $1 | cat >> $filename 
    vim $filename 
    gpg -c $filename 
    rm $filename

So what's happening here.

Taking off the .gpg ending.

We first have to decrypt a file into a file that has no .gpg ending, edit it, and then encrypt it again.
To take off the ending we will use a default Unix utility called basename.
In the function above the $1 is the first argument of the function. Which is our file if we execute it as so: encryptedit file.sh.gpg
basename is used to remove the file extension in the end of the file. All you have to do is provide the filename and the extension you want to remove. In our case we are removing the .gpg extension from the first argument ($1), and are assigning to the variable filename:

filename=$(basename $1 .gpg)

Decrypting the file into a file

As I said in the TMUX post, the decryption of a file will just push the output into the stdin if its a plain text file. We can pipe that output anywhere we want. This time, instead of piping it into the sh, we are going to cat it and log the cat output with >> into the $filename.

Editing the file

Well, I use vim, but its just a preference, you can do that in Atom too, but you will have to hault the process inbetween. Since we are editing a script, it has to be something about terminal, lol.

Encrypting the file again

by using the same $filename variable.

Deleting a not encrypted file

just delete it, by using the same $filename variable.

That's about it. If you have any suggestions as to how to improve this script security- or efficiency-wise, please let me know in the comments.

Posted on by:

weirdmayo profile

Daniel Mayovsky


A dude. Preaching Mithril.js to be the best MVC framework.


markdown guide